I want to show Instagram photos on my website, using PHP7. I have registered my app, and added the clientId and secretId from Instagram. But code below gives me the errors: data is aof non object and foreach is an invalid arguement.
<?php
// Supply a user id and an access token
$clientid = "my client id";
$accessToken = "my client secret";
// Gets our data
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Pulls and parses data.
$result = fetchData("https://api.instagram.com/v1/users/{$clientid}/media/recent/?access_token={$accessToken}");
$result = json_decode($result);
?>
<?php foreach ($result->data as $post): ?>
<!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
<a class="group" rel="group1" href="<?= $post->images->standard_resolution->url ?>"><img src="<?= $post->images->thumbnail->url ?>"></a>
<?php endforeach ?>
You can use this api for self not for any other users.
https://api.instagram.com/v1/users/self/media/recent/?access_token=ACCESS-TOKEN
Refer this - https://www.instagram.com/developer/endpoints/users/
If I'm right, you have only Client ID & Client Secret only, But you don't have Access Token.
Follow my steps
first, you have to create access_token with your Client ID & Client Secret
Visit one of the following websites to make access_token
https://elfsight.com/service/generate-instagram-access-token/
https://rudrastyh.com/tools/access-token
or you can login to the instagram account and make your access_token
after created the access_token it will be like this format :
xxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Then, add following code in your controller function
$access_token = 'xxxxxxxxxx.xxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; // your access_token will be this format
$count_feed = 2; // how many feed want to load
$requestURL = 'https://api.instagram.com/v1/users/self/media/recent?access_token='.$access_token.'&count='.$count_feed;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $requestURL,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => 1
));
$json_response = curl_exec($ch);
curl_close($ch);
$insta_feeds = json_decode($json_response, true);
_e($insta_feeds); // $insta_feeds['data'] will return feed
You can get the feed by using the Instagram library. check out the library here : https://github.com/suhindra/CodeIgniter-Instagram-API
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With