I can't remember :/
Re: json_decode and arrays
« Reply #30, on June 3rd, 2013, 09:36 PM »
I can't remember :/
"image":[
{
"#text":"http:\/\/userserve-ak.last.fm\/serve\/34s\/3996573.jpg",
"size":"small"
},
{
"#text":"http:\/\/userserve-ak.last.fm\/serve\/64s\/3996573.jpg",
"size":"medium"
},
{
"#text":"http:\/\/userserve-ak.last.fm\/serve\/126\/3996573.jpg",
"size":"large"
},
{
"#text":"http:\/\/userserve-ak.last.fm\/serve\/300x300\/3996573.jpg",
"size":"extralarge"
}
] if ($track['image']['size'] == medium) {
$thumb = $track['image']['#text'];
}$image = null;
foreach ($track['image'] as $i) {
if ($i['size'] == 'medium') {
$image = $i['#text'];
break;
}
}
$image = array_reduce($track['image'], function ($image, array $i) { return $image ?: ($i['size'] == 'medium' ? $i['#text'] : null); });$image = array_filter($track['image'], function ($image) { return $image['size'] == 'medium'; });
$image = isset($image[0]['#text']) ? $image[0]['#text'] : null;
<?php
// JSON URL which should be requested
$json_url = 'https://api.feedbin.me/v2/entries.json';
$username = 'username'; // authentication
$password = 'password'; // authentication
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $username . ":" . $password // authentication
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); // Getting JSON result string
$i=0;
foreach (json_decode($result) as $obj) {
if($i==5) break;
$feedbin_title = $obj->title;
$feedbin_url = $obj->url;
echo '<li><a href="', $feedbin_url, '">', $feedbin_title, '</a></li>';
}
?>
foreach (array_slice(json_decode($data_feedin), 0, 5) as $obj)Now you're just showing off. I'm so proud!
<?php
$json_url = 'https://api.feedbin.me/v2/entries.json';
$username = 'username'; // authentication
$password = 'password'; // authentication
$ch = curl_init( $json_url );
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $username . ":" . $password // authentication
);
// Setting curl options
curl_setopt_array( $ch, $options );
$result = curl_exec($ch); // Getting JSON result string
$cache_feedbin = '/var/www/ffio.it/htdocs/'.sha1($json_url).'.json';
if(file_exists($cache_feedbin) && filemtime($cache_feedbin) > time() - 1000){
// if a cache file newer than 1000 seconds exist, use it
$data_feedbin = file_get_contents($cache_feedbin);
} else {
$data_feedbin = file_get_contents($result);
file_put_contents($cache_feedbin, $data_feedbin);
}
foreach (array_slice(json_decode($data_feedbin), 0, 5) as $obj) {
$feedbin_title = $obj->title;
$feedbin_url = $obj->url;
echo '<li><a href="', $feedbin_url, '">', $feedbin_title, '</a></li>';
}
?>
<?php
function get_photos($user_id=XXXX,$count=X,$width=XXX,$height=XXX,$token="XXXX"){
$url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$token.'&count='.$count;
$cache = './BLAH/'.sha1($url).'.json';
if(file_exists($cache) && filemtime($cache) > time() - 1000){
$jsonData = json_decode(file_get_contents($cache), true);
} else {
$jsonData = json_decode(file_get_contents($url), true);
file_put_contents($cache,json_encode($jsonData));
}
$result = '<div id="instagram">'.PHP_EOL;
$i = 0;
foreach ($jsonData as $value) {
$title = (!empty($value->caption->text))?' '.$value->caption->text:'...';
BLAHBLAH
}
$result .= '</div>'.PHP_EOL;
return $result;
}
echo get_photos();
?>