V
V
Vladimir2020-02-11 13:23:29
PHP
Vladimir, 2020-02-11 13:23:29

How to parse CURL json array from Yandex.Direct API?

The issue of lack of knowledge. Director himself, freelance.
You need a temporary crutch solution for the withdrawal of the balance of Direct accounts.
I want to convert api output to html. The problem with parsing the array, I can not figure out how.

<?php
$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,'https://api.direct.yandex.ru/live/v4/json/');
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS,'{
     "method": "AccountManagement",
     "token": "AgAAAAA8-9-uAAVNXerAXgmXDkXquGRgLPMjN30",
     "param": {"SelectionCriteria": {}, "Action": "Get"}}');  //Post Fields
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $headers = [
    'POST /json/v5/ads/ HTTP/1.1',
    'Host: api.direct.yandex.com',
    'Authorization: Bearer AQAAAAAW-**********************************',
    'Accept-Language: ru',
    'Client-Login:  *************@yandex.ru',
    'Content-Type: application/json; charset=utf-8',
  ];
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  $server_output = curl_exec ($ch);
  curl_close ($ch);

$balance = json_decode($server_output,true);

print_r ($balance);
echo $balance['Amount'];
echo $balance['AccountID'];
echo $balance['data']['AccountID'];
echo $balance['0']['AccountID'];


Displays
Array ( [data] => Array ( [ActionsResult] => Array ( ) [Accounts] => Array ( [0] => Array ( [Login] => pl4tpass [Discount] => 0 [Currency] => RUB [AmountAvailableForTransfer] => 0 [SmsNotification] => Array ( [MoneyOutSms] => No [PausedByDayBudgetSms] => Yes [SmsTimeTo] => 21:00 [MoneyInSms] => No [SmsTimeFrom] => 09:00 ) [Amount] => 0 [AgencyName] => [EmailNotification] => Array ( [SendWarn] => [MoneyWarningValue] => 20 [PausedByDayBudget] => Yes [Email] => [email protected] ) [AccountID] => 49797112 ) ) ) )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-02-11
@codegolic

Formatting the dump allows you not to ask such "stupid" questions.
It's also much better and immediately clear the structure of the array

Array ( 
  [data] => Array (
    [ActionsResult] => Array ( )
    [Accounts] => Array (
      [0] => Array ( 
        [Login] => pl4tpass 
        [Discount] => 0 
        [Currency] => RUB 
        [AmountAvailableForTransfer] => 0 
        [SmsNotification] => Array ( 
          [MoneyOutSms] => No
          [PausedByDayBudgetSms] => Yes
          [SmsTimeTo] => 21:00
          [MoneyInSms] => No
          [SmsTimeFrom] => 09:00 
        )
        [Amount] => 0 
        [AgencyName] => 
        [EmailNotification] => Array (
          [SendWarn] =>
          [MoneyWarningValue] => 20
          [PausedByDayBudget] => Yes
          [Email] => [email protected]
        ) 
        [AccountID] => 49797112 
      ) 
    ) 
  ) 
)

And it's immediately clear how to get to the balance
$balance['data']['Accounts'][0]['Amount']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question