doujingya1166 2013-10-09 10:52
浏览 35
已采纳

简单的html dom检查是否找到文件

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

  • 写回答

1条回答 默认 最新

  • dongnanman9093 2013-10-10 08:50
    关注

    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.";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