Images with PHP

Asked

Viewed 77 times

-2

Hello guys I’m working with a band’s website and I need to make your latest instagram photos appear in a certain area of their site, could you give me some idea of how to do this? I cannot incorporate them, this synchronization has to be done automatically

  • There are several libraries on the internet that do this, all easy and simple

  • Could you tell me more about that?

  • These are snippets of code that you use the instagram api to capture the images of a specific profile.

  • What is your real need? Because you can easily get this result with the Instagram documentation https://www.instagram.com/developer/

  • You can do it with json, so you can capture the images.

1 answer

2

Unofficial:

Any Facebook website has the parameter ?__a=1, this is very curious and do not ask me why it exists, but it exists. Many resources using this parameter returns everything in JSON, or close to a JSON, for example, if you use:

https://www.facebook.com/profile.php?id=4&__a=1

You will return the profile link.

If you use:

https://www.facebook.com/settings/applications/typeahead?__a=1

You will get the list of all the applications that the connected user has installed, in JSON.


With Instagram is no different, if you do:

https://www.instagram.com/instagram/?__a=1

You’ll get all results in JSON, including the latest posts, which is what you want, as well as the URL, the amount of likes. Finally I recommend that you access the URL above, which is the profile of "Instagram", and see what is returned.

Using this URL it is then possible to use CURL to get the information, for example to simplify using the function:

function getInstagramContent(string $usuario, bool $isSeguro = true){

    $ch = curl_init('https://www.instagram.com/'.$usuario.'/?__a=1');

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_TIMEOUT_MS => 1500,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_MAXREDIRS => 2,
        CURLOPT_SSL_VERIFYHOST => $isSeguro ? 2 : 0,
        CURLOPT_SSL_VERIFYPEER => $isSeguro ? 1 : 0,
        CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
    ]);

    $conteudo = curl_exec($ch);
    $codigoHTTP = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $codigoHTTP === 200 && $conteudo != '' ? json_decode($conteudo, true) : false;

}

This way you can consume the function as follows:

if($conteudoInstagram = getInstagramContent('inkeliz', false)) {

    if($conteudoInstagram['user']['is_private'] === false) {

        foreach ($conteudoInstagram['user']['media']['nodes'] as $indice => $conteudoFoto) {

            $HTML = '<a href="https://www.instagram.com/p/' . $conteudoFoto['code'] . '">
                    <img src="' . $conteudoFoto['thumbnail_src'] . '">
                </a>';

            echo $HTML;

        }

    }

}

This is not official and is not even documented by Instagram!


Explanations:

getInstagramContent(string $usuario, bool $isSeguro = true)

The $usuario sets the Instagram name as the second argument ($isSeguro) sets whether or not to check SSL, by default will check, including this is the standard of PHP 7.1.

The function will return a JSON (array format) or return false if something goes wrong.

CURLOPT_RETURNTRANSFER => 1

Allows CURL to receive results.

CURLOPT_TIMEOUT_MS => 1500,

Make the CURL be canceled if it takes more than 1500 milliseconds.

CURLOPT_FOLLOWLOCATION => 1, 

Makes the CURL follow a possible Location:.

CURLOPT_MAXREDIRS => 2, 

Sets the maximum redirects of 2.

CURLOPT_SSL_VERIFYHOST => $isSeguro ? 2 : 0,

If isSeguro for true will verify whether SSL is present in https://www.instagram.com is really from https://www.instagram.com and whether the isSeguro for false nothing is verified.

CURLOPT_SSL_VERIFYPEER => $isSeguro ? 1 : 0,

If isSeguro for truewill check if certificate of the https://www.instagram.com is valid using the CA Bundlepresent on its device, thus verifying that the certificate is signed by a trusting authority. On condition of isSeguro was false nothing is verified.

CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4

Make CURL use only IPv4.


I do not consider it a good question, but I already had the answer, since I developed this for the blog a long time ago. The only change was to make it a function and few changes were made.

You can make a CURL from the $conteudoFoto['thumbnail_src'] in order to download the image and store it on local disk, thus removing the need to upload the image through the Instagram link.


Bailiff:

Use the Instagram API on https://www.instagram.com/developer/, so create an app and in "Sandbox" add the account you want to use.

  1. Log in to your Instagram account and then log in: https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token.

  2. Get the access_token.

Now just use the CURL to consume the https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN, the ACCESS-TOKEN must be the TOKEN who previously obtained.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.