最近两天,我试图通过原始PHP显示来自Instagram个人资料的照片。但是我遇到了这些错误。
Notice: Trying to get property of non-object in E:\xampp7\htdocs\instagram\index.php on line 29
Warning: Invalid argument supplied for foreach() in E:\xampp7\htdocs\instagram\index.php on line 29
Run Code Online (Sandbox Code Playgroud)
我从instagram API 创建了正确的身份验证。我请求的API clients access有效(clientId, userId, accessToken)。当我用PHP失败时,我尝试使用JavaScript插件instafeedjs。现在,它运行良好。这是我的PHP代码,如下所示:
<body>
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/3550421370/media/recent/?access_token=MY_VALID_ACCESS_TOKEN_IS_HERE");
$result = json_decode($result);
foreach ($result->data as $post) {
if(empty($post->caption->text)) {
// Do Nothing
}
else { …Run Code Online (Sandbox Code Playgroud)