[英]Getting Instagram Photos With Specific Hashtag
在我的一個網站的主頁上,我顯示了3張最新圖片的Instagram提要。 我通過以下代碼實現了這一點:
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,而這些照片我並不想一定要顯示在網站上,所以我想我可以找到一種只檢索帶有特定主題標簽的圖像的方法。 在Google進行快速搜索后,彈出了多個解決方案,但其中一個與我現有的代碼相似,但很突出。 可以在這里找到該解決方案: 使用標簽獲取所有照片
我在解決方案中注意到,URL中僅包含一個tags
變量,因此我通過添加$hashtag = "bodypiercing";
變量來修改了代碼$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);
添加到URL $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
根據您所鏈接到的頁面上的信息使用錯誤的URL,可以將您將部分井號標簽URL插入原始用戶的URL中。
引用的文章使用以下網址:
$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;
與往常一樣,答案可以在API文檔中找到。 您無法通過特定用戶的主題標簽進行搜索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.