【问题标题】:PHP Adding Curl to function which uses file_get_contents [duplicate]PHP向使用file_get_contents的函数添加Curl [重复]
【发布时间】:2014-09-23 12:13:26
【问题描述】:

我在我的网站上启用file_get_contents 时遇到问题。有人建议我可以改用 curl。

这是我原来的函数,使用file_get_contents:

function getFile($fileQuery){
  global $user, $pass, $domain;

  return file_get_contents("https://$user:$pass@$domain:2083/".$fileQuery);

}

这是我尝试过的,使用 curl:

function getFile($fileQuery){
  global $user, $pass, $domain;

  //return file_get_contents("https://$user:$pass@$domain:2083/".$fileQuery);

  $url = "https://$user:$pass@$domain:2083/".$fileQuery;
    $ch = curl_init();    
    curl_setopt($ch, CURLOPT_URL,$url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable  

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 4000);    //timeout in seconds

    $result = curl_exec($ch);  
    echo "<pre>"; print_r($result);  
    curl_close($ch); 


}

但是,它返回一个空白页。

有什么想法吗?

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您可以通过以下方式使用 curl 代替 file_get_contents:

       $url = "https://$user:$pass@$domain:2083/".$fileQuery;
        $ch = curl_init();    
        curl_setopt($ch, CURLOPT_URL,$url);  
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable  
    
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
        curl_setopt($ch, CURLOPT_TIMEOUT, 4000);    //timeout in seconds
    
        $result = curl_exec($ch);  
        echo "<pre>"; print_r($result);  
        curl_close($ch);  
    

    【讨论】:

    • 感谢您的回复。我更新了我的问题...请参阅主要问题。尝试将此代码添加到函数中,但得到一个空白页。
    • 能否请您提供完整的网址 https://$user:$pass@$domain:2083/".$fileQuery 我的意思是 $fileQuery 的值在这里,以便我可以测试和修复你的问题。
    • 对不起,我不能在这里提供网站网址。如果您因此无法再帮助我,我理解。
    • 步骤 1. if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch);使用此代码块,这将准确打印错误是什么或第 2 步。您可以提供任何网页的虚拟 url,如果它返回,则返回数据,这意味着您的 url 有问题。
    猜你喜欢
    • 2018-09-25
    • 2016-05-24
    • 2014-10-08
    • 2012-06-19
    • 2011-06-23
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多