问题
I'm new to PHP and I can't figure out how to deal with Instagram APIs in order to (for example) extract a list of links to the standard resolution images by recent 3 items published by the user_id 3.
Here's what I created till now:
<?php
function get_instagram($user_id,$count)
{
$user_id = '3';
$count = '3';
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
$jsonData = $json_decode((file_get_contents($url)));
$data = $jsonData->data;
$result = '<ul>';
foreach ($data as $key => $value) {
$result .= '<li><a href='.$value->link.' ><img src="'.$value->images->standard_resolution->url.'" width="70" height="70" /></a></li> ';
}
$result .= '</ul>';
return $result;
}
The result is a blank page though.. can you help me?
回答1:
You need to echo or do something with the returned data (Also you have a rouge $
in front of your json_decode function)
Try this:
<?php
function get_instagram($user_id=15203338,$count=6,$width=190,$height=190){
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count;
// Also Perhaps you should cache the results as the instagram API is slow
$cache = './'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache));
} else {
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache,json_encode($jsonData));
}
$result = '<div id="instagram">'.PHP_EOL;
foreach ($jsonData->data as $key=>$value) {
$result .= "\t".'<a class="fancybox" data-fancybox-group="gallery"
title="'.htmlentities($value->caption->text).' '.htmlentities(date("F j, Y, g:i a", $value->caption->created_time)).'"
style="padding:3px" href="'.$value->images->standard_resolution->url.'">
<img src="'.$value->images->low_resolution->url.'" alt="'.$value->caption->text.'" width="'.$width.'" height="'.$height.'" />
</a>'.PHP_EOL;
}
$result .= '</div>'.PHP_EOL;
return $result;
}
echo get_instagram();
?>
With
$value->location->name
If you want to check its empty and if it is then dont show it but also want to append a string onto it if it is set you would do something like:
$location = (!empty($value->location->name))?'@'.$value->location->name:null;
Then use $location
to echo where you want it.
回答2:
I've extended the selected answer by writing a small script that can be used a boilerplate for anyone trying this.
It's setup with all the variables and starts with basic markup. Using selectors from Instagrams API docs you can build onto its output as needed.
This is built to be used specifically with WordPress with one small tweak to the image size conditional it can be used in any PHP application.
<?php
function instagram($count = 10, $width = 640, $height = 640) {
$user_id = 123456789;
$access_token = '123456789';
$size = wp_is_mobile() ? 'low_resolution' : 'standard_resolution';
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$access_token.'&count='.$count;
$cache_location = './'.sha1($url).'.json';
$cache_time = '-1 hour';
if (file_exists($cache_location) && filemtime($cache_location) > strtotime($cache_time)) {
// If a cache file exists, and it is newer than 1 hour, use it
$jsonData = json_decode(file_get_contents($cache_location));
} else {
$jsonData = json_decode((file_get_contents($url)));
file_put_contents($cache_location,json_encode($jsonData));
}
foreach ($jsonData->data as $key=>$value) {
echo '<div>';
echo '<img src="'.$value->images->$size->url.'"/>';
echo '</div>';
}
}
Basic Usage
<?php echo instagram(); ?>
Advanced Usage
<?php echo instagram($count = 1, $width = 100, $height = 100); ?>
Github Repo
回答3:
Welp, This was working a week ago, but now, it gives me a PHP error:
"Message: Trying to get property of non-object"
on the line with the foreach statement: foreach ($jsonData->data as $key=>$value)
Turns out my JSON feed was actually corrupted, the captions to some of my instagrams were missing, so I had to add an if empty statement. I also am using a different method now, curious on your thoughts, works great!
<?php
function fetchData($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$result = fetchData("https://api.instagram.com/v1/users/USER ID HERE/media/recent/?access_token=ACCES TOKEN HERE&count=14");
$cache = './instagram.json';
if(file_exists($cache) && filemtime($cache) > time() - 60*60){
// If a cache file exists, and it is newer than 1 hour, use it
$instaData = json_decode(file_get_contents($cache));
} else {
$instaData = json_decode((file_get_contents($result)));
file_put_contents($cache,json_encode($instaData));
}
foreach ($instaData->data as $post) {
if(empty($post->caption->text)) {
// Do Nothing
}
else {
echo '<a class="instagram-unit" target="blank" href="'.$post->link.'">
<img src="'.$post->images->low_resolution->url.'" alt="'.$post->caption->text.'" width="100%" height="auto" />
<div class="instagram-desc">'.htmlentities($post->caption->text).' | '.htmlentities(date("F j, Y, g:i a", $post->caption->created_time)).'</div></a>';
}
}
?>
来源:https://stackoverflow.com/questions/10231369/dealing-with-instagram-apis-json-php