Silverstripe:Instagram 小部件 - 未定义 属性:stdClass::$data

Silverstripe: Instagram Widget - Undefined property: stdClass::$data

从 Instagram 小部件中获取此错误:

[Notice] Undefined property: stdClass::$data 
GET /

Line 146 in /massi/widget_imagegallery/ImageGalleryWidget.php

这是 Widget.php 代码。第 146 行是 "foreach ($json->data ....":

$numberOfImages = $this->MaxImageCount;
            if (!is_numeric($numberOfImages) || $numberOfImages === 0) {
                $numberOfImages = 500;
            }

            $service = new RestfulService(sprintf('https://api.instagram.com/v1/users/%s/media/recent/?access_token=%s&count=%d', $this->InstagramUserID, $this->InstagramAccessToken, $numberOfImages));
            $xml = $service->request()->getBody();
            $json = json_decode($xml);

            $arrayList = arrayList::create();

            $current = 1;
            if ($json) {
                $index = 0;
                foreach ($json->data as $listItem) {
                    if ($current <= $numberOfImages) {

                        $data = array(
                            'ImageNormal'      => $listItem->images->standard_resolution->url,
                            'Shape'            => $this->ImageShape( $counter ),
                            'ExtraClasses'     => $this->ImageExtraClasses( $counter ),
                            'ImageGalleryType' => $this->ImageGalleryType,
                            'DataSource' => $this->DataSource,
                            'Pos'              => $index,
                            'Title'            => $listItem->caption->text
                        );


                        $imageGalleryItemsList->push($data);

                    }
                    $counter ++;
                    $index ++;
                }
            }

我在设置中获得了正确的访问令牌和用户 ID。似乎代码在请求数据时没有从 Instagram 返回任何信息?

Instagram API returns 一个 json 对象,所有媒体都存储在数据 属性 下: https://www.instagram.com/developer/endpoints/users/#get_users_media_recent_self

这解释了 foreach ($json->data as $listItem)

因此,如果 data 属性 不存在,则可能意味着 api 返回了错误或者没有 data.

转储 json 数据,你就会知道哪里出了问题。