Pregunta

I am trying to get the title and meta description data of page by providing it url of target page but file_get_html() always return FALSE value. Any suggestions? by the way I have enabled the php extension 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";
    }
?>

UPDATE:

So file_get_html() works fine now but any idea about dealing with facebook update browser message?

¿Fue útil?

Solución

<?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);
?>

This displays:

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.
)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top