Latest tweet

Speaking about the @Apple ecosystem: what about the idea to enable the hotspot on the Watch? It could be very usefu… https://t.co/eNx91zUu8P

twitted on Dec 12, 2018 at 03:07

Latest photos
A simple PHP class to get photos from Instagram

I publish a very simple PHP class to quickly get photos from your Instagram profile. 

The class works with the new Instagram API and uses curl to perform images fetching.

What are we talking about?

In this post I will illustrate how to integrate the latest n photos published on Instagram profile on own website or blog using a (very) simple PHP 7 class. 

To use this class the only library required is cURL.


Instagram Access Token

Since Instagram API doesn't work with Client ID anymore and every API connection requires now Access Token, you can get it using this tool.


Instagram class

Below there is the class that should be included into your sources. Remember to set the private variables $AccessToken and $Username according to your necessities. 

class Instagram {

    private $AccessToken = ""; // ACCESS TOKEN
    private $Username = "";    // USERNAME

    private function Connection( $api_url ){
    	$connection_c = curl_init();
    	curl_setopt( $connection_c, CURLOPT_URL, $api_url );
    	curl_setopt( $connection_c, CURLOPT_RETURNTRANSFER, 1 );
    	curl_setopt( $connection_c, CURLOPT_TIMEOUT, 5 ); // complete within 5 secs.
    	$json_return = curl_exec( $connection_c );
    	curl_close( $connection_c );
    	return json_decode( $json_return );
    }

    private function GetInstagramUserID() {
        $GetIdUrl = "https://api.instagram.com/v1/users/search?q=" . $this--->Username . "&access_token=" . $this->AccessToken;
        $Conn = $this->Connection($GetIdUrl);
        $ID = $Conn->data[0]->id;
        return $ID;
    }

    public function GetInstagramPhotos($NumberOfPhotos = NULL) {
        $ID = $this->GetInstagramUserID();
        $MediaURL = "https://api.instagram.com/v1/users/" . $ID . "/media/recent?access_token=" . $this->AccessToken;
        $Conn = $this->Connection($MediaURL);
        $Images = array();
        //print_r($Conn->data);
        $i = 0; foreach($Conn->data as $Img) {
            $Images[$i]["link"] = $Img->link;
            $Images[$i]["text"] = $Img->caption->text;
            $Images[$i]["thumb"] = $Img->images->thumbnail->url;
            $Images[$i]["img"] = $Img->images->standard_resolution->url;
            $i++;
        }

        if(isset($NumberOfPhotos)) {
            if(is_numeric($NumberOfPhotos)) {
                if($NumberOfPhotos <= $i) {
                    return array_splice($Images, 0, $NumberOfPhotos);
                } else {
                    return $Images;
                }
            }
        } else {
            return $Images;
        }
    }
}

How to use the class?

A common usage of the class in your php script could be the following:

require_once("path_to/instagram.class.php");
$IG = new Instagram;
$Photos = $IG->GetInstagramPhotos(4); // <-- Get the latest 4 photos

In this way you will get an array containing the latest four photos.

 

More precisely, you will obtain an array containing per each photo: a link to the post on Instagram, the caption of the photo, the url of the thumbnail, the url of the photo in standard resolution.


The argument of the GetInstagramPhotos() function specifies the number of pics to catch from Instagram. If nothing is passed to the function, it will return all photos of your profile. Remember that Instagram limits the number of catchable objects to 20 until you approve your application.

<