Pregunta

I´m currently trying to get all the URLs for my pictures from my instagram feed and print them out in a simple HTML gallery.

I have managed with "some help" to authenticate and get an accesstoken which allows me to get a JSON result containing all my pictures, but I´m totally new to how to fetch all the pictures with the help of JSON.

The code I've got so far looks like this:

<?php
session_start();
//require_once 'src/config.php';

$n_client_id = '87fdd319f8244a728a86f3692527fb15';
$n_client_secret = '040d431d4e0247a292612229446b5240';
$n_redurect_uri = 'http://ideweb2.hh.se/~lukpal12/Startsida.php';

//require_once 'src/Instagram.php';
date_default_timezone_set('UTC');

$url = "https://api.instagram.com/oauth/access_token";
$access_token_parameters = array(
    'client_id'     =>     $n_client_id,
    'client_secret'     =>     $n_client_secret,
    'grant_type'        =>     'authorization_code',
    'redirect_uri'      =>     $n_redurect_uri,
    'code'          =>     $_GET["code"]
);
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$access_token_parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);

$authObj = json_decode($result);

print_r($result);

$instagram = <<<END
<a href="https://api.instagram.com/v1/users/2912979/media/recent/?access_token={$authObj->access_token}">Get pictars!</a>
END;

echo $instagram;

echo $authObj->access_token;

?>

Since I'm completely new to JSON and how to work with it, I have no idea how the code should be written to do what I'm asking.

After some searching around and after a video or two explaining JSON I managed to get this code to do the Json_decode:

$json = file_get_contents("https://api.instagram.com/v1/users/2912979/media/recent/?access_token=2912979.87fdd31.0949d22f4a714349ae84643c5af165ef");
$data = json_decode($json);
echo $data->standard_resolution[0]->url; 

But that does not work at all. I've got the link to the JSON containing all the data here:

https://api.instagram.com/v1/users/2912979/media/recent/?access_token=2912979.87fdd31.0949d22f4a714349ae84643c5af165ef

If someone would be kind enough to help me out.

¿Fue útil?

Solución

First loop will give an array of images with all resolutions, while the second one an array with only standard_resolutions, use print_r to inspect the arrays.

$json = file_get_contents("https://api.instagram.com/v1/users/2912979/media/recent/?access_token=2912979.87fdd31.0949d22f4a714349ae84643c5af165ef");
$data = json_decode($json);

// to get the array with all resolutions
$images = array();
foreach( $data->data as $user_data ) {
    $images[] = (array) $user_data->images;
}
// print_r( $images );

// to get the array with standard resolutions
$standard = array_map( function( $item ) {
    return $item['standard_resolution']->url;
}, $images );

// print_r( $standard );

EDIT

To put these images to a gallery use foreach loop. See the PHP manual on how to use an array.

foreach( $standard as $url ) {
    echo "<img src=\"$url\">";
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top