Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURL execution returns empty response and shows

Tags:

php

curl

I am using CURL in my project, Its working fine locally but if i use the same code its not executing, I tried to debug it. The output as follows :

Took 0 seconds to send a request to https://www.google.co.in

I used the following sample code:

$handle=curl_init('https://www.google.co.in');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);

if(!curl_errno($handle))
{
    $info = curl_getinfo($handle);

    echo '<br/> Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
else
{
    echo 'Curl error: ' . curl_error($handle);
}

echo $content;

The Problem is in server, I don't know what to enable in php.ini.

Please note that in server CURL and SSL is enabled. If anyone faced similar problems, share solutions please.

like image 825
AnNaMaLaI Avatar asked May 27 '13 08:05

AnNaMaLaI


People also ask

Why cURL returns nothing?

The error “empty reply from server” indicates that a zero-length response was received. This means no HTTP headers or content, simply a closed TCP connection with no HTTP payload is transmitted. curl: (52) Empty reply from the server is a server related issue.

Why is cURL not working?

cURL is supported by your hosting company/plan but not enabled: If cURL is supported by you hosting company but it is not enabled by default, then often you simply just need to login to your hosting dashboard, navigate to the relevant section and enable it. Done!

What does it mean empty reply from server?

The "empty reply from server" error indicates that a zero length response was received - no HTTP headers or content, simply a closed TCP connection with no HTTP payload transmitted. One common cause of this problem is attempting to make a plain HTTP request against the HTTPS (TLS / SSL) web server port.

What is cURL exec?

curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.


1 Answers

For me in this case, the problem was that I was getting a 302 redirect rather than the actual get request.

After turning on CURLOPT_VERBOSE from this answer

$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);

I got the following in the errorlog.txt:

* Connected to www.example.com (X.X.X.X) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
*        subject: OU=Domain Control Validated; OU=COMODO SSL; CN=www.example.com
*        start date: 2016-04-28 00:00:00 GMT
*        expire date: 2018-04-08 23:59:59 GMT
*        issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
*        SSL certificate verify ok.
> GET /path/to/file.php
HTTP/1.1^M
Host: www.example.com^M
Accept: */*^M
^M
< HTTP/1.1 302 Found^M
* Server nginx is not blacklisted
< Server: nginx^M
< Date: Tue, 20 Sep 2016 02:06:45 GMT^M
< Content-Type: text/html^M
< Transfer-Encoding: chunked^M
< Connection: keep-alive^M
< X-Powered-By: PHP/5.5.9-1ubuntu4.19^M
< Location: https://www.example.com/other/url/^M
< ^M
* Connection #0 to host www.example.com left intact

N.B.

  1. HTTP/1.1 302 Found (should just be a 200 response)
  2. Location: https://www.example.com/other/url/ is different from the GET request URL
like image 110
icc97 Avatar answered Sep 23 '22 07:09

icc97