X
X
xenonhammer2020-01-13 12:49:28
PHP
xenonhammer, 2020-01-13 12:49:28

How to get processed json data by curl method in php?

The vendor gave a code example for working on api, I added checks on my own to make sure everything goes away.
But using it, I get this response:

Код от сервера: 200

Проверка заголовка: Accept: application/json
Проверка заголовка: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0

C:\wamp64\www\testsitework.local\samson.php:21:boolean false

Code that used:
<?php
// "application/json" example
$curl = curl_init("https://api.samsonopt.ru/v1/category/?api_key=*******************************************************");
$arHeaderList = array();
$arHeaderList[] = 'Accept: application/json';
$arHeaderList[] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0';

curl_setopt($curl, CURLOPT_HTTPHEADER, $arHeaderList);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($curl);
             // Ответ от сервера
 			$URL = 'https://api.samsonopt.ru/v1/category/?api_key=*******************************************************c';
      $headers = get_headers($URL);
      echo 'Код от сервера:  ' . $code = substr($headers[0], 9, 3) . "<br/>";
                  //Проверка заголовков
            foreach($arHeaderList as $arHeaderList_info)
                       {
     			      echo '<br/>' . "Проверка заголовка:  " . $arHeaderList_info;
            }
var_dump($result);
curl_close($curl);
  
?>

Sample response I should get:
[
  {
    "data": [
      {
        "id": "26203",
        "name": "Тетради 12-24 листов",
        "parent_id": "26165",
        "depth_level": "3"
      }
    ],
    "meta": {
      "pagination": {
        "previous": "https://api.samsonopt.ru/v1/category/index.php?pagination_page=1&api_key=*******************************************************&response_format=xml",
        "next": "https://api.samsonopt.ru/v1/category/index.php?pagination_page=3&api_key=*******************************************************&response_format=xml"
      }
    }
  }
]

By hammering in the url in the browser, I really get the raw json data set.
Can you tell me if there are any errors in the code?
I need to get the processed data so that I can at least add it to excel, for a start

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2020-01-13
@xenonhammer

We learn programming.
1. Having received the word false at the output, we must look in our code for the place from which this word is obtained.
We look: $result = curl_exec($curl);. It turns out that the curl_exec() function returned false.
2. to find out in which cases the curl_exec() function returns false, you need to open the documentation
3. Open the documentation and read, Returns FALSE in case of an error.
4. Well, here it is more difficult. It takes either experience or some googling to discover the curl_error() function
5. Add the output of this function to your script, and then watch what it outputs and act according to the circumstances.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question