Silverstripe:Instagram Widget - 未定义属性:stdClass :: $ data

时间:2016-05-20 04:46:59

标签: php json api instagram silverstripe

从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获得任何回报?

1 个答案:

答案 0 :(得分:1)

Instagram API返回一个json对象,其中所有媒体都存储在data属性下: https://www.instagram.com/developer/endpoints/users/#get_users_media_recent_self

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

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

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