There was a parser script that uploaded posts from a third-party site. Everything worked until I moved to another hosting. (PHP version is the same everywhere 7.0.) The parser is working but under the half of the posts this message pops up
Warning: Invalid argument for foreach () in /var/www/u0000000/data/www/test.ru/fars.php on line 83
And the first_name field is left blank. Although the information is on a third-party site. Tell me what could be the problem?
Part of the code with 80 lines:
function user($id){ $url = 'https://api.vk.com/method/users.get?user_ids='.$id.'&version=5.73&fields=city,photo_100&access_token=asdfasdf'; $result = file_get_contents ($url); $b = json_decode($result, true); $result = file_get_contents ($url); $b = json_decode($result, true); // Перебирает массив засовывает в переменную value. foreach($b['response'] as $value) : endforeach; $first_name = $value['first_name']; $avatar = $value['photo_100']; // Достает город. if ($value['city'] == 113) { $city = 'Москва'; }elseif ($value['city'] == 4433) { $city = 'СПБ'; }elseif ($value['city'] == 889) { $city = 'Воронеж'; } else { $city = 'Город не определен'; } //возвращает ответ из переменной $b return compact('first_name','city','avatar'); }
$value = end($b['response'])not easier? PS: look at what you return by reference, make sure that there is a valid json and it has aresponsefield, which is an array. and replace the if-elseif construct with switch or an array of values. - teranforeach. so no matter what's infirst_name. error before - teran