解析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 & GRAPHIC STANDARDS</h1>
<div class="content-sep"></div>
<?php foreach ($feed as $item) {
var_dump($item);?>
}
?>
</div>
。您确定要cURL输出是以json格式?因为如果它是json格式,那么我不认为在这里有什么可能会出错 – Shades88
理想情况下,你需要显示你回来的东西 - 即。当前从'get_data()'返回。 – Orbling
无法重现,根本不是字符串:http://codepad.viper-7.com/nsNQyX – hakre