简体   繁体   中英

Getting Instagram Photos With Specific Hashtag

On the main page of one of my sites I display an Instagram feed of the 3 most recent pictures. I've achieved this with the following 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;
$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>';
}

Lately I've been uploading photos to Instagram that I don't want to necessarily display on the site so I figured I'd find a way to only retrieve images with a specific hashtag. Upon a quick Google search multiple solutions popped up but one stuck out that was similar to my existing code. This solution can be found here: Fetch All Photos With A Hashtag

I noticed in the solution that there was simply a tags variable in the URL so I modified my code by adding a variable of $hashtag = "bodypiercing"; and adding it to the URL as $return = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/tags/".$hashtag."/users/self/media/recent?access_token=".$access_token."&count=".$image_return); .

Here is that full 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."/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>';
}

However this results in the following:

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

What might I have done wrong for this one variable to do this?

Results of 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

Judging by the information on the page you have linked to you are using the wrong url, you have inserted part of the hashtag url into your original users url.

Quoted article uses this url:

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

As always, the answer can be found in the API documentation . You can't search by hashtags from a specific user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM