I'm using Facebook's graph API to retrieve some data via a cURL call in PHP and I'm receiving an Undefined index error and I don't know how to target it correctly.
cURL Request:
<?php
/* Call the cURL request to pull in Instagram images */
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "https://graph.facebook.com/v5.0/". get_option('insta_id') ."/media?fields=media_url,permalink,username,media_type,thumbnail_url&access_token=". get_option('insta_accesstoken'));
$result = curl_exec($curl);
$array = json_decode($result, true);
?>
Array mapping:
<?php
/* Loop through the array and only pull API fields */
$mediaUrls = array_map(function($entry) {
return [
'media_url' => $entry['media_url'],
'permalink' => $entry['permalink'],
'username' => $entry['username'],
'media_type' => $entry['media_type'],
'thumbnail_url' => $entry['thumbnail_url']
];
}, $array['data']);
?>
Via the Graph API, some have the thumbnail_url and some don't as shown below:
Correct answer was 'thumbnail_url' => !empty($entry['thumbnail_url']) ? $entry['thumbnail_url'] : ""
by @CD001.