按用户ID

时间:2016-06-02 20:59:44

标签: php api instagram

我正在尝试创建一个类似Instagram的网站,根据特定的标签检索用户的媒体,因此每个用户都有自己的网页。

根据新的Instagram API我努力使用access_tokens请求媒体,我在下面有这个代码,但它根本不起作用,逻辑上它是正确的

// 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/>';
}

以下是我的完整代码

   <?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

任何人都可以告诉我如何使用我的应用程序为每个用户使用access_tokens?

1 个答案:

答案 0 :(得分:0)

您是否已批准Instagram上的应用程序许可? Instagram已经在2016/6/1改变了他们的api政策。