[英]Curl error: Operation timed out
嘗試使用 Curl 時出現以下致命錯誤:
PHP Fatal error: Uncaught HTTP_Request2_MessageException:
Curl error: Operation timed out after 30000 milliseconds with 0 bytes received in
/usr/share/php/HTTP/Request2/Adapter/Curl.php on line 200
Exception trace Function Location
0 HTTP_Request2_Adapter_Curl::wrapCurlError('Resource id #12')
/usr/share/php/HTTP/Request2/Adapter/Curl.php:200
1 HTTP_Request2_Adapter_Curl->sendRequest(Object(HTTP_Request2))
/usr/share/php/HTTP/Request2.php:959< in /usr/share/php/HTTP/Request2/Adapter/Curl.php on line 172
但是,我看不出如何最好地調試它。 沒有參考我編寫的任何代碼行,只有HTTP_Request2
和Curl
模塊。 嘗試解決此問題的最佳方法是什么?
你的 curl 超時了。 可能您正在嘗試的網址需要超過 30 秒。
如果您通過瀏覽器運行腳本,則將set_time_limit
設置為零無限秒。
set_time_limit(0);
使用此選項CURLOPT_TIMEOUT
增加 curl 的操作時間限制
curl_setopt($ch, CURLOPT_TIMEOUT,500); // 500 seconds
來自服務器的無限重定向也可能發生這種情況。 要停止此操作,請嘗試在禁用跟隨位置的情況下運行腳本。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
有時在 Joomla 中出現此錯誤是因為 SESSION 或 coockie 出現了某些錯誤。 這可能是因為不正確的 HTTPd 服務器設置或因為某些在 CURL 或服務器 http 請求之前
所以 PHP 代碼如下:
curl_setopt($ch, CURLOPT_URL, $url_page);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_REFERER, $url_page);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "./cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "./cookie.txt");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if( $sc != "" ) curl_setopt($ch, CURLOPT_COOKIE, $sc);
將需要替換為 PHP 代碼
curl_setopt($ch, CURLOPT_URL, $url_page);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
//curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_REFERER, $url_page);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
//curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "./cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "./cookie.txt");
//curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); // !!!!!!!!!!!!!
//if( $sc != "" ) curl_setopt($ch, CURLOPT_COOKIE, $sc);
可能是一些正文回復此選項如何與“卷曲錯誤:操作超時后..”相關聯
我有很多時間遇到同樣的問題。 檢查您的請求 url,如果您在本地服務器上請求127.1.1/api或192.168.... ,請嘗試更改它,確保您正在訪問雲。
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "", // Server Path
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 3000, // increase this
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"email\":\"DarylBrowng@gmail.coh\",\"password\":\"markus William\",\"username\":\"Daryl Brown\",\"mobile\":\"013132131112\","msg":"No more SSRIs." }",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Postman-Token: 4867c7a3-2b3d-4e9a-9791-ed6dedb046b1",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
根據響應所需的時間,使用以秒為單位的值初始化數組索引“CURLOPT_TIMEOUT”。
在 curl 請求中添加超時 0,因此其無限時間設置為 CURLOPT_TIMEOUT 設置 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.