dsf12123 2016-11-18 20:17
浏览 279

带有循环的PHP cURL

I am attempting to integrate a web service where the JSON response returns a URL for the next 1,000 results. What I have to do is loop the cURL to continue to process the request until the "next" variable is empty.

I am having an issue getting this done because I can not loop back up once i see the variable returned.

$url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?offset=0&limit=1000"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);                                                              
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                                    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);                                                                                                                                            

$result = curl_exec($ch);

file_put_contents(rj_array, $result, FILE_APPEND);

$server = file_get_contents(rj_array);

$json_result = json_decode($server, true);

$next = $json_result['next'];

I was thinking of doing something like:

while(!empty($next)){
//Do the curl request again with the new URL $next
}

But I can not reset the $next variable each time. Does anyone have any ideas?

  • 写回答

2条回答 默认 最新

  • dsy48837 2016-11-18 20:35
    关注

    Why not

    $base_url = "https://xxxxxxxxxxxxxx.com/process/api/transactions/from/$yesterday/to/$today/for/company/21?limit=1000";
    $next = 0;
    while( $next !== false ) {
        $url = $base_url . "&offset=" . $next;
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);                                                              
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");                                                                                                                                    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: $cookie"));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);                                                                                                                                            
    
        $result = curl_exec($ch);
    
        file_put_contents(rj_array, $result, FILE_APPEND);
    
        $server = file_get_contents(rj_array);
    
        $json_result = json_decode($server, true);
    
        $next = $json_result['next'];
        if( /*test if next is empty*/ ) {
            $next = false;
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况