簡體   English   中英

cURL可在CLI中使用,但不能在PHP中使用?

[英]cURL works in CLI, but not in PHP?

這是我正在使用的代碼:

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

這可以正常工作(“ -k”標志是工作或超時所必需的)

然后,我在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);

而且它超時了-我嘗試了很多變化,但是$result總是錯誤的

這是我執行phpinfo()時的PHP cURL信息:

啟用了cURL支持
cURL信息 7.38.0
3
特征
異步DNS
調試
GSS協商
IDN
IPv6
大文件
NTLM
SPNEGO
SSL
SSPI編號
krb4
libz
CharConv
協議 dict,file,ftp,ftps,gopher,http,https,imap,imaps,pop3,pop3s,rtsp,smtp,smtps,telnet,tftp
主機 x86_64-unknown-linux-gnu
SSL版本 OpenSSL / 1.0.1e
ZLib版本1.2.3

任何想法將不勝感激。


更新

這是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' => '',
)

更新2

來自curl_error響應:

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

更新3-解決方案

感謝@Valery Viktorovsky,我想清楚地提出我想出的解決方案,他指出他們只接受TLS 1.0。

解決方案是添加以下內容:

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

更多信息在這里

您的php代碼很好。 做一個curl_getinfo($ch)curl_error($ch)curl_exec看到服務器返回的響應代碼。

更新:我測試了ashleydirect.com SSL證書,它僅支持TLS 1.0。 因此,請確保您的php版本支持TLSv1.0。

對於沒有-k的命令行和PHP代碼,它對我來說都很好。

如果出於某種原因超時,則可以設置更大的超時時間:

// 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

同樣,調用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)通常伴隨着:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

以獲得完整的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM