PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-循环中的cURL请求有时根本不返回任何内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

问题:

我正在使用PHP,cURL和公共API来获取json字符串.
这些json字符串的格式如下(简化后,平均大小约为50-60 kB):

{
   "data": {},
   "prevIoUs": "url",
   "next": "url"
}

尝试做的是通过检查“ next”属性获取从第一个字符串开始的所有json字符串.因此,我有一个while循环,只要有一个“ next”属性,我就获取一个URl.

问题有时是,循环随机地在结束之前停止,我无法弄清楚为什么经过多次测试.

我之所以说是随机的,是因为有时循环会持续到最后,并且不会发生任何问题.有时在N个循环后崩溃.

到目前为止,我无提取任何信息来帮助我对其进行调试.

我正在使用PHP 7.3.0,并从CLI启动我的代码.

到目前为止我尝试过的是:

检查标题

没有头返回.没事

使用curl_errno()和curl_error():

我在执行请求(curl_exec($ch))之后立即尝试了以下代码,但它从未触发.

if(curl_errno($ch)) {
   echo 'curl error ' . curl_error($ch) . PHP_EOL;
   echo 'response received from curl error :' . PHP_EOL;
   var_dump($responsE); // the json String I should get from the server.
}

检查响应是否返回null:

if(is_null($responsE))

或者如果我的json字符串有错误

if(!json_last_error() == JSON_ERROR_NONE)

尽管我认为它没有用,因为如果cURL响应为null或为空,它将永远无效.触发此代码时,json错误代码为3(JSON_ERROR_CTRL_CHAR)

有问题的代码

function apiCall($url) {
   ...
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_httpHEADER, $headers);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, truE);
   $response = curl_exec($ch);
}
$inc = 0;
$url = 'https://api.example.com/' . $id;
$jsonString = apiCall($url);

if(!is_null($jsonString)) {
file_put_contents('pathToDirectory/' . $id + $inc, $jsonString);
$nextUrl = getNextUrl($jsonString);

    while ($nextUrl) {
        $jsonString = apiCall($url . '?page=' . $nextUrl);

        if(!is_null($jsonString)) {
            $inc++;
            file_put_contents('pathToDirectory/' . $id + $inc, $jsonString);
            $nextUrl = getNextUrl($jsonString);
        }
    }
}

我期望我的代码能做什么:

不要随意停止,或者至少给我一个明确的错误代码.

解决方法:

问题是您的API可能返回空响应,格式错误的JSON甚至状态码为200的错误,您将立即停止执行.

由于您不控制API响应,因此您知道它会随机失败,并且您无权访问API服务器日志(因为您没有,对吧?);您需要在消费者中建立某种弹性.

很简单的事情(您需要对其进行调整)可能是

function apiCall( $url, $attempts = 3 ) {
    // ..., including setTing "$headers"
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_httpHEADER, $headers );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    for ( $i = 0; $i < $attempts; $i ++ ) {
        $response  = curl_exec( $ch );
        $curl_info = curl_geTinfo( $ch );

        if ( curl_errno( $ch ) ) {
            // log your error & try again
            conTinue;
        }

        // I'm only accepTing 200 as a status code. check with your API if there Could be other posssible "good" responses
        if ( $curl_info['http_code'] != 200 ) {
            // log your error & try again
            conTinue;
        }

        // everything seems fine, but the response is empty? not good.
        if ( empty( $response ) ) {
            // log your error & and try again
            conTinue;
        }

        return $response;
    }

    return null;
}

这将使您可以执行以下操作(从代码提取):

do {
    $jsonString = apiCall($url . '?page=' . $nextUrl);
    $nextUrl    = false;

    if(!is_null($jsonString)) {
        $inc++;
         file_put_contents('pathToDirectory/' . $id + $inc, $jsonString);
         $nextUrl = getNextUrl($jsonString);
    }
}
while ($nextUrl);

我没有检查API的返回是否为非空,不是连接错误,状态是否不同于’200’以及无效的JSON.

您可能还需要检查这些内容,具体取决您使用的API的脆弱程度.

大佬总结

以上是大佬教程为你收集整理的php-循环中的cURL请求有时根本不返回任何内容全部内容,希望文章能够帮你解决php-循环中的cURL请求有时根本不返回任何内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: