0

以下のスクリプトを使用して、ページのタイトルを取得しようとしています。ただし、次のエラーが発生し続けるため、何か問題が発生しています。

PHP Fatal error:  Call to a member function getElementsByTagName() on a non-object in /Users/robertquinn/Desktop/SCRAPE/asu.php on line 22  

カール関数を使うのは初めてなので、ここで何かがひどく台無しになっている場合はお知らせください。getElementsByTagName()soleyはXML DOMメソッドですか?

<?php  

    function get_data($url) {

        $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5';
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_COOKIE,"someCookie=2127;onlineSelection=C");
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 100);
        $html = curl_exec($ch);
        curl_close($ch);

        $doc = new DOMDocument();
        $body = $doc->loadHTML( $html );
        $title_value = $body->getElementsByTagName('title')->nodeValue;

        echo $title_value;
    }

    get_data('http://www.someurl.com');

    ?>
4

2 に答える 2

3

DOMDocument部分も変更します。

 $doc = new DOMDocument();
 $doc->loadHTML( $html );
 //Suppress strict errors or you could just suppress errors directly e.g: @$doc->loadHTML( $html );
 $doc->strictErrorChecking = false;

 $title_value = $doc->getElementsByTagName('title')->item(0)->nodeValue;
于 2012-08-18T00:30:20.090 に答える
0

simple_html_domを使用するのは本当に簡単です。たとえば、

...
$page = curl_exec($ch);

$html = str_get_html($page);
echo $html->find('title', 0)->plaintext;
于 2012-08-18T06:40:13.570 に答える