Filter and Hooks

Fetch Instagram feed without using access token

Hello visitor, if you are trying to display Instagram Feed without access token this article might be handy for you.

I have used Guzzle to send send HTTP requests using a GuzzleHttp\ClientInterface object. You can install Guzzle using composer.  To get the detail information about the Guzzle, you can follow the documentation. In the below code, please replace {USERNAME}  by your Instagram username.

require 'vendor/autoload.php';
use GuzzleHttp\Client;
$api_url = "https://www.instagram.com/{USERNAME}/?__a=1";
// Create a client
$client = new Client();
//Send a request to "https://www.instagram.com/{USERNAME}/?__a=1"
$request = $client->get($api_url,['verify' => false ]);
//The body of a response can be retrieved using the getBody method
$instaContents = json_decode($request->getBody());
$instaMedia = $instaContents->graphql->user->edge_owner_to_timeline_media->edges;
$items =[];
foreach ($instaMedia as $instaContent) :
    $nodeContents = $instaContent->node;
    $link = "https://www.instagram.com/p/".$nodeContents->shortcode."/";
    $items[] = [$link,$nodeContents->thumbnail_resources[0]->src];
endforeach;

I have indicate Instagram URL to the client, building an HTTP GET request via the $client->get() method.
Finally, to see the response from the server, I have execute the $request->getBody() method.

You can use var_dump() function to dump information about your Instagram i.e var_dump($instaContents) and use the necessary information to display in your feeds. I just need the url and image from my feeds therefore, I have used short-code to generate the url of feed.

If the https://www.instagram.com/{USERNAME}/?__a=1, doesn’t work, please follow the documentation to create Instagram App and generate access token for displaying the Instagram feed .

Views: 704

Standard