【发布时间】:2018-03-29 17:39:04
【问题描述】:
我有一个发往身份服务器的 curl 帖子。我必须发布用户的凭据并处理返回的响应。我的问题是脚本在响应返回之前继续执行。
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $myvars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$output = array();
$output = json_decode($response,true);//this is not populated before the next line
//if username or password is incorrect
$error = $output["error"];
if(!empty($error)){
echo "output error var during login";
echo var_dump($output);// NULL every time
}
代码没有等待 curl 响应,所以当我在“if”测试中检查 $error 时,每次它都是 NULL,即使我输入了错误的密码。我有一个检查代码的单元测试,服务器正在返回响应。
$output 变量最终会被填充,但我读到 curl 会阻塞线程,那么为什么
【问题讨论】:
-
你能提供一个 var_dump($output) 吗?
-
$output 的 var_dump 为 NULL。
-
你有没有试过在
$error = $output["error"];声明之前得到一个var_dump($output) -
在
curl_exec($ch)之后使用curl_error($ch)来检查卷曲错误