Non-Official:
Any Facebook website has the ?__a=1
parameter, this is quite curious and do not ask me why it exists, but it does exist. Many features using this parameter return everything in JSON, or near a JSON , for example, if you use:
https://www.facebook.com/profile.php?id=4&__a=1
Will return the profile link.
If you use:
https://www.facebook.com/settings/applications/typeahead?__a=1
You will get a list of all applications that the connected user has installed in JSON.
Instagram is no different, if you do:
https://www.instagram.com/instagram/?__a=1
You will get all results in JSON, including the latest posts, which is what you want, as well as the URL, the amount of tanned. Finally I recommend that you access the above URL, which is the "Instagram" profile, and see what is returned.
Using this URL you can then 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
defines the name of Instagram while the second argument ( $isSeguro
) defines whether or not to check SSL, by default it will check even this is the PHP 7.1 default .
The function returns a JSON (in array format) or will return false
if something goes wrong.
CURLOPT_RETURNTRANSFER => 1
Allows CURL to receive results.
CURLOPT_TIMEOUT_MS => 1500,
Causes CURL to be canceled if it takes more than 1500 milliseconds.
CURLOPT_FOLLOWLOCATION => 1,
Make CURL follow a possible Location:
.
CURLOPT_MAXREDIRS => 2,
Defines the maximum of 2 redirects.
CURLOPT_SSL_VERIFYHOST => $isSeguro ? 2 : 0,
If isSeguro
is true
it will check if the SSL present in https://www.instagram.com
is actually https://www.instagram.com
and if isSeguro
is false
nothing is checked.
CURLOPT_SSL_VERIFYPEER => $isSeguro ? 1 : 0,
If isSeguro
is true
will verify that https://www.instagram.com
certificate is valid using CA Bundle
on your device, so it checks to see if the certificate is signed by a%
trustworthy authority. In the condition that isSeguro
is false
nothing is checked.
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
Make CURL use only use IPv4
.
I do not consider it a good question, though I already had the answer, since I developed this for the blog, some time ago. The only change was to make this a function and few changes were made.
You can make a CURL of $conteudoFoto['thumbnail_src']
in order to download the image and store it to local disk, thus removing the need to load the image through the Instagram link.
Official:
Use the Instagram API at link , in this way create an application and in the "Sandbox" add the account you want to use .
Log in to the Instagram account and then go to:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token
.
Get access_token
.
Now just use CURL
to consume https://api.instagram.com/v1/users/self/?access_token=ACCESS-TOKEN
, ACCESS-TOKEN
should be TOKEN
you previously obtained.