Simple way to get instagram recent images using php and instagram api, get instagram access token and get your recent images easily.

How To Get Instagram Recent Images

Using Instagram API and PHP cURL, we will get instagram recent images easily.

Register a New Client

Firstly, go to instagram developer and register a new client, like this:

instagram api register a new client

PHP cURL

Now we will use PHP cURL via Qassim_HTTP() function:

function Qassim_HTTP($method, $url, $header, $data){
    /* By Qassim Hassan, wp-time.com */

    if( $method == 1 ){
        $method_type = 1; // 1 = POST
    }else{
        $method_type = 0; // 0 = GET
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);

    if( $header !== 0 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }

    curl_setopt($curl, CURLOPT_POST, $method_type);

    if( $data !== 0 ){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $response = curl_exec($curl);
    $json = json_decode($response, true);
    curl_close($curl);

    return $json;
}

Get Instagram Access Token

Now in your “redirect uri” page file, add this code to get access token (enter client id and client secret and redirect uri):

if( isset($_GET['code']) ){
    /* By Qassim Hassan, wp-time.com */
    $data = http_build_query(
        array(
            "client_id" => "xxxx", // enter your client id.
            "client_secret" => "xxxx", // enter your client secret.
            "grant_type" => "authorization_code", // do not change it!.
            "redirect_uri" => "xxxx", // enter your redirect uri.
            "code" => $_GET['code']
        )
    );

    $url = "https://api.instagram.com/oauth/access_token";

    $result = Qassim_HTTP(1, $url, 0, $data);

    echo 'This is your access token, save it: '.$result['access_token'];

}

Now open this link in browser to get access token (enter client id and redirect uri):

https://www.instagram.com/oauth/authorize/?client_id=enter_your_client_id&redirect_uri=enter_your_redirect_uri&response_type=code

You will get access token, save it:

get instagram access token

Get Instagram Recent Images

Now we will get instagram recent images, using this code (enter access token):

/* By Qassim Hassan, wp-time.com */
$access_token = "xxxx"; // enter your access token.

$count = 5; // enter count of recent images.

$url = "https://api.instagram.com/v1/users/self/media/recent/?access_token=$access_token&count=$count";

$result = Qassim_HTTP(0, $url, 0, 0);

foreach ( $result['data'] as $image ) {
    echo '<p><img src="'.$image['images']['standard_resolution']['url'].'"></p>';
}

Download Example

You can download full code here.