curL on wamp不工作

时间:2014-03-09 16:27:49

标签: php curl

我使用公司编写的php客户端来访问他们的网络服务。 客户端非常全面,包括从Web服务端点生成的XML响应中填充的类。

当我在适当的Web服务器上运行它时,它可以工作。但是,当我在本地使用它时,它会抛出一个没有收到数据的错误。

我已经在StackOverflow周围查了一下,我通常会找到问题的答案,但是这个让我抓狂!

这里是httpPost函数(作为他们提供的php客户端的一部分编写):

private function _httpPost(array $parameters)
    {
        $query = $this->_getParametersAsString($parameters);
        $url = parse_url ($this->_config['ServiceURL']);
        $scheme = '';

        switch ($url['scheme']) {
            case 'https':
                $scheme = 'https://';
                $port = $port === null ? 443 : $port;
                break;
            default:
                $scheme = '';
                $port = $port === null ? 80 : $port;
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $scheme . $url['host'] . $url['path']);
        curl_setopt($ch, CURLOPT_PORT, $port);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->_config['UserAgent']);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
        curl_setopt($ch, CURLOPT_HEADER, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


        if ($_config['ProxyHost'] != null && $_config['ProxyPort'] != -1)
        {
            curl_setopt($ch, CURLOPT_PROXY, $_config['ProxyHost'] . ':' . $_config['ProxyPort']);
        }
        $response = "";
        $response = curl_exec($ch);
        $response = str_replace("HTTP/1.1 100 Continue\r\n\r\n", "", $response);


        curl_close($ch);

        list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
        $other = preg_split("/\r\n|\n|\r/", $other);
        list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3);

        return array ('Status' => (int)$code, 'ResponseBody' => $responseBody);
    }

顺便说一句,我的curl扩展程序处于活动状态,我甚至做了一个简单的测试来成功获取google.com。

2 个答案:

答案 0 :(得分:8)

我的猜测是它与你所使用的VERIFYPEER选项有关,通常没有配置wamp上的curl支持它(CA证书包),所以默认情况下它会拒绝所有SSL证书为无法验证。 / p>

尝试替换它(只有在您的localhost上进行测试时,您希望在使用服务器时将其设置为ON):

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
祝你好运。

修改
这取自curl的“服务器SSL证书详细信息”:

  

如果远程服务器使用自签名证书,则不使用   如果服务器使用由a签名的证书,则安装CA证书捆绑包   未包含在您使用的捆绑包中或远程主机所在的CA.   冒名顶替你最喜欢的网站,你想转移   来自此服务器的文件,执行以下操作之一:

     

1)告诉libcurl 验证对等方。使用libcurl可以禁用它       with curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,FALSE);同       curl命令行工具,你用-k / - insecure禁用它。

     

2)获取可以验证远程服务器的CA证书,并使用正确的选项指出此CA证书以进行验证       连接。对于libcurl黑客:curl_easy_setopt(curl,       CURLOPT_CAPATH,capath);使用curl命令行工具:--cacert       [文件]

答案 1 :(得分:0)

Shay,你是对的,问题是因为在WAMP的Windows发行版中没有CA cert包。

解决问题而不禁用ssl验证(这不安全且不建议用于生产)

  1. 下载CA捆绑包。例如,从官方crul网站:http://curl.haxx.se/ca/cacert.pem保存到c:/wamp
  2. 在你的apache中打开php.ini。就我而言:c:\wamp\bin\apache\apache2.4.9\bin\php.ini
  3. 设置curl.cainfo="c:/wamp/cacert.pem"
  4. 重启Apache
  5. 完成)