【问题标题】:curl function return Protocol http not supported or disabled in libcurlcurl 函数返回协议 http 在 libcurl 中不受支持或禁用
【发布时间】:2015-10-09 10:16:08
【问题描述】:

我在这里遇到了这个功能的问题:

function curl_function($uri) 
{    
    $ch = curl_init($uri);
    $timeout = 30; //set to zero for no timeout
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_USERAGENT, 'mycoreg');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    $file_contents = curl_exec($ch);
    $errornum      = curl_errno($ch);
    $info          = curl_getinfo($ch);
    $status        = (int) $info['http_code'];
    if ($errornum !== 0) {
        echo 'Error: ', curl_error($ch);
        $file_contents = NULL;
    }
    curl_close($ch);

    return $file_contents;
}

它给出的错误是

"http protocol is not supported or disabled".

请需要帮助。 谢谢。

【问题讨论】:

  • 很奇怪的错误,你确定不支持https 而不是http ???
  • 哎呀,我刚刚发现了问题,这是我的网址中http之前的一个空格。

标签: php http curl


【解决方案1】:

调试打印您的 URL 并确保它不包含无关字符。

在我的例子中,我在 php curl 中使用的 URL 被引用了。 curl 错误处理不会打印引用,因此该错误相当混乱且难以追踪。

您可以使用以下代码生成此错误:

// wrong
$uri = "'". "http://www.google.com/" . "'"; // quoted string
curl_setopt($ch, CURLOPT_URL, $url);

现在尝试使用正确的 URL 格式...

// right
$uri = "http://www.google.com/"; // correct format URL
curl_setopt($ch, CURLOPT_URL, $url);

【讨论】:

    【解决方案2】:

    像这样更新你的函数以避免奇怪的错误:)

    function curl_function($uri) {
        $uri = trim($uri); 
        // ... your code  
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-14
      • 1970-01-01
      • 2011-10-16
      • 2015-02-07
      • 2019-08-06
      • 2012-03-24
      • 2017-01-10
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多