We will create simple php Rest API wrapper to access user information through Instagram api. The Lumen is a popular php microrest framework.The Lumen framework help to create rest api or wrapper for existing rest api using php. The user must authorize client app to access information.

This lumen tutorial demonstrate, how to consume Instagram api using lumen. The Instagram is popular social sharing website.You can share image and videos using Instagram website or mobile application.We just access user information using Instagram api.

The pre-requisites are –

  • – You must have Instagram account, if not please create new one.
  • – Api token – You need to register your app within Instagram app developer section.

How to Register App with Instagram

The Instagram is providing functionality to create sandbox account for your application.You can register multiple app into single account.You can add your app with manage section of Instagram.The form would be look like below –

register-new-client-instagram

You can modify app information at any time using manage client section, After successfully registration of your app. You will get app client id.That will be use to get new authentication token for your application.The token will use to get information from Instagram using api.

How to Generate Client Token in Instagram

The Instagram API providing rest end point to generate token based on client id, Please keep in mind the client id and client token are two different thing.The below rest call use to get client token –

https://www.instagram.com/oauth/authorize/?client_id=client-app-id&redirect_uri=app-redirect-url&response_type=token

Whereas –

  • client-app-id : This is registered client app id.
  • app-redirect-url : This is the registered app redirect url which are given at the time of registration of app, You haven’t remember – Please find the app-redirect url from manage client section.

Above rest call will return app client token, that will use subsequent call to get information from Instagram. This token authorize to get information.

The above call is not working with client side and throw exception "implicit authentication" , like generating token using browser.

By security reasons Instagram disables OAuth 2.0 client-side authentication by default. If your app hasn’t server side you should go to Manage Clients Security section and unset Disable implicit OAuth option. After saving all should work well.

Get user Information using Instagram API

We will create user information using /user end points, we just need to pass client token with rest call and get user information.We will make route information into web.php file.

Make Route Entry into web.php File

$app->get('instagram_user_info', 'InstagramController@getUserInfo');

we will create InstagramController.php file into HTTP controller folder and add below method –

Create Handler Method in Lumen

var $instagramAPI = "https://api.instagram.com/v1/";
private function _client($endpoint) {
$client = new Client([
'base_uri' => $endpoint,
'timeout' => 300,
'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json"],
'http_errors' => false,
'verify' => false
]);
return $client;
}
public function getUserInfo() {
$res = array();
try {
$response = $this->getUserInfoService();
return json_encode($response);
} catch(Exception $ex) {
return 'Unable to get instagram user info';
}
}
/**
*/
public function getUserInfoService() {
$token = 'your instagram token';
$client = $this->_client($this->instagramAPI);
try {
$response = $client->get("users/self/?access_token="+$token)->getBody()->getContents();
return json_decode($response);
} catch(Exception $ex) {
Log::info($ex->getMessage());
return 'Unable to create instagram user info';
}
return 'Unable to create instagram user info';
}

I hope, You have enjoyed this article, Please share and subscribe news letter.

Genislab Technologies

NexGeneration complete end-2-end software testing & modern development operations tooling & solutions

Do you want to discuss your testing requirements with us? please don’t hesitate to hit the contact us button below, and we will get back to you at our earliest..