私のサイトのメインページには、最新の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
'var_dump($ return)'のコメントを外します。何が返されていますか?あなたのコードは配列を含む 'data'パラメータを持つオブジェクトであることを期待しています。それは事実ですか? – ceejayoz
あなたの '$ return'変数はユーザを返すように見えますが、api呼び出しは' $ user_search'で使われたものと同じです –
@ceejayoz 'var_dump'の編集結果と含まれています。 @MacroManはもともとはチュートリアルの例でした。なぜそれがそうだったのか分かりませんが、うまくいきました。 –