file_get_html()不起作用?

时间:2014-04-21 13:31:26

标签: php simple-html-dom

我试图通过提供目标页面的url来获取页面的标题和元描述数据,但file_get_html()始终返回FALSE值。 有什么建议?顺便说一句,我启用了php扩展php_openssl。

<?php
    include("inc/simple_html_dom.inc.php");
    $contents = file_get_html("https://www.facebook.com"); 
    if($contents !=FALSE) //always skips if condition
    {
        foreach($contents->find('title') as $element) 
        {
            $title = $element->plaintext;
        }

        foreach($contents->find('meta[description]') as $element) 
        {
            $meta_description = $element->plaintext;
        }
        $output = array('title'=>$title, 'meta'=> $meta_description);
        echo json_encode($output);
    }
    else
    {
        echo"Couldn't load contents";
    }
?>

更新:

所以file_get_html()现在运行正常,但是有关处理facebook更新浏览器消息的想法吗?

1 个答案:

答案 0 :(得分:5)

<?php
    $ch = curl_init('https://www.facebook.com/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    /* 
     * XXX: This is not a "fix" for your problem, this is a work-around.  You 
     * should fix your local CAs 
     */
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    /* Set a browser UA so that we aren't told to update */
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36');

    $res = curl_exec($ch);

    if ($res === false) {
        die('error: ' . curl_error($ch));
    }

    curl_close($ch);

    $d = new DOMDocument();
    @$d->loadHTML($res);

    $output = array(
        'title' => '',
        'meta'  => ''
    );

    $x = new DOMXPath($d);

    $title = $x->query("//title");
    if ($title->length > 0) {
        $output['title'] = $title->item(0)->textContent;
    }

    $meta = $x->query("//meta[@name = 'description']");
    if ($meta->length > 0) {
        $output['meta'] = $meta->item(0)->getAttribute('content');
    }

    print_r($output);
?>

显示:

Array
(
    [title] => Welcome to Facebook - Log In, Sign Up or Learn More
    [meta] => Facebook is a social utility that connects people with friends and others who work, study and live around them. People use Facebook to keep up with friends, upload an unlimited number of photos, post links and videos, and learn more about the people they meet.
)