如何使用cURL返回“对象”,而不是字符串或html


How to return 'object', not string or html using cURL?

因为我的服务器主机禁用了

file_get_contents()

simple_html_dom.php内部,

我替换了

file_get_html()

function file_get_html_using_CuRL($url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

但是下面的代码

 $html = file_get_html_using_CuRL($url);
 $arvl2_arr = $html->find('div[class="arvl2"]');

返回错误:

 Fatal error: Call to a member function find() on a non-object in

我猜问题是因为$html不是对象?

当我使用时代码有效

$html = file_get_html($url);

有什么办法解决这个问题吗?

SimpleHTMLDOM中str_get_html()可用的函数可用于加载curl返回值。

只需相应地修改它:

function file_get_html_using_CuRL($url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    $output = str_get_html($output);
    curl_close($ch);
    return $output;
}

这将返回一个 SimpleHTMLDOM 对象,您现在可以在其中链接您喜欢的方法,如 ->find() 等,然后制定必要的逻辑。

注意:当然,首先加载 SimpleHTMLDOM 库。