2017-12-14 26 views
1

私のサイトのメインページには、最新の3枚の写真のInstagramフィードが表示されます。最近、私は私が唯一取得する方法を見つけるだろう考え出しので、私は必ずしもサイトに表示したくないというのInstagramに写真をアップロードしてきた特定のハッシュタグを使って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>'; 
} 

:私は、次のコードでこれを達成しました特定のハッシュタグを持つ画像。クイックグーグル検索では、複数のソリューションがポップアップしましたが、既存のコードに似ていました。このソリューションは、ここで見つけることができます:Fetch All Photos With A Hashtag

が、私はそう、私は$hashtag = "bodypiercing";の変数を追加し、$return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."https://stackoverflow.com/users/self/media/recent?access_token=".$access_token."&count=".$image_return);としてURLに追加して自分のコードを修正するだけでURL tags変数があった溶液中で気づきました。

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."https://stackoverflow.com/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

'var_dump($ return)'のコメントを外します。何が返されていますか?あなたのコードは配列を含む 'data'パラメータを持つオブジェクトであることを期待しています。それは事実ですか? – ceejayoz

+0

あなたの '$ return'変数はユーザを返すように見えますが、api呼び出しは' $ user_search'で使われたものと同じです –

+0

@ceejayoz 'var_dump'の編集結果と含まれています。 @MacroManはもともとはチュートリアルの例でした。なぜそれがそうだったのか分かりませんが、うまくいきました。 –

答えて

0

間違ったURLを使用しているあなたにリンクしているページの情報から判断するには、あなたは、元のユーザーのURLにハッシュタグのURLの一部が挿入されています。

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

はいつものように、答えが API documentationで見つけることができます:

引用符で囲まれた資料では、このURLを使用しています。特定のユーザーからハッシュタグで検索することはできません。

+0

私は自分のイメージを取得しているので、私は自分のURLに 'users/self'を使っています。 '/ tags/hashtag'を私のURLに挿入するだけで、まだ返り値が制限されるようです。私はいくつかの作業を行い、そのURLを試してみます。 –

+0

悲しいことに、私が持っているaccess_tokenをURLが要求しています。追加すると画像は返されません。私はURLをコードではなくブラウザに直接置いています。これは '{" meta ":{" code ":400、" error_type ":" OAuthPermissionsException "、" error_message ":"このアクセストークンはこのスコープでは承認されていません。このアクセス許可を付与するには、スコープ= public_contentを指定してアプリケーションを再認証する必要があります。 –

+0

APIからのタグリクエストには3つのエンドポイントしかありません。タグオブジェクトに関する情報を取得します。最近タグ付けされたメディアのリストを取得し、名前でタグを検索します。ユーザーが制限するオプションはありません。あなたの最善の策は、あなたのウェブサイトで必要な画像に誰も使用しないハッシュタグを使用することです。 – miknik

関連する問題