Connect with us

How to Download Instagram Photo and Video using PHP (easy way)

kizinho

Published

on

Follow
NEWS: How to Download Instagram Photo and Video using PHP (easy way)  [New  Developer] » Naijacrawl
Photo: Naijacrawl
site

Hi folks, this script is really cool for those of you that want to get informations like photos and videos from Instagram, this tool is for you.

 

Assuming you are working on a project that needs to get data from Instagram just follow these steps and get it done.

 

Step 1 

Download this package using the following command 

 

composer require ayesh/instagram-download

 

Step 2 

 

Set your Instagram photo or video URLs as show below 

 

getDownloadUrl(); // Returns the download URL.
  $type = $client->getType(); // Returns "image" or "video" depending on the media type.
}
catch (\InvalidArgumentException $exception) {
  /*
   * \InvalidArgumentException exceptions will be thrown if there is a validation 
   * error in the URL. You might want to break the code flow and report the error 
   * to your form handler at this point.
   */
  $error = $exception->getMessage();
}
catch (\RuntimeException $exception) {
  /*
   * \RuntimeException exceptions will be thrown if the URL could not be 
   * fetched, parsed, or a media could not be extracted from the URL. 
   */
  $error = $exception->getMessage();
}

 

You are good to go.

 

 

Features:

 

1.Validates Instagram URL (domain validation, URL path validation).

 

2.Uses OG properties to detect the image and video URL.

 

3.Supports Instagram photos, videos, and Instagram TV videos Verbose error reporting with proper exceptions.

 

4.Full unit tests No dependencies other than PHP curl extension (which is most likely enabled by already)

 

This awesome script written by Ayesh, Visit their official github repository and follow for future updates.

 

Demo

 

Enjoy.

Download File 6.66 Kb 19
site


kizinho

I am a software developer, like meeting people and love blogging, that's why I developed naijacrawl because that's what I love doing.

Continue Reading
Click To Comment

