user990717
user990717

Reputation: 470

Parse Json File

Having trouble parsing this json feed with php, only returns a string instead of an object. Need to return the title and url field for each item.

function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$feed = json_decode(get_data('http://xxxx/?json=1&post_type=logos&count=5', TRUE));
var_dump($feed);
?>
<div class="content-box-right">
    <h1>LOGO &amp; GRAPHIC STANDARDS</h1>

    <div class="content-sep"></div>
    <?php foreach ($feed as $item) {  
           var_dump($item);?>
         } 
     ?>

</div>

Upvotes: 0

Views: 155

Answers (1)

Mike Brant
Mike Brant

Reputation: 71384

Based on the structure of the JSON I see at that URL, it looks like if you are looking for the posts item, then you would need to access like:

<?php foreach ($feed->posts as $item) { ?>

Upvotes: 3

Related Questions