解析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