Auf der Hauptseite einer meiner Seiten zeige ich einen Instagram-Feed der 3 neuesten Bilder an. Ich habe dies mit dem folgenden Code erreicht:Instagram-Fotos mit spezifischem Hashtag erhalten
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>';
}
letzter Zeit habe ich Fotos wurden Hochladen auf Instagram, dass ich möchte nicht unbedingt auf der Website angezeigt werden, so ich dachte, ich würde einen Weg finden, um nur abrufen Bilder mit einem bestimmten Hashtag. Bei einer schnellen Google-Suche tauchten mehrere Lösungen auf, von denen eine aussah, die meinem bestehenden Code ähnelte. Diese Lösung ist hier zu finden: Fetch All Photos With A Hashtag
ich in der Lösung bemerkt, dass es einfach war eine tags
Variable in der URL so modifizierte ich meinen Code durch eine Variable $hashtag = "bodypiercing";
hinzufügen und auf die URL als $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);
hinzufügen. Hier
ist, dass die vollständige Code:
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>';
}
Jedoch ergibt sich wie folgt zusammen:
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
Was könnte ich falsch gemacht haben für diese eine Variable zu tun Dies?
Ergebnisse 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
Kommentieren Sie die 'var_dump ($ return)'. Was wird zurückgegeben? Ihr Code erwartet, dass es ein Objekt ist, mit einem 'data' Parameter, der ein Array enthält. Ist das der Fall? – ceejayoz
Ihre '$ return' -Variable scheint einen Benutzer zurückzugeben, der API-Aufruf ist derselbe wie der für '$ user_search' verwendete. –
@ceejayoz Bearbeitete und enthaltene Ergebnisse von' var_dump'. @MacroMan, das ursprünglich ein Beispiel aus einem Tutorial war. Ich bin mir nicht sicher, warum es so war, aber es hat funktioniert. –