ThaiKov
ThaiKov

Reputation: 175

cURL on wamp not working

I'm using a php client written by a company for accessing their web service. The client is very comprehensive and includes classes that get populated from XML responses that the web service endpoint generates.

when I run it on a proper web server, it works. But, when I use it locally with wamp it's throwing an error that no data was received.

I've already checked all around StackOverflow, I usually find answers to my questions but this one has drived me crazy!

here is the httpPost function (written as part of the php client they provide):

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);
    }

By the way, my curl extension is active and I even made a simple test to fetch google.com with success.

Upvotes: 6

Views: 2720

Answers (2)

Andrew
Andrew

Reputation: 106

Shay, you are right, the issues is because CA cert bundle is not available in Windows distribution of WAMP.

To fix the issue without disabling ssl verification (which is not secure and not recommended for production)

  1. download CA bundle. For example from official crul site: http://curl.haxx.se/ca/cacert.pem save to c:/wamp
  2. open php.ini inside your apache. in my case: c:\wamp\bin\apache\apache2.4.9\bin\php.ini
  3. set curl.cainfo="c:/wamp/cacert.pem"
  4. restart Apache

Done)

Upvotes: 1

Shay Elkayam
Shay Elkayam

Reputation: 4158

My guess is that it's related to the VERIFYPEER option you have on, usually curl on wamp isn't configured by default to support it (CA certificate bundle), So by default it'll reject all SSL certificates as unverifiable.

try to replace it with (only when testing on your localhost, you want this to be ON when using a server):

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

good luck.

EDIT:
This is taken from curl's "Details on Server SSL Certificates":

If the remote server uses a self-signed certificate, if you don't install a CA cert bundle, if the server uses a certificate signed by a CA that isn't included in the bundle you use or if the remote host is an impostor impersonating your favorite site, and you want to transfer files from this server, do one of the following:

1) Tell libcurl to not verify the peer. With libcurl you disable this with curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); With the curl command line tool, you disable this with -k/--insecure.

2) Get a CA certificate that can verify the remote server and use the proper option to point out this CA cert for verification when connecting. For libcurl hackers: curl_easy_setopt(curl, CURLOPT_CAPATH, capath); With the curl command line tool: --cacert [file]

Upvotes: 12

Related Questions