尝试使用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
模块。尝试解决此问题的最佳方法是什么?
答案 0 :(得分:27)
你的卷发会超时。可能你正在尝试的网址需要30秒以上。
如果您通过浏览器运行脚本,则将set_time_limit
设置为零无限秒。
set_time_limit(0);
使用此选项CURLOPT_TIMEOUT
curl_setopt($ch, CURLOPT_TIMEOUT,500); // 500 seconds
从服务器进行无限重定向也会发生这种情况。要暂停此操作,请尝试在禁用后续位置的情况下运行脚本。
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
答案 1 :(得分:1)
有些时候,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);
可能是某些正文回复此选项如何与“卷曲错误:操作超时后...”
相关联答案 2 :(得分:0)
我经常遇到相同的问题。检查您的请求网址,如果您要在本地服务器上进行请求,例如 127.1.1 / api 或 192.168 .... ,请尝试进行更改,确保您遇到了云问题
答案 3 :(得分:0)
$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”。
答案 4 :(得分:0)
在curl请求中添加超时0,因此其无限时间设置为CURLOPT_TIMEOUT设置为0