使用curl

时间:2018-10-31 09:08:03

标签: curl http-status-codes

在使用curl从支付网关中获取某些数据时,该网关有时返回500个状态代码,而在其他时间返回200个状态代码。我正在尝试使用curl从标头中获取状态代码,但它显示的是零(即退出代码)而不是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;
    }

1 个答案:

答案 0 :(得分:0)

在实际使用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);
相关问题