how to use CURL and PHP Simple HTML DOM Parser with object

前端 未结 2 1167
礼貌的吻别
礼貌的吻别 2020-12-01 15:18

Using CURL to get content from website. Getting response in object. How to convert that object in to PHP Simple HTML DOM Parser

function get_data($url) 
{
          


        
相关标签:
2条回答
  • 2020-12-01 16:02

    You're not creating the DOM correctly, you must do it like this:

    // Create a DOM object
    $dom = new simple_html_dom();
    // Load HTML from a string
    $dom->load(curl_exec($ch))
    
    print_r( $dom );
    

    Check the Manual for more details...

    Edit

    It seems that is a cURL settings problem, please refer to the documentation to configure it correctly...

    This is a function I usualy use to download some pages, feel free to adjust it to your needs:

    function dlPage($href) {
    
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_URL, $href);
        curl_setopt($curl, CURLOPT_REFERER, $href);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4");
        $str = curl_exec($curl);
        curl_close($curl);
    
        // Create a DOM object
        $dom = new simple_html_dom();
        // Load HTML from a string
        $dom->load($str);
    
        return $dom;
        }
    
    $url = 'http://www.example.com/';
    $data = dlPage($url);
    print_r($data);
    
    0 讨论(0)
  • 2020-12-01 16:08

    Curl will return a string containing the HTML right? Just use the quick start sample?

    $html = str_get_html(curl_exec($ch));
    
    0 讨论(0)
提交回复
热议问题