即使有实时curl请求,我的PHP脚本仍继续运行。我正在将数组发送到外部网站以通过curl处理。
我尝试修改curl选项并在php.ini中设置max_execution_time并在脚本中设置set_max_time(0),但均未产生任何结果。
$postfielddata = array(
'action' => 'masscreate',
'type' => $type,
'fields' => $fields,
'sessiontoken' => $_POST['sessiontoken'],
);
//I expected this to pause the script until all data in the $post variable has been processed on the external website
//external website that is being curled runs fine with other large operations
$result = $this->runCurl($postfielddata);
error_log("response from api: " . print_r($result));
if (!$result)
{
//this block executes immediately, not waiting for curl to finish above
error_log("here no result so redirect error");
$this->redirect_error($type, 'Invalid file uploaded.', $data);
return;
}
//curl function that is being called
private function runCurl($postfielddata)
{
$post = array( 'data' => json_encode($postfielddata) );
$url = $this->config->item('urlToCurl');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($post));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($post) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 400);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$result = json_decode($result, true);
curl_close($ch);
return $result;
}
我希望脚本等待所有curl请求完成后再继续。我在这里可以做什么?
答案 0 :(得分:0)
正如其他人所说,您的超时时间可能很小, 并添加CURLOPT_RETURNTRANSFER也可能会有所帮助,没有它,该值将不会出现在您的变量中
添加一些错误处理也将使调试更加容易
if(curl_exec($ch) === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
json可能还会产生在当前代码中不可见的错误