json_decode and arrays

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #31, on June 3rd, 2013, 09:48 PM »
Quote from Arantor on June 3rd, 2013, 09:36 PM
I can't remember :/
I think DateTime does. Because it displayed correct time and strtotime not. Then I added set_locale and strtotime displayed correct time too.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #33, on June 5th, 2013, 08:48 PM »
My JSON has

Code: [Select]
        "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"
           }
        ]

How can I retrieve 'medium' url?

I think it's something like this..?

Code: [Select]
        if ($track['image']['size'] == medium) {
$thumb = $track['image']['#text'];
}
Re: json_decode and arrays
« Reply #34, on June 5th, 2013, 09:24 PM »
Some guy suggested me

Code: [Select]
$image = null;
foreach ($track['image'] as $i) {
    if ($i['size'] == 'medium') {
        $image = $i['#text'];
        break;
    }
}

or

Code: [Select]
$image = array_reduce($track['image'], function ($image, array $i) { return $image ?: ($i['size'] == 'medium' ? $i['#text'] : null); });

or

Code: [Select]
$image = array_filter($track['image'], function ($image) { return $image['size'] == 'medium'; });
$image = isset($image[0]['#text']) ? $image[0]['#text'] : null;

The one line solution is cool :D
Re: json_decode and arrays
« Reply #35, on June 6th, 2013, 08:21 PM »
Code: [Select]
<?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>';
    }
?>


It works fine. I get a 100 objects JSON result. I'm trying to limit the objects processed to the first 5. Why doesn't work the $i=0; if($i==5) break; thing? :)

Alanthar

  • Congratulations. :)
  • Posts: 22
Re: json_decode and arrays
« Reply #36, on June 6th, 2013, 09:30 PM »
I think you forgot to increase the counter:

  ...
  echo ' ...
  $i++;
}

MultiformeIngegno

  • Posts: 1,337

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

MultiformeIngegno

  • Posts: 1,337

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #42, on June 6th, 2013, 10:02 PM »Last edited on June 7th, 2013, 12:37 AM
Code: [Select]
<?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), 05) as $obj) { 
$feedbin_title $obj->title
$feedbin_url $obj->url
echo 
'<li><a href="'$feedbin_url'">'$feedbin_title'</a></li>'

?>


Why I'm getting "false" in the locally cached json? Is that json_encode wrong?

EDIT: Forgot to say it works perfectly if I don't try to load it from cache (so it's not something related to cURL or authentication).
Re: json_decode and arrays
« Reply #43, on June 7th, 2013, 12:43 AM »
Solved.
It should have been $data_feedbin = $result;, not $data_feedbin = file_get_contents($result); :)
Re: json_decode and arrays
« Reply #44, on June 8th, 2013, 06:22 PM »
Code: [Select]
    <?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();
    
?>

What's wrong with foreach ($jsonData as $value)? I'm grabbing an associative array with the "true" argument in json_decode.. isn't that foreach okay? Or does it grab objects instead?

Here's the json input: http://pastebin.com/EBGG10hx