Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cURL works in CLI, but not in PHP?

This is the code I am using:

curl -k https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg

This works fine (the "-k" flag is necessary for it to work or it times out)

I then use this code in PHP:

$ch = curl_init("https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

And it times out -- I've tried a ton of variations, but $result is always false.

This is the PHP cURL information when I do phpinfo():

cURL support enabled
cURL Information 7.38.0
Age 3
Features
AsynchDNS No
Debug No
GSS-Negotiate No
IDN Yes
IPv6 Yes
Largefile Yes
NTLM Yes
SPNEGO No
SSL Yes
SSPI No
krb4 No
libz Yes
CharConv No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, pop3, pop3s, rtsp, smtp, smtps, telnet, tftp
Host x86_64-unknown-linux-gnu
SSL Version OpenSSL/1.0.1e
ZLib Version 1.2.3

Any ideas would be greatly appreciated.


UPDATE

Here is the information from curl_getinfo($ch):

array (
  'url' => 'https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg',
  'content_type' => NULL,
  'http_code' => 0,
  'header_size' => 0,
  'request_size' => 0,
  'filetime' => -1,
  'ssl_verify_result' => 1,
  'redirect_count' => 0,
  'total_time' => 59.27538100000000298450686386786401271820068359375,
  'namelookup_time' => 0.00975999999999999957867036215475309290923178195953369140625,
  'connect_time' => 0.05170500000000000095923269327613525092601776123046875,
  'pretransfer_time' => 0,
  'size_upload' => 0,
  'size_download' => 0,
  'speed_download' => 0,
  'speed_upload' => 0,
  'download_content_length' => -1,
  'upload_content_length' => -1,
  'starttransfer_time' => 0,
  'redirect_time' => 0,
  'certinfo' => 
  array (
  ),
  'primary_ip' => '65.207.240.29',
  'primary_port' => 443,
  'local_ip' => '172.24.32.132',
  'local_port' => 54461,
  'redirect_url' => '',
)

UPDATE 2

Response from curl_error:

Unknown SSL protocol error in connection to www.ashleydirect.com:443

UPDATE 3 - Solution

I wanted to clearly put the solution I came up with, thanks to @Valery Viktorovsky who pointed out they only accept TLS 1.0.

The solution, then was to add this:

// Set to TLS 1.0 (CURL_SSLVERSION_TLSv1_0)
curl_setopt($ch, CURLOPT_SSLVERSION, 4);

More information here

like image 784
Kerry Jones Avatar asked Aug 19 '15 21:08

Kerry Jones


2 Answers

Your php code is fine. Do a curl_getinfo($ch) and curl_error($ch) aftercurl_exec to see the response code returned by the server.

Update: I tested ashleydirect.com SSL certificate and it supports only TLS 1.0. So make sure your php version supports TLSv1.0.

like image 82
Valery Viktorovsky Avatar answered Oct 05 '22 17:10

Valery Viktorovsky


It works fine for me, both the command line without -k and the PHP code.

If for some reason it times out you can set a bigger timeout:

// if it times out on establishing the connection
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);   // seconds
// if it times out while waiting for the response
curl_setopt($ch, CURLOPT_TIMEOUT, 60);          // seconds

Also, the call curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false) is usually accompanied by:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

to get the complete effect.

like image 24
axiac Avatar answered Oct 05 '22 18:10

axiac