使用 curl 时从 Headers 获取 HTTP 响应状态代码时出现问题
Issue getting HTTP response Status code from Headers while using curl
我正在使用 curl 从支付网关获取一些数据,网关 returns 有时是 500 状态代码,有时是 headers 中的 200 状态代码。我正在尝试使用 curl 从 headers 获取状态代码,但它显示为零(这是退出代码)而不是 200 或 500(状态代码)。
我正在使用 curl_getinfo($ch, CURLINFO_HTTP_CODE); 无法正常工作...
卷曲函数
public function global_Curl_payStat($data, $url, $try = 1)
{
//dd($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Prevents usage of a cached version of the URL
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
//Listen for status code to see if 200 or 500
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = json_decode(curl_exec($ch));
dd($statusCode);
curl_close($ch);
return $response;
}
您对 curl_getinfo()
的调用在您实际使用 curl_exec()
调用它之前进行。在你执行之前没有信息:)
$response = json_decode(curl_exec($ch));
//Listen for status code to see if 200 or 500
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
我正在使用 curl 从支付网关获取一些数据,网关 returns 有时是 500 状态代码,有时是 headers 中的 200 状态代码。我正在尝试使用 curl 从 headers 获取状态代码,但它显示为零(这是退出代码)而不是 200 或 500(状态代码)。
我正在使用 curl_getinfo($ch, CURLINFO_HTTP_CODE); 无法正常工作...
卷曲函数
public function global_Curl_payStat($data, $url, $try = 1)
{
//dd($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ($_ENV['API_ENDPOINT_NGINX_IP'] . '/' . $url));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Prevents usage of a cached version of the URL
curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
//Listen for status code to see if 200 or 500
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = json_decode(curl_exec($ch));
dd($statusCode);
curl_close($ch);
return $response;
}
您对 curl_getinfo()
的调用在您实际使用 curl_exec()
调用它之前进行。在你执行之前没有信息:)
$response = json_decode(curl_exec($ch));
//Listen for status code to see if 200 or 500
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);