curl_exec() 需要参数 1,给定 sting

curl_exec() expects parameter 1, sting is given

我之前是这样用的file_get_content
if($html = @DOMDocument::loadHTML(file_get_contents($url))) {.. }

但切换到 curl 因为它更安全,但我收到错误

curl_exec() expects parameter 1, sting is given

我的代码

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $html = curl_exec($url);
    curl_close($ch);

      if($html) {

          $xpath = new DOMXPath($html);
..
..

}

您正在执行 URL 字符串而不是 curl 句柄

 $html = curl_exec($url);

改为

 $html = curl_exec($ch);

错误是说你正在给 curl_exec 字符串,给它 curl 句柄。使用下面的代码

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);

    $html = curl_exec($ch);
    curl_close($ch);

      if($html) {

          $xpath = new DOMXPath($html);
..
..

}

希望对您有所帮助