0

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>
3
  • are you sure your cURL output is in json format? Because if it's in json format, then I don't think there's anything that can go wrong here Commented Sep 17, 2012 at 17:48
  • 1
    Ideally, you need to show what you are getting back - ie. the current return from get_data(). Commented Sep 17, 2012 at 17:50
  • Unable to reproduce, not a string at all: codepad.viper-7.com/nsNQyX Commented Sep 17, 2012 at 17:54

1 Answer 1

3

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) { ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.