Vikas Mangal
Vikas Mangal

Reputation: 831

extract user and caption from instagram image URL

I want to extract the user who has posted the photo and caption associated with the photo from the photo URL using instagram API.

For example:

The image URL is: http://instagram.com/p/rRoE9uskcD/

How can I extract user and caption from this ?

Upvotes: 2

Views: 8789

Answers (2)

JFK
JFK

Reputation: 41143

You actually only need to use Instagam's Embedding Endpoints

so this link :

http://api.instagram.com/oembed?url=http://instagr.am/p/rRoE9uskcD/

will return the info associated with such media in a json format.

You can get the info programmatically inside an ajax function like:

var url = "http://api.instagram.com/oembed?url=http://instagr.am/p/rRoE9uskcD/";
jQuery(document).ready(function ($) {
    $.ajax({
        url: url,
        dataType: "jsonp", // <== this is important
        cache: false,
        success: function (data) {
            console.log(data.author_name); // the author
            console.log(data.title); // the caption (if any)
        }
    });
});

See JSFIDDLE

Upvotes: 8

Cyclonecode
Cyclonecode

Reputation: 30031

If you have setup an Instagram client you could do like this using curl and php:

$instagram_client_secret = 'YOUR_CLIENT_SECRET';
$instagram_client_id = 'YOUR_CLIENT_ID';
$shortcode = 'rRoE9uskcD';

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_URL, "https://api.instagram.com/v1/media/shortcode/$shortcode?client_id=$instagram_client_id&client_secret=$instagram_client_secret");
$result = curl_exec($ch);
$result = json_decode($result);
if(isset($result->meta->code) && $result->meta->code == 200) {
  // print out the username and caption
  print $result->data->user->full_name;
  print $result->data->caption->text;
}
curl_close($ch);

The following example uses jQuery to achive the same thing:

$(document).ready(function() {
   $.get('https://api.instagram.com/v1/media/shortcode/rRoE9uskcD?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET', function(data) {
     alert(data.data.user.full_name + ' ' + data->data->caption->text); 
   }, 'jsonp');
});

Reference: Media Endpoints

Upvotes: 1

Related Questions