BE First To Comment


    Leave a Reply

    Your email address will not be published. Required fields are marked *

    PHP 7.3 Released

    kizinho

    Published

    on

    NEWS: PHP 7.3 Released [New  Developer] » Naijacrawl
    Photo: Naijacrawl

    Yesterday the PHP team released PHP 7.3.0 for general availability (GA) and marked the third feature update to PHP 7.

    You can download the latest version from the official PHP downloads page.

    You can also get all the nitty-gritty details about PHP 7.3 by reading the PHP 7 changelog on the official site. While today marks the day of the stable release, you will have to wait a bit longer for the migration guide, which should be available shortly.

    If you havent read much on PHP 7.3 yet, here are the highlight features coming to PHP 7.3:


    Trailing Commas in function calls


    JSON_THROW_ON_ERROR flag for json_encode() and json_decode()


    Flexible Heredoc and Nowdoc syntax


    An is_countable() function


    list() reference assignment .

    Besides the flagship PHP 7.3 announcement, December 6th included five total PHP releases: php.net



    info and privacy Among the five releases, PHP 5.6.39 and PHP 7.0.33 are both security releases considered to be the last release in their respective branches. You should view these versions as the final releases unless an unforeseen security issue warrants another release. For the latest PHP-releated news consider following @official_php on Twitter and the PHP News Archive.

    Continue Reading

    Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js)

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js) [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl

    Upload files to Amazon S3 from the browser using pre-signed post (Laravel, Vue.js/Nuxt.js)

    This article was written by Okonkwo Buchi Flair a ccolleague of mind, read and know how to simply upload your files via Amazon S3.

    original article can be found here

    Before I begin I would love to point out that is my first post so please pardon me any errors you may find as you read along


    Recently I’ve been working on a e-learning platform which of course was going to deal with a lot of uploads and letting the server handle all that will be quite… 😫 And so I had to find an alternative which was direct uploads from the browser. I hit Google search and boy! there were tons of info but then they all made use of javascript, the ones with php used html forms. However, in this post we won’t be using html form. we’ll be making use of axios.

    This post assumes you’ve set up your s3 bucket and is fully focused on how to get your uploads working. Don’t forget your CORS configuration as well.


    First add your bucket details to your .env file and don’t forget to restart when you make changes to your .env file

    AWS_ACCESS_KEY_ID=xxxxxxV2HQFVM
    AWS_SECRET_ACCESS_KEY=xxxxxx+xxxx+xxxx+xxxxx
    AWS_DEFAULT_REGION=eu-west-2
    AWS_BUCKET=Your-Bucket-Name
    

    Next install aws sdk by running the following code

    composer require aws/aws-sdk-php-laravel
    

    Create a controller that’s going to take care of your uploads. Meanwhile the filesystem configuration file located at config/filesystem.php is already set up by default to help you access your S3 configurations in the .env file

    use Aws\S3\PostObjectV4;
    use Aws\S3\S3Client;
    
    class MyController extends Controller
    {
    public function upload(Request $request)
    {
    $filename =  $request->filename;
    $directory =  $request->directory;
    $s3 = config('filesystems.disks.s3');
    $client = new S3Client(['version' => 'latest','region' => $s3['region'],'credentials' => ['key' => $s3['key'],'secret' => $s3['secret'],
    ]]);
    $bucket = $s3['bucket'];
    $prefix = $directory . '/';
    $acl = 'public-read';
    $expires = '+10 minutes';
    $formInputs = [
     'acl' => $acl,
     'key' => $prefix . $filename,
    ];
    $options = [
     ['acl' => $acl],
     ['bucket' => $bucket],
     ['starts-with', '$key', $prefix],
    ];
    $postObject = new PostObjectV4($client, $bucket, $formInputs, $options, $expires);
    $attributes = $postObject->getFormAttributes();
    $inputs = $postObject->getFormInputs();
    return response(['attributes' => $attributes, 'inputs' => $inputs, 'url' => $attributes['action'] . '/' . $directory . '/' . $filename]);
    }
    }
    

    So let me explain what’s happening in the above code. From my frontend I am going to make a post request which would contain the name of the file (just as it would be in my s3 bucket and also the folder where it’s going to be saved since my files are going to be saved in different folders. I get my s3 configurations from the env file use s3 file configuration in the config/filesystem.php file. The rest are self explanatory I hope. The $attributes contains information like the form action which is going to look like https://your-bucket-url. Yours won’t necessary have a directory of course. The ‘url’ in my return is what I intend to save in my database once my upload is successful and it would look like this https://bucket-url/directory/filename. With this you will be able successfully generate a presigned POST.

    Next create a post route that points to the controller. Since I am making use of Lumen my route looks like this

    $router->post(‘uploads/store’, ‘[email protected]’);
    

    Next we’ll dive into the frontend part where we’ll be submitting a form with the detailed generated above.

    In my Nuxt file I created a component S3FileUploads.vue that looked like this

    I am going to attach screenshots instead for some codes.

    I have a computed property which basically returns the file extension

    computed property get file extension

    I also have a method for generating random strings

    random string generator

    I like to have a general file or files where my endpoints are. It makes it easier to make changes. So I have an endpoint which points to the controller that generates the presigned post for me.

    And then I have my upload file method. The first part of this method involved me trying to generate a unique name for my files and

    There was an issue I encountered while trying send the file to my s3 with axios. My header already contained an Authorization token which made the s3 reject the request, and I couldn’t remove the Authorization because of course I can’t access the backend if my request doesn’t contain the token. So removing it wasn’t an option. Th alternative for me was to use the fetch

    Of course there were other things I did like store the file url to the database if the upload was successful. That’s basically it. Feel free to ask any questions, I’ll try to reply them, and I’d really appreciate those claps too. Thanks for reading

    Continue Reading

    How to show a nice message to the user when an error occurs in using Guzzle with Laravel

    kizinho

    Published

    on

    DEVELOPER PROGRAMMING: How to show a nice message to the user when an error occurs in using Guzzle with Laravel [New  Developer Programming] » Naijacrawl
    Photo: Naijacrawl

    Hi Folks, have you encountered an ugly Exception when making a post, get, put, delete to external API when an error like 500 or 404 occurs. Today you are at the right place, after reading this you will be able to show a nice message to your users in case of any of those errors occurs

    When making a request to external API using guzzle to solve this problem you will need to catch the Exception by following the steps below.

    1. Prepare your request using Guzzle, I believe you know or have set it up already
    2. Make a post or get request to your external API
        public function create(Request $request) {
        $input = $request->all();
        try {
            $client = new Client();
            $headers = ['API-Key' => env('API_KEY')];
            $url = 'api/post/create';
            $response = $client->request('POST', $url, ['headers' => $headers, 'query' => $input]);
            $res = json_decode($response->getBody());
    //here will catch error to aviod ugly message to users  
        } catch (\GuzzleHttp\Exception\RequestException $res) {
            if ($res->hasResponse()) {
                $response = $res->getResponse();
                if ($response->getStatusCode() == 500) {
                    abort(500);
                } if ($response->getStatusCode() == 404) {
                    abort(404);
                }
            }
        }
    }
    


    Congrats , you have saved your users from seeing that ugly message . Share and comment

    Continue Reading

    Latest