Reputation: 69
I have a very simple script which helps me to display IG images on my website and it works great, but ideally I would like that once clicked the images would link to the actual post page rather than to the image source. Is there any way to have this tweaked?
PS: I know how to get rid of the fancybox in the link - but I don't know how and where to retrieve the actual post id / link from.
Here my current code:
<?php
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 = 'my access token ';
$username = 'my username';
$user_search = rudr_instagram_api_curl_connect("https://api.instagram.com/v1/users/search?q=" . $username . "&access_token=" . $access_token);
// $user_search is an array of objects of all found users
// we need only the object of the most relevant user - $user_search->data[0]
// $user_search->data[0]->id - User ID
// $user_search->data[0]->first_name - User First name
// $user_search->data[0]->last_name - User Last name
// $user_search->data[0]->profile_picture - User Profile Picture URL
$limit = 8;
$i = 0;
// $user_search->data[0]->username - Username
$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/" . $user_id . "/media/recent?access_token=" . $access_token);
//var_dump( $return ); // if you want to display everything the function returns
foreach ($return->data as $post) {
echo '<a href="' . $post->images->standard_resolution->url . '" class="fancybox"><img src="' . $post->images->standard_resolution->url . '" /></a>';
}
?>
Upvotes: 1
Views: 3792
Reputation: 1997
Actually you're linking to $post->images->standard_resolution->url
Now, if we take a look at what the API returns (https://www.instagram.com/developer/endpoints/users/)
},
"link": "http://instagr.am/p/BWrVZ/",
"user": {
"username": "kevin",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_3_75sq_1295574122.jpg",
"id": "3"
},
"created_time": "1296710327",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/02/6ea7baea55774c5e81e7e3e1f6e791a7_7.jpg",
"width": 612,
"height": 612
}
As you can see, on the top there's link
- All you have to do is to change your href to this link instead of the image URL.
So change:
echo '<a href="' . $post->images->standard_resolution->url . '" class="fancybox"><img src="' . $post->images->standard_resolution->url . '" /></a>';
to:
echo '<a href="' . $post->link . '" class="fancybox"><img src="' . $post->images->standard_resolution->url . '" /></a>';
And it should work. Hope it helped.
Upvotes: 1