0

simple_html_dom我已经工作了好几个小时,正在研究用于 vine 过滤器网站的错误处理。我需要这样做,以便如果$videoid没有字符串内容,它要么 A. 结束 simple_html_dom 并输出一个普通字符串,如“这里没有内容”之类的东西。我还是新手,请原谅我的错误..

我的错误结果:http: //i.imgur.com/IyC5mJu.png

我的代码

<?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.";
}
?>

把我的头发拉出来,我有一种强烈的感觉,这是一个很容易解决的问题。有人可以帮帮我吗?

尝试了其他变通方法: 如果字符串的数量website大于 kill dom

4

1 回答 1

1

您可能想检查是否$videoid在脚本开头定义。下面的&符号@file_get_html将在找不到页面的情况下抑制错误消息。

<?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.";
}
?>
于 2013-10-10T08:50:18.773 回答