【问题标题】:How to use CURL instead of file_get_contents?如何使用 CURL 而不是 file_get_contents?
【发布时间】:2012-01-22 08:54:48
【问题描述】:

我使用file_get_contents 函数在我的特定页面上获取和显示外部链接。

在我的本地文件中一切正常,但我的服务器不支持file_get_contents 函数,所以我尝试使用 cURL 和以下代码:

function file_get_contents_curl($url) {
    $ch = curl_init();

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

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

    return $data;
}

 echo file_get_contents_curl('http://google.com');

但它返回一个空白页。怎么了?

【问题讨论】:

  • curl_error 说什么?
  • 你的编码工作正常,也许 curl 没有安装?签出 phpinfo()
  • 您没有进行错误检查,然后想知道为什么没有出现错误。那是……不明智的。
  • 我怀疑如果您的托管服务提供商禁用了 fopen 包装器(file_get_contents() 工作所需的),那么他们也没有安装 curl。如果是您自己的服务器,则在您的 PHP 配置中启用 allow_url_fopen。
  • 在 phpinfo cURL support enabled

标签: php curl file-get-contents


【解决方案1】:

试试这个:

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

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

    return $data;
}

【讨论】:

  • Curl 返回空内容.. 我怎样才能避免这种情况?
  • @151291 添加以下配置对我有用: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
【解决方案2】:

这应该可以工作

function curl_load($url){
    curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

$url = "http://www.google.com";
echo curl_load($url);

【讨论】:

  • 此代码的行为与 file_get_contents 不同。您的代码不会遵循重定向,file_get_contents 会这样做。
【解决方案3】:

//你可以试试这个。它应该可以正常工作。

function curl_tt($url){

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);

return $data;
}
echo curl_tt("https://google.com");

【讨论】:

    【解决方案4】:

    我在通过直接链接访问 Google Drive 内容时遇到了这样的问题。

    调用 file_get_contents 后返回 302 临时移动

    //Any google url. This example is fake for Google Drive direct link.
    $url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
    $html = file_get_contents($url);
    echo $html; //print none because error 302.
    

    使用下面的代码再次运行:

    //Any google url. This example is fake for Google Drive direct link.
    $url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $html = curl_exec($ch);
    curl_close($ch);
    
    echo $html;
    

    我今天测试了它,2018 年 3 月 19 日

    【讨论】:

    • curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 和 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 也让我绊倒了很长时间。很棒的收获!
    猜你喜欢
    • 2013-11-13
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多