Reputation: 9
I am trying to create a website like Instagram where user's media are retrieved according to a specific hashtag, so every user has his/her own web page.
according to new Instagram API I'm struggling to request media using access_tokens , I have this code below but it is not working at all, logically it is correct
// function to print out images
function printImages($userID){
$url = 'https://api.instagram.com/v1/users/'.$userID.'/media/recent/? access_token='.$_GET['code'].'&count=5';
$instagramInfo = connectToInstagram($url);
$results = json_decode($instagramInfo, true);
//parse through the images one by one
foreach($results['data'] as $items){
$image_url = $items['images']['low_resolution']['url']; // goining through all result and give back url of picture to save it on the php serve.
echo '<img src" '. $image_url .' "/><br/>';
}
and here is my full code in case
<?php
//config for user PHP server
set_time_limit(0);
ini_set('default_socket_timeout', 300);
session_start();
//Make constraints using define.
define('clientID', 'my client id I have removed it');
define('clientSecret', 'my secret I have removed it');
define('redirectURI', 'my url removed too for the question');
define('ImageDirectory', 'pics/');
// function that is going to connect to instagram.
function connectToInstagram($url){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => 2,
));
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
if (isset($_GET['code'])){
$code = ($_GET['code']);
$url = 'https://api.instagram.com/oauth/access_token';
$access_token_settings = array ('client_id' => clientID,
'client_secret' => clientSecret,
'grant_type' => 'authorization_code',
'redirect_uri' => redirectURI,
'code' => $code
);
//function to get userID cause username doesn't allow uss to get pictures
function getUserID($userName){
$url = 'https://api.instagram.com/v1/users/search?q='.$userName.'&access_token='.$_GET['code'];
$instagramInfo = connectToInstagram($url);
$results = json_decode($instagramInfo, true);
echo $results['data']['0']['id'];
}
// function to print out images
function printImages($userID){
$url = 'https://api.instagram.com/v1/users/'.$userID.'/media/recent/?access_token='.$_GET['code'].'&count=5';
$instagramInfo = connectToInstagram($url);
$results = json_decode($instagramInfo, true);
//parse through the images one by one
foreach($results['data'] as $items){
$image_url = $items['images']['low_resolution']['url']; // goining through all result and give back url of picture to save it on the php serve.
echo '<img src" '. $image_url .' "/><br/>';
}
}
//cURL is what we use in PHP , it's a library calls to other API's.
$curl = curl_init($url); //setting a cURL session and we put in $url because that's where we are getting the data from
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $access_token_settings); // setting the POSTFIELDS to the array setup that we created above.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // setting it equal to 1 because we are getting strings back
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // but in live work-production we want to set this to true for more security
$result = curl_exec($curl);
curl_close();
$results = json_decode($result, true);
$use
could anyone tell me what should I do to use access_tokens for every user using my app ?
Upvotes: 0
Views: 521
Reputation: 51
Have you approved permission of your app from instagram? Instagram has changed their api policy on 2016/6/1.
Upvotes: 0