2012-09-17 114 views
0

解析php的这个json feed时遇到问题,只返回一个字符串而不是一个对象。需要返回每个项目的标题和网址字段。解析Json文件

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> 
+0

。您确定要cURL输出是以json格式?因为如果它是json格式,那么我不认为在这里有什么可能会出错 – Shades88

+1

理想情况下,你需要显示你回来的东西 - 即。当前从'get_data()'返回。 – Orbling

+0

无法重现,根本不是字符串:http://codepad.viper-7.com/nsNQyX – hakre

回答

3

基于JSON的我在那个URL看到的结构,它看起来像,如果你正在寻找的posts项目,那么你就需要来访问,如:

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

谢谢诀窍 – user990717

+0

@ user990717接受它然后 – Orbling