Задать вопрос
@FRATEREH

Извлечение вложенного API?

Я не уверен, но мне кажется что у меня есть проблема когда я делаю $ch5 = curl_init($rty); подобный код. Потому что в $rty у меня лежит массив url, и на сколько я понимаю он выводит по факту в curl_init или Array (но по-моему нет) или массив url в чем и есть проблема. Мои мысли это сделать ряд переменных (примерно $ch5 с ++) и сделать это через мультипоточность. Но 1 я не совсем понимаю как это реализовать. 2 Мне кажется что должен быть более простой вариант

Дано:

Мой код извлечения:
$headers2 = array(
   "Accept: application/json",
   "Ocp-Apim-Subscription-Key: 88888888888888888",
);

$ch2 = curl_init("https://api.wto.org/qr/v1/notifications?locale=en&type=complete-notification&status=published");
function ex_curl_setopt($ch2, int $option2, $val2): void
{
    if (!curl_setopt($ch2, $option2, $val2)) {
        throw new \RuntimeException("curl_setopt failed: " . curl_errno($ch2) . ":" . curl_error($ch2));
    }
}
curl_setopt($ch2, CURLOPT_HTTPHEADER, $headers2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

$response2 = curl_exec($ch2);
if(curl_errno($ch2)){
    throw new \RuntimeException("curl_exec failed: ".curl_errno($ch2).": ".curl_error($ch2));
}
curl_close($ch2);



$data2 = json_decode($response2, true, 999, JSON_THROW_ON_ERROR);

$rty = [];
  foreach ($data2['data'] as $links) {
$rty[] = $links['details'];  
  }

$ch5 = curl_init($rty);
function ex_curl_setopt2($ch5, int $option5, $val5): void
{
    if (!curl_setopt($ch5, $option5, $val5)) {
        throw new \RuntimeException("curl_setopt failed: " . curl_errno($ch5) . ":" . curl_error($ch5));
    }
}
curl_setopt($ch5, CURLOPT_HTTPHEADER, $headers2);
curl_setopt($ch5, CURLOPT_RETURNTRANSFER, true);

$response5 = curl_exec($ch5);
if(curl_errno($ch5)){
    throw new \RuntimeException("curl_exec failed: ".curl_errno($ch5).": ".curl_error($ch5));
}
curl_close($ch5);

$data5 = json_decode($response5, true, 999, JSON_THROW_ON_ERROR);


В $data2 я получаю

{
  "data": [{
    "id": 1280,
    "document_symbol": "G\/MA\/QR\/N\/TUR\/2\/Add.3",
    "document_language": "English",
    "period_covered": "2020 - 2022",
    "notification_date": "16\/11\/2021",
    "reporter_member_name": "Turkey",
    "reporter_member_code": "C792",
    "hs_class_code": "H6",
    "type": "Complete",
    "details": "https:\/\/qr.wto.org\/api\/v2\/notifications\/1280"
  }, {
    "id": 1272,
    "document_symbol": "G\/MA\/QR\/N\/RUS\/5",
    "document_language": "English",
    "period_covered": "2020 - 2022",
    "notification_date": "21\/10\/2021",
    "reporter_member_name": "Russian Federation",
    "reporter_member_code": "C643",
    "hs_class_code": "H6",
    "type": "Complete",
    "details": "https:\/\/qr.wto.org\/api\/v2\/notifications\/1272"
  },...


В $rty я получаю массив значений из ключа details массива выше

А скажем по самой ссылке я должен так же получить массив (по крайне мере я его получал когда просто извлекал по ссылке без переменной)

"data": [{
    "id": 7336,
    "notification_id": 1272,
    "member_name": "Russian Federation",
    "member_code": "C643",
    "document_symbol": "G\/MA\/QR\/N\/RUS\/5",
    "qr_serial_number": 3,
    "qr_group": 961370777,
    "measures": [{
      "symbol": "CP",
      "label": "Prohibition except under defined conditions"
    }, {
      "symbol": "NAL",
      "label": "Non-automatic licensing"
    }, {
      "symbol": "NAL-X",
      "label": "Non-automatic licensing"
    }, {
      "symbol": "CP-X",
      "label": "Prohibition except under defined conditions"
    }],
    "administrative_mechanisms": "Administered by the Federal Service Supervision of Natural Resources (issuance of permits) and Ministry of Industry and Trade of the Russian Federation (issuance of licenses)",
    "national_legal_bases": "Decision of the Board of the Eurasian Economic Commission \u2116 30 of April 21; 2015; as last amended and adjusted",
    "description": "Import and export prohibition and restriction of ozone-depleting substances",
    "period_from_dt": "01\/10\/2020",
    "period_to_dt": "30\/09\/2022",
    "in_force_dt": "01\/10\/2021",
    "details": "https:\/\/qr.wto.org\/api\/v2\/qrs\/7336"
  },.....

Но пока ничего не работает.
  • Вопрос задан
  • 107 просмотров
Подписаться 1 Средний Комментировать
Помогут разобраться в теме Все курсы
  • Skillfactory
    Профессия Fullstack веб-разработчик на JavaScript и PHP
    20 месяцев
    Далее
  • Хекслет
    PHP-разработчик
    10 месяцев
    Далее
  • Нетология
    Веб-разработчик с нуля: профессия с выбором специализации
    14 месяцев
    Далее
Решения вопроса 1
Adamos
@Adamos
У вас проблема, когда вы не читаете документацию и программируете по тому, что вам кажется.
В доке на curl_init четко указан тип параметра - string, и от того, куда вы засунете свой массив, его работа не поменяется.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы
FoodSoul Калининград
от 180 000 до 250 000 ₽
IT-Spirit Москва
от 230 000 до 320 000 ₽
от 200 000 до 290 000 ₽