RPeraltaJr
2/6/2019 - 5:32 AM

Instagram API

Easily fetch your or any Instagram feed without OAuth for PHP.

Documentation: https://github.com/pgrimaud/instagram-user-feed

<?php 

/*
* -------------------------------------------
* Create Instagram Table
* -------------------------------------------
*/

function create_instagram_table() {
    global $wpdb;
    $table_name = "instagram";

	$wpdb->query(
        "CREATE TABLE $table_name (
			id INT AUTO_INCREMENT PRIMARY KEY,
            username TEXT DEFAULT NULL,
			date TEXT DEFAULT NULL,
            image TEXT DEFAULT NULL,
            caption TEXT CHARACTER SET utf8mb4 NULL,
            url TEXT DEFAULT NULL
        )"
    );
 
    exit("Instagram table created");
}
if (!empty($_GET['create-instagram-table']) && is_user_logged_in()) {
    add_action('init', 'create_instagram_table');
}

/*
* -------------------------------------------
* Drop Instagram Table
* -------------------------------------------
*/

function delete_instagram_table() {
    global $wpdb;
    $table_name = "instagram";

	$wpdb->query("DROP TABLE $table_name");
 
    exit("Instagram table dropped");
}
if (!empty($_GET['delete-instagram-table']) && is_user_logged_in()) {
    add_action('init', 'delete_instagram_table');
}

/*
* -------------------------------------------
* Fetch Instagram Feed
* -------------------------------------------
*/

function get_instagram_feed() {

    // setup
    global $wpdb;
    $table_name = "instagram";

    // truncate table
    $wpdb->query("TRUNCATE TABLE $table_name");

    // clean input function
    function clean_input($data) {
        $data = trim($data); // remove whitespaces from both sides of a string
        $data = stripslashes($data); // removes backslashes
        $data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8'); // converts some predefined characters to HTML entities (ex. & to &amp;)
        return $data;
    }

    // init 
    require_once __DIR__ . '/../vendor/autoload.php';
    $api = new Instagram\Api();

    $accounts = [
        "travelcraterlake",
        "visitmesaverde",
        "visitasilomar"
    ];

    foreach($accounts as $account):

        // get username(s)
        $api->setUserName($account);

        // fetch
        $feed = $api->getFeed();
        $json = json_encode($feed); // convert data to json
        $data = json_decode($json, true);
        $data = (object) $data; // convert to object
        
        try {

            // get account username
            $username = $data->userName;

            foreach($data->medias as $media):

                $media = (object) $media; // convert to object
                $media->caption = clean_input($media->caption); // clean caption data

                // reformat date
                $date = strtotime($media->date['date']);
                $date = date('Y-m-d H:i:s', $date);

                $wpdb->insert($table_name, [
                    "username"  => $username,
                    "date"      => $date,
                    "image"     => $media->displaySrc,
                    "caption"   => $media->caption,
                    "url"       => $media->link
                ]);

            endforeach;

        } catch(Exception $e) {
            echo "Message: " . $e->getMessage();
            exit;
        }

    endforeach;

}

add_action('get_instagram_feed', 'get_instagram_feed');

if (!empty($_GET['instagram']) && is_user_logged_in()) {
    add_action('init', 'get_instagram_feed');
}

Installation of version ^5.0

composer require pgrimaud/instagram-user-feed