Skip to content

Instantly share code, notes, and snippets.

@maxparm
Created April 3, 2012 17:42
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save maxparm/2294032 to your computer and use it in GitHub Desktop.
Save maxparm/2294032 to your computer and use it in GitHub Desktop.
PHP - Request Instagram api with PHP/Curl
<?php
//Get data from instagram api
$hashtag = 'max';
//Query need client_id or access_token
$query = array(
'client_id' => '',
'count' => 3
);
$url = 'https://api.instagram.com/v1/tags/'.$hashtag.'/media/recent?'.http_build_query($query);
try {
$curl_connection = curl_init($url);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
//Data are stored in $data
$data = json_decode(curl_exec($curl_connection), true);
curl_close($curl_connection);
} catch(Exception $e) {
return $e->getMessage();
}
?>
@dan-king
Copy link

dan-king commented May 1, 2015

Thank you maxparm! This worked wonderfully!

@bnamnguyen
Copy link

Thank you maxparm! This helped me very much.

@jagroop
Copy link

jagroop commented Jan 17, 2016

Thank you Sir !

@YhorbyMatias
Copy link

this not working

@mdbauman
Copy link

@YhorbyMatias Instagram recently (as in a month before your comment) changed their API to disallow things like requesting media for a hashtag except for by certain authorized accounts. Most applications can now only request feed data from the user whose access token they're using. Replace the query URL with a valid endpoint like https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN and you'll get the expected results.

@marcelkraan
Copy link

for me it's a bit complex... do i do 3 curl $url requests to instagram? code, token, hastag? do you have a small working sample? please?

@LucasLLLS
Copy link

thank you!

@HivaEzami
Copy link

nice code - thank you

@juong89
Copy link

juong89 commented Aug 11, 2017

Thank you!!!

@chenli-dev
Copy link

How can I do if I want to login the instagram?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment