S
S
sochi-russia2016-02-08 10:05:27
PHP
sochi-russia, 2016-02-08 10:05:27

API error in getting data. Array is highlighted, how to fix it?

There is a code

<?php

$map = function($array, $from, $to){
    $result = [];
    if(!empty($array) && is_array($array)){
        foreach ($array as $element) {
            $key = $element[$from]?:null;
            $value = $element[$to]?:null;
            if($key && $value){
                $result[$key] = $value;
            }
        }
    }
    return $result;
};

$airlines = $map(json_decode(file_get_contents('http://site.com/json/airlines.json'), true), 'iata', 'name');

$cit=json_decode(file_get_contents("http://site.com/json/cities.json")); $cities=array(); foreach($cit as $val) { if(property_exists($val->name_translations,"ru")) $cities[$val->code]=$val->name_translations->ru;}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://othersite.com/v1/city-directions?origin=REN&token=xxxxxxx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Access-Token: xxxxx"));
$response = curl_exec($ch);
curl_close($ch);

$products=json_decode($response,true);




$replace_value = function($key, $val) use ($cities, $airlines){
    $response = $val;
    switch($key){case 'destination':
            $response = $cities[$val];break;
            case 'origin':
            $response = $cities[$val];break;
        case 'airline':
            $response = $airlines[$val];
            break;
    }
    return $response;
}

?>
<table class="features-table">
<thead style="background: rgba(2, 69, 141, 0.46) none repeat scroll 0% 0%; color: rgb(255, 255, 255);">
    <tr>
              <th class="views-field views-field-field-logo-fid">
        Пункт отправления     </th>
              <th class="views-field views-field-tid">
          Пункт назначения      </th>
              <th class="views-field views-field-tid-1">
  Цена, р.        </th>
              <th class="views-field views-field-field-napravlenie-value">Пересадки
                  </th>
              <th class="views-field views-field-field-cenaot-value">
          Авиакомпания       </th>
              <th class="views-field views-field-field-prodsgas-value">№ рейса</th>
              <th class="views-field views-field-field-prodsgapo-value">
         Дата вылета     </th>
              <th class="views-field views-field-field-perelets-value">Обратно</th>
              <th class="views-field views-field-field-pereletpo-value">
</th></tr>
</thead>
<?php
if(isset($products['data']) && is_array($products['data'])) {
foreach ($products['data'] as $key => $data) {
?>
<tr>
<?php
foreach ($data as $field => $val) {
if ('expires_at' == $field) {
?>
<td><a rel="nofollow" href="http://site.com/searches/new?origin_iata=REN&destination_iata=<?=$data['destination']?>&depart_date=<?=substr($data['departure_at'], 0, 10)?>&return_date=<?=substr($data['return_at'], 0, 10)?>&adults=1&children=0&infants=0&trip_class=0&marker=marker&with_request=true">Купить</a></td>
<?php
} else {
?>
<td><?php echo $replace_value($field, $val); ?></a></td>
<?php
}
}
?>
</tr>
<?php
}
}
?>
</table>

Here is the api response itself in case of a request through the address bar, the code does not work with it
{"success": true, "data": {"AAQ":{"1":{"price":15450,"airline":"SU","flight_number":5831,"departure_at":"2016-02-13T19:30:00Z","return_at":"2016-02-21T17:55:00Z","expires_at":"2016-02-10T08:36:26Z"},"2":{"price":18275,"airline":"SU","flight_number":1245,"departure_at":"2016-03-17T05:55:00Z","return_at":"2016-03-28T12:05:00Z","expires_at":"2016-02-09T09:28:27Z"},"3":{"price":21798,"airline":"SU","flight_number":1245,"departure_at":"2016-03-17T05:55:00Z","return_at":"2016-03-28T12:05:00Z","expires_at":"2016-02-09T09:28:27Z"}},"ABA":{"1":{"price":98040,"airline":"R2","flight_number":5833,"departure_at":"2016-02-10T09:00:00Z","return_at":"2016-02-15T09:55:00Z","expires_at":"2016-02-10T06:24:28Z"}},"ADD":{"2":{"price":131269,"airline":"SU","flight_number":1245,"departure_at":"2016-02-10T05:55:00Z","return_at":"2016-02-20T23:59:00Z","expires_at":"2016-02-09T15:21:53Z"}},"AER":{"1":{"price":81152,"airline":"SU","flight_number":1245,"departure_at":"2016-05-01T05:55:00Z","return_at":"2016-05-22T10:45:00Z","expires_at":"2016-02-10T17:35:28Z"}}}}

When requested via php, Array.
But with this answer the code works
{"success":true,"data":{"AAQ":{"origin":"REN","destination":"AAQ","price":15450,"transfers":1,"airline":"SU","flight_number":5831,"departure_at":"2016-02-13T19:30:00Z","return_at":"2016-02-21T17:55:00Z","expires_at":"2016-02-10T08:36:26Z"},"AER":{"origin":"REN","destination":"MOW","price":9250,"transfers":0,"airline":"FV","flight_number":6754,"departure_at":"2016-06-14T07:40:00Z","return_at":"2016-06-14T09:30:00Z","expires_at":"2016-02-10T06:09:23Z"},"SIP":{"origin":"REN","destination":"SIP","price":15450,"transfers":1,"airline":"SU","flight_number":5833,"departure_at":"2016-02-22T09:00:00Z","return_at":"2016-02-29T08:40:00Z","expires_at":"2016-02-11T04:10:37Z"},"SVX":{"origin":"REN","destination":"SVX","price":18429,"transfers":0,"airline":"O7","flight_number":85,"departure_at":"2016-02-15T07:00:00Z","return_at":"2016-02-19T15:10:00Z","expires_at":"2016-02-10T15:51:10Z"}},"error":null,"currency":"rub"}

Please tell me how to fix? Thanks in advance, best regards.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Silm, 2016-02-08
@Silm

Some API and a lot of clumsy code...
Debug the code. First, look at the response dump before starting to process it.
If there is expected data in the dump, then look for an error in subsequent processing, if there is no expected data, then there is an error in the request, look for an error in it.

P
pikls23, 2022-04-07
@pikls23

err_net_array how to fix error

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question