简体   繁体   中英

Simple HTML Dom and CURL error

I have some problem here,

my code is:

<?php
    $curl_handle=curl_init();
    curl_setopt($curl_handle, CURLOPT_URL,'http://www.website.org');
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'name');
    $html = curl_exec($curl_handle);
    curl_close($curl_handle);
    include_once("simple_html_dom.php");
    $element= $html->find("div[class=something]");
    echo $element;
?>

and I get this error: Fatal error: Call to a member function find() on a non-object on line 10

You can't use a member function on a non-object. In this case you can't use the member function find on $element which is a string .

From the documentation of curl_exec :

Return Values

Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

The result being the html content of the page you try to get which is a STRING

Try to use var_dump and you will see by yourself.

So I guess you want to use the DOM extension and the DOMXpath class and do something like:

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.website.org');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'name');
$html = curl_exec($curl_handle);
curl_close($curl_handle);
$encodeHtml = utf8_encode(html_entity_decode($html));
$dom = new DOMDocument;
$dom->loadXML($encodeHtml);
$xpath = new DOMXPath($dom);
$res = $xpath->query($Path);

$Path being the xpath of the element you are trying to echo.

  • Please try to understand the very basics of Object-Orientation.
  • You'll have to instantiate a simple_html_dom object to call methods on it.
  • Please replace $html = curl_exec($curl_handle); with $result=curl_exec($curl_handle); $html = new simple_html_dom(); $html->load($result); $result=curl_exec($curl_handle); $html = new simple_html_dom(); $html->load($result);

and move include_once("simple_html_dom.php"); above this part.

<?php

    $curl_handle=curl_init();
    curl_setopt($curl_handle, CURLOPT_URL,'http://www.google.com');
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'name');
$html = curl_exec($curl_handle);
curl_close($curl_handle);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$mytag = $doc->getElementsByTagName('div');
foreach ($mytag  as $value) {
    //echo $value->nodeValue, PHP_EOL;
    //echo  $value->getAttribute('class'), PHP_EOL;
    if($value->getAttribute('class') == "someclass"){
        //do something 
    }

?>

http://php.net/manual/en/domdocument.getelementsbytagname.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM