X
X
xenonhammer2020-02-05 16:24:33
PHP
xenonhammer, 2020-02-05 16:24:33

How to correctly set up receiving using curl when paginating?

Hello!
I get data via api, but not all, because pagination with data is installed on the donor.

An example of the resulting pagination

"meta": {
      "pagination": {
        "previous": "https://api.site.ru/category/index.php?pagination_page=1&api_key=33799217a53e5c36b072dc758c363a46&response_format=xml",
        "next": "https://api.site.ru/category/index.php?pagination_page=3&api_key=33799217a53e5c36b072dc758c363a&response_format=xml"
      }
    }



I received data, it seems, only the first page, since I did not set up pagination.

How do I do it now

<?php
// Отправляем запрос на получение категорий каталогоа в JSON
$curl_0 = curl_init("https://api.site.ru/category/?api_key=33799217a53e5c36b072dc758c363a");
$arHeaderList = array();
    $arHeaderList[] = 'Accept: application/json';
    $arHeaderList[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.78';
curl_setopt($curl_0, CURLOPT_HTTPHEADER, $arHeaderList);
curl_setopt($curl_0, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($curl_0);
curl_close($curl_0);

  if($result){
      echo "<hr/>" . "Данные от поставщика получены;" . "<br/>";
  } else { echo "<hr/>" . "Данные от поставщика не получены;" . "<br/>";
  }	
  return $result;
  
  
?>



I have thoughts on how to beat this, but my site is crashing.
I think that you can do this:
1 - Make a separate code for each pagination page, but it seems to me that this will not be correct .
2 - Try to use curl_multi_init the site crashes when I try to use the code below
spoiler

<?php
// Отправляем запрос на получение категорий каталогоа в JSON
$curl_0 = curl_init("https://api.site.ru/v1/category/?api_key=a6eae107c640e81ea44d94014a52fe80");
$curl_1 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=1&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_2 = curl_init("https://api.sitt.ru/v1/assortment/index.php?pagination_page=2&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_3 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=3&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_4 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=4&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_5 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=5&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_6 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=6&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_7 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=7&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");
$curl_8 = curl_init("https://api.site.ru/v1/assortment/index.php?pagination_page=8&api_key=a6eae107c640e81ea44d94014a52fe80&response_format=xml");

$arHeaderList = array();
  $arHeaderList[] = 'Accept: application/json';
  $arHeaderList[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 OPR/65.0.3467.78';

  curl_setopt($curl_0, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_0, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_0, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_1, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_1, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_1, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_2, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_2, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_2, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_3, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_3, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_3, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_4, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_4, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_4, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_5, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_5, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_5, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_6, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_6, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_6, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_7, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_7, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_7, CURLOPT_FOLLOWLOCATION, true);

  curl_setopt($curl_8, CURLOPT_HTTPHEADER, $arHeaderList);
  curl_setopt($curl_8, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl_8, CURLOPT_FOLLOWLOCATION, true);

$m_curl = curl_multi_init();

  curl_multi_add_handle($m_curl,$curl_0);
  curl_multi_add_handle($m_curl,$curl_1);
  curl_multi_add_handle($m_curl,$curl_2);
  curl_multi_add_handle($m_curl,$curl_3);
  curl_multi_add_handle($m_curl,$curl_4);
  curl_multi_add_handle($m_curl,$curl_5);
  curl_multi_add_handle($m_curl,$curl_6);
  curl_multi_add_handle($m_curl,$curl_7);
  curl_multi_add_handle($m_curl,$curl_8);


do {
    $result = curl_multi_exec($m_curl, $active);
    if ($active) {
        curl_multi_select($m_curl);
    }
} while ($result != -1);





curl_multi_close($m_curl);


  if($result){
      echo "<hr/>" . "Данные от поставщика получены;" . "<br/>";
  } else { echo "<hr/>" . "Данные от поставщика не получены;" . "<br/>";
  }	
  return $result;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shamanov, 2020-02-05
@SilenceOfWinter

Why fence a bunch of connections when you can just change the url in the options? about getting a list of pages - a while loop running until the server returns a 404 code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question