Home » Php » php – How to use instagram api to fetch image with certain hashtag?

php – How to use instagram api to fetch image with certain hashtag?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am learning the instagram api to fetch certain hashtag image into my website recently.

After searching on the webs for a very long time I coundnt find any workable code for it.

Anyone can help?

Thanks!

How to&Answers:

If you only need to display the images base on a tag, then there is not to include the wrapper class “instagram.class.php”. As the Media & Tag Endpoints in Instagram API do not require authentication. You can use the following curl based function to retrieve results based on your tag.

function callInstagram($url)
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2
));

$result = curl_exec($ch);
curl_close($ch);
return $result;
}

$tag = 'YOUR_TAG_HERE';
$client_id = "YOUR_CLIENT_ID";

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

$inst_stream = callInstagram($url);
$results = json_decode($inst_stream, true);

//Now parse through the $results array to display your results... 
foreach($results['data'] as $item){
    $image_link = $item['images']['low_resolution']['url'];
    echo '<img src="'.$image_link.'" />';
}

Answer:

You will need to use the API endpoint for getting a list of recently tagged media by the hashtag. It would look something like this to get media for the hashtag #superpickle

https://api.instagram.com/v1/tags/superpickle/media/recent

You will need to read the Instagram API documentation to learn more about it and how to register for a client ID. http://instagram.com/developer/

Answer:

You can use statigram cURL method, isnt Instagram API, but can resolve it.
Im use CodeIgniter and make a service to return a XML, usign simple_xml_load to read feed.
Good Lucky.

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_URL, "http://statigr.am/feed/cristiano");
$content = curl_exec($curl);
curl_close($curl);

$this->xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);

echo json_encode($this->xml->channel);