dse55384 2019-03-30 19:49
浏览 74

如何自动循环获取所有页面并在独特的json中合并页面?

I need get json multpages API responses and merge results in an unique json.

The API GET, returns 50 collections per page.

I need:

  1. Generate curl request loop;
  2. Break Loop when last page response empty;
  3. Merge Json responses from pages into one (unique) json response;

My working PHP Code:

 $p = 1;

 $curl = curl_init();

 curl_setopt_array($curl, array(
     CURLOPT_URL => "https://demourl.com/api-v1/products/?page=' . $p . '",
     CURLOPT_RETURNTRANSFER => true,
     CURLOPT_ENCODING => "",
     CURLOPT_MAXREDIRS => 10,
     CURLOPT_TIMEOUT => 30,
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
     CURLOPT_CUSTOMREQUEST => "GET",
     CURLOPT_POSTFIELDS => "",
     CURLOPT_COOKIE => "ckAPI=lkjk09je1iiw90q882388mfvufuiag6f5n4dodr4",
     CURLOPT_HTTPHEADER => array(
         "authorization: Basic Og==",
         "content-type: application/json",
         "x-api-key: 85f8287ae41o2o3j4h56u7ub3416a1f7d06b2c5",
         "x-app-key: m1n2b3v4v4qwrty0192of9mawo0LswostOjeXoW"
     ),
 ));

 $response = curl_exec($curl);

 $err = curl_error($curl);

 curl_close($curl);

 if ($err) {
     echo "cURL Error #:" . $err;
 } else {
     header('Content-type: Application/JSON');
     $j = json_decode($response);
     echo  json_encode($j, JSON_PRETTY_PRINT);
 }

My code work fine, but this code no generates loop.

Please help me. Tks.

  • 写回答

1条回答 默认 最新

  • doutenglou6588 2019-03-30 22:54
    关注

    I found one solution, but a find other options too.

    The solution and working code, is this:

    <?php
    
    // abre curl
    $curl = curl_init();
    $result = array();
    $numberOfPages = 5;
    for($i = 1; $i < $numberOfPages; $i++) {
        $url = sprintf("https://myurl.com/api-v1/products/?page=%d", $i);
    
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_POSTFIELDS => "",
      CURLOPT_COOKIE => "ckreg=9df47237df410nknvufuiag6f5n4dodr4",
      CURLOPT_HTTPHEADER => array(
        "authorization: Basic Og==",
        "content-type: application/json",
        "x-api-key: 1l2k3j3h4a0s9d8fg8g7gh7lkjasdfh",
        "x-app-key: 4z1x2c23v4b5n6n7mqwerpy0876lkbj"
      ),
    ));
    
    
    $response = curl_exec($curl);
    
    $err = curl_error($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
    
     $j = json_decode($response, true);
    
     $result[] = $j;
    
    }
    
    
    }
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
     header('Content-type: Application/JSON');
    $result_M = json_encode($result, JSON_PRETTY_PRINT);
    print_r( $result_M ); 
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题