【问题标题】:Getting Instagram Photos With Specific Hashtag获取带有特定标签的 Instagram 照片
【发布时间】:2017-12-14 21:29:22
【问题描述】:

在我的一个网站的主页上,我显示了 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,所以我想我会找到一种方法来只检索带有特定主题标签的图像。在谷歌快速搜索后,弹出了多个解决方案,但其中一个与我现有的代码相似。这个解决方案可以在这里找到:Fetch All Photos With A Hashtag

我在解决方案中注意到 URL 中只有一个 tags 变量,因此我修改了我的代码,添加了一个变量 $hashtag = "bodypiercing"; 并将其作为 $return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."/users/self/media/recent?access_token=".$access_token."&amp;count=".$image_return); 添加到 URL。

这是完整的代码:

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

【问题讨论】:

  • 取消注释var_dump($return)。什么被退回?您的代码期望它是一个对象,带有一个包含数组的 data 参数。是这样吗?
  • 您的$return 变量似乎正在返回用户,api 调用与$user_search 使用的相同
  • @ceejayoz 编辑并包含var_dump 的结果。 @MacroMan 最初是教程中的一个示例。我不知道为什么会这样,但它奏效了。
  • 哎呀意识到我用工作代码发布了var_dump 的结果。固定。

标签: php curl instagram instagram-api


【解决方案1】:

根据您链接到的页面上的信息判断,您使用了错误的 url,您已将部分主题标签 url 插入到原始用户 url 中。

引用的文章使用这个网址:

$url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;

与往常一样,答案可以在API documentation 中找到。您无法通过特定用户的主题标签进行搜索。

【讨论】:

  • 好吧,我在我的 URL 中使用了users/self,因为我正在检索我自己的图像。似乎在我的 URL 中插入 /tags/hashtag 仍然应该限制返回。不过,我会做一些工作并尝试使用该 URL。
  • 遗憾的是,该 URL 要求我拥有一个 access_token。添加后它不会返回图像。我将 URL 直接放入浏览器而不是代码,这就是我得到的 {"meta": {"code": 400, "error_type": "OAuthPermissionsException", "error_message": "This request requires scope=public_content, but this access token is not authorized with this scope. The user must re-authorize your application with scope=public_content to be granted this permissions."}}
  • 来自 API 的标签请求只有 3 个端点。获取有关标签对象的信息。获取最近标记的媒体列表并按名称搜索标记。没有由用户限制的选项。你最好的选择是使用一个没有其他人会在你的网站上使用你想要的图像的标签,然后搜索它
  • 您的 access_token 没有该 API 调用所需的权限。您需要申请一个新的并批准新范围的权限
  • 我已经阅读了一些关于此权限的内容,它说不再接受申请。现在我真的很茫然。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多