php twitter API试图获取非对象位置的属性

时间:2018-08-23 07:58:00

标签: php twitter

我试图从Twitter帖子中提取位置名称,但是由于某种原因,我收到“试图获取非对象属性”错误

当我使用

$twitterPosts = json_decode($this->twitterAPI());
foreach ($twitterPosts as $twitterPost) {
  $post["location"] = ($twitterPost->place);
}

我收到以下答复:

"location": {
            "id": "8d65596349ee2e01",
            "url": "https://api.twitter.com/1.1/geo/id/8d65596349ee2e01.json",
            "place_type": "country",
            "name": "Republic of Croatia",
            "full_name": "Republic of Croatia",
            "country_code": "HR",
            "country": "Republic of Croatia",
            "contained_within": [],
            "bounding_box": {
                "type": "Polygon",
                "coordinates": [
                    [
                        [
                            13.4897243,
                            42.3776665
                        ],
                        [
                            19.4480171,
                            42.3776665
                        ],
                        [
                            19.4480171,
                            46.5549896
                        ],
                        [
                            13.4897243,
                            46.5549896
                        ]
                    ]
                ]
            },
            "attributes": {}
        },

但是我需要做的就是从中提取名称字段

当我尝试

  

$ post [“ location”] =($ twitterPost-> place-> name);

我收到一个错误“试图获取非对象的属性” 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果未添加位置,则不会返回空值...

if (isset($twitterPost->place->name)) {
                $post["location"] = $twitterPost->place->name;
            }