【发布时间】:2016-06-02 20:59:44
【问题描述】:
我正在尝试创建一个像 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 ?
【问题讨论】: