在我的某个网站的主页上,我显示了最近3张图片的Instagram Feed。我用以下代码实现了这个目标:
function rudr_instagram_api_curl_connect( $api_url ){
$connection_c = curl_init(); // initializing
curl_setopt( $connection_c, CURLOPT_URL, $api_url ); // API URL to connect
curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $connection_c ); // connect and get json data
curl_close( $connection_c ); // close connection
return json_decode( $json_return ); // decode and return
}
$access_token = 'access_token';
$image_return = 3;
$user_search = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$access_token);
$user_id = $user_search->data[0]->id; // or use string 'self' to get your own media
$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$access_token."&count=".$image_return);
// var_dump( $return ); // if you want to display everything the function returns
foreach ($return->data as $post) {
echo '<div class="col-sm-12 col-md-4 mb-3 text-center">
<a href="'.$post->link.'" target="_blank">
<img src="' . $post->images->standard_resolution->url . '" class="img-fluid img-thumbnail bx-shadow"/>
</a>
<p><i class="fa fa-tag" aria-hidden="true"></i> ' . $post->caption->text . '<br><i class="fa fa-heart" aria-hidden="true"></i> '.$post->likes->count.' <i class="fa fa-comment" aria-hidden="true"></i> '.$post->comments->count.'<br><i class="fa fa-clock-o" aria-hidden="true"></i> ' . date('M j, Y', $post->created_time) . '</p>
</div>';
}
最近我一直把照片上传到Instagram,我不想在网站上显示,所以我想我找到了一种只能检索带有特定标签的图像的方法。快速谷歌搜索出现了多个解决方案,但有一个解决方案与我现有的代码相似。可以在此处找到此解决方案:Fetch All Photos With A Hashtag
我在解决方案中注意到,网址中只有一个tags
变量,因此我通过添加$hashtag = "bodypiercing";
变量并将其作为$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."/users/self/media/recent?access_token=".$access_token."&count=".$image_return);
添加到网址来修改我的代码。
以下是完整代码:
function rudr_instagram_api_curl_connect( $api_url ){
$connection_c = curl_init(); // initializing
curl_setopt( $connection_c, CURLOPT_URL, $api_url ); // API URL to connect
curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
curl_setopt( $connection_c, CURLOPT_TIMEOUT, 20 );
$json_return = curl_exec( $connection_c ); // connect and get json data
curl_close( $connection_c ); // close connection
return json_decode( $json_return ); // decode and return
}
$access_token = 'access_token';
$image_return = 3;
$hashtag = "bodypiercing";
$user_search = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/self/media/recent?access_token=".$access_token);
$user_id = $user_search->data[0]->id; // or use string 'self' to get your own media
$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."/users/self/media/recent?access_token=".$access_token."&count=".$image_return);
// var_dump( $return ); // if you want to display everything the function returns
foreach ($return->data as $post) {
echo '<div class="col-sm-12 col-md-4 mb-3 text-center">
<a href="'.$post->link.'" target="_blank">
<img src="' . $post->images->standard_resolution->url . '" class="img-fluid img-thumbnail bx-shadow"/>
</a>
<p><i class="fa fa-tag" aria-hidden="true"></i> ' . $post->caption->text . '<br><i class="fa fa-heart" aria-hidden="true"></i> '.$post->likes->count.' <i class="fa fa-comment" aria-hidden="true"></i> '.$post->comments->count.'<br><i class="fa fa-clock-o" aria-hidden="true"></i> ' . date('M j, Y', $post->created_time) . '</p>
</div>';
}
然而,这导致以下结果:
Notice: Trying to get property of non-object in /home/jesse666toxik/public_html/php/index.main.php on line 42
Warning: Invalid argument supplied for foreach() in
/home/jesse666toxik/public_html/php/index.main.php on line 42
对于这个变量,我可能做错了什么呢?
var_dump
的结果:
Notice
: Undefined property: stdClass::$data in
/home/jesse666toxik/public_html/php/index.main.php
on line
37
Notice
: Trying to get property of non-object in
/home/jesse666toxik/public_html/php/index.main.php
on line
37
NULL
Notice
: Trying to get property of non-object in
/home/jesse666toxik/public_html/php/index.main.php
on line
42
Warning
: Invalid argument supplied for foreach() in
/home/jesse666toxik/public_html/php/index.main.php
on line
42
答案 0 :(得分:0)
根据您链接到的页面上的信息判断您使用了错误的网址,您已将部分主题标签网址插入原始用户网址。
引用文章使用此网址:
$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;
与往常一样,答案可以在API documentation中找到。您无法通过特定用户的主题标签进行搜索。