php发送curl请求并等待响应 [英] php send curl request and wait for the response
本文介绍了php发送curl请求并等待响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我向服务器发送curl请求,需要几秒钟时间来处理请求并抛出响应。我相信我的PHP脚本继续,不等待,因此我基于响应的foreach循环吐出0结果。在移动和处理数据之前,我如何等待curl事务完成?
I am sending a curl request to a server that needs a few seconds to process the request and spit out a response. I believe my php script is continuing on and not waiting, therefore my foreach loop based on the response is spitting out 0 results. How can i wait for the curl transaction to complete before moving on and processing data?
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_USERPWD, "admin:password");
curl_setopt($curl, CURLOPT_URL, "http://server/r/?dst_user__substr='user'");
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($curl);
$result = json_decode($ret,true);
<<<<I need to wait until transaction is complete here>>>>
foreach ($result['data'] as $key => $value)
{
//process data from $result
}
推荐答案
curl正在阻止,这意味着:
curl is blocking, which means that:
$result = json_decode($ret,true);
foreach ($result['data'] as $key => $value)
{
//process data from $result
}
将不会执行:
$ret = curl_exec($curl);
完成。您可以使用curl_error()和使用curl_getinfo()检查HTTP响应代码来检查错误和其他问题。
is complete. You can check for errors and other issues using curl_error() and by checking the HTTP response code with curl_getinfo().
这篇关于php发送curl请求并等待响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文