Question

I've been up for hours now researching error handling with simple_html_dom it's for a vine filter website. I need to make it so that if $videoid has no string content it will either A. End simple_html_dom and output a normal string like "no content is here" kind of thing. I'm still new so excuse my mistakes..

My error result: http://i.imgur.com/IyC5mJu.png

my code

<?php
include('include/simple_html_dom.php');
$website = 'http://vine.co/v/';
// if this is left empty site will display php errors saying page not found.
$videoid = 'haiKqBFA9Yw';
$html = file_get_html($website . $videoid);

if ($html->find('meta[property=og:image]')) {
  foreach ($html->find('meta[property=og:image]') as $element) {
    $img[] = $element->attr['content'];
    }
}

//Change http to https and remove versionId=    
$element->content = str_replace( 'https://', 'http://', $element->content );
$element->content = str_replace( 'versionId=', '', $element->content );

//Remove extra version id junk
$img = $element->content;
$element->content = substr($img, 0, strpos($img, "?"));


// My attempt using isset to determine if it has no content or if it does.. it did not work.
if (isset($videoid)) {
    echo $element->content;
}else{
    echo "It appears $videoid is empty.";
}
?>

Pulling my hair out on this one and I have a strong feeling it's an easy fix. Could someone please give me a hand?

Other work arounds attempted: if string is amount of website than kill dom

Was it helpful?

Solution

You probably want to check if $videoidis defined at the beginning of the script. The ampersand in @file_get_html below will suppress error messages in the case when the page is not found.

<?php
include('include/simple_html_dom.php');
$website = 'http://vine.co/v/';
#$videoid = 'haiKqBFA9Yw';
$videoid = array_key_exists("videoid", $_GET)? $_GET["videoid"]: "";
if ($videoid != "") {
    $page = $website . $videoid;
    $html = @file_get_html($page);
    if ($html) {
        foreach ($html->find('meta[property=og:image]') as $element) {
            $image = $element->content;

            //Change http to https and remove versionId=
            $image = str_replace( 'https://', 'http://', $image );
            $image = str_replace( 'versionId=', '', $image );

            //Remove extra version id junk
            $image = substr($image, 0, strpos($image, "?"));

            $images[] = $image;
        }
        echo "<pre>" . print_r($images, TRUE) . "</pre>";
    } else {
        echo "Page not found: $page";
    }
} else {
    echo "It appears videoid is empty.";
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top