Save any resources from Instagram
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
engine
tests
.gitignore
.travis.yml
LICENSE
Readme.md
composer.json
phpunit.xml

Readme.md

License Total Downloads Build Status Latest Stable Version

InstaSave 🤘 📷 💻

You can fetch all resources from instagram and download them with your program. At least PHP 7.0 is required.

InstaSave support public feeds, it also support playlist, IGTV and HQ users profile pictures as well.

#Documentation

#Installation

InstaSave uses Composer to make things easy. Composer is a dependency management tool for PHP that allows you to declare the dependencies your project needs and installs them into your project.

#Basic Setup

Learn to use composer and run this command line:

composer require alshf/instasave

#How to use

Make sure you have composer's autoload file included

require 'vendor/autoload.php';

#Example

use InstaSave\URL\URL;
use Instasave\InstaSave;

try {
    // First we create URL object, This object do Validation check on URL.
    // You can also access URL with $absoluteUrl property.
    $url = new URL('https://www.instagram.com/p/BoaOrTsBIvm/');

    // Create an InstaSave object, InstaSave use injected URL and pass it to the Client and send GET request
    // to the instagram website and fetch a JSON as a response, then it will pass it to the EntityCollector to 
    // detect instagram feed type and suitable response to parse.
    $instaSave = new InstaSave($url);

    // Parse response to the suitable Entity in InstaSave\Response\Entity.
    $response = $instaSave->fetch();
} catch (Exception $e) {
    echo $e->getMessage();
}

#Response

After you get the Response by calling fetch() method on InstaSave object, it will bring you the Response Entity which it is instance of Entity (Playlist, Feed, Igtv, User) in Instasave\Response\Entity folder.

These response has a type property that you can compare it with InstaSave\Enumeration\Entity.

use InstaSave\Enumeration\Entity;

// Check Response type
$response->type === Entity::Feed;
$response->type === Entity::Playlist;
$response->type === Entity::User;
$response->type === Entity::Igtv;

Responses has lots of properties base on their entity for example feed entity has these properties:

$response->id;
$response->type;
$response->shortcode;
$response->comments;
$response->postedAt;
$response->likes;

// Dimenstion of the Entity which contains with & height
$response->dimensions;

// Owner of the Entity
$response->owner;

// Array of Resources -> Image or Video
$response->resources;

You can also check response tree in InstaSave\Response\Sample\Parsed folder.

#Owner Property

This node contains information about the owner of entity.

$response->owner->id;
$response->owner->username;
$response->owner->fullname;
$response->owner->avator;
#Dimension Property

Contains entity dimensions, note that resources property also has dimensions property.

$response->owner->id;
$response->owner->username;
$response->owner->fullname;
$response->owner->avator;
#Resources Property

Contains array of ressources (Image, Video) which blongs to the entity. This property also has a type property that you can compare it with InstaSave\Enumeration\Resource.

use InstaSave\Enumeration\Resource;

// Check Resource type
$response->resources[0]->type === Resource::Video;
$response->resources[1]->type === Resource::Image;

Other resources Property:

$response->resources[0]->id;
$response->resources[0]->type
$response->resources[0]->shortcode;
$response->resources[0]->dimensions;
$response->resources[0]->thumbnail;

// If resource type is Video then it will have these additional property
$response->resources[0]->video;
$response->resources[0]->duration;
$response->resources[0]->views;

NOTE: duration Property will be 0 on Playlist Entity.

#Exceptions

You can get URL validation Errors with URLValidationException but if you want to get request errors like connection error, Server errors like 5xx or 4xx errors you can catch them with ClientException, you can also catch Response errors with ResponseException.

  • URLValidationException: When URL string pass into InstaSave\URL\URL class if it doesn't validate it will throw exception.
  • ResponseException: When we dont have any response or error on parsing response this exception will throw.
  • ClientException: Connection Errors, 4xx, 5xx will handle with this exception this exception has previous exception which it comes from Alshf\Exceptions\FootmanRequestException.
use InstaSave\URL\URL;
use Instasave\InstaSave;
use Instasave\Exceptions\ClientException;

try {
    // Create invalid URL object
    $url = new URL('https://www.instagram.com/some404UrlThatDoesntExist/');

    // Create InstaSave object from URL
    $instaSave = new InstaSave($url);

    // Try to get Response
    $instaSave->fetch();
} catch (ClientException $e) {
    echo $e->getPrevious()->getStatusCode(); // 404
    echo $e->getPrevious()->getReasonPhrase() // Not Found
}

For more information check UnitTest file.


#Contributing

Bugs and feature request are tracked on GitHub.


#Credits

The code on which this package is principally developed and maintained by Ali Shafiee.


#License

InstaSave package is released under MIT.