Graph API上cURL请求的未定义索引

问题描述 投票:1回答:1

[我正在使用Facebook的图形API通过PHP中的cURL调用检索一些数据,并且收到未定义的索引错误,并且我不知道如何正确地定位它。

cURL请求

<?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);
?>

数组映射

<?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']);
?>

通过Graph API,有些具有thumbnail_url,有些则没有如下所示:enter image description here

我收到如下调试错误:enter image description here

php curl php-curl
1个回答
0
投票

正确答案是@ CD001的'thumbnail_url' => !empty($entry['thumbnail_url']) ? $entry['thumbnail_url'] : ""

© www.soinside.com 2019 - 2024. All rights reserved.