0

用 php 解析这个 json 提要时遇到问题,只返回一个字符串而不是一个对象。需要返回每个项目的标题和 url 字段。

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>
4

1 回答 1

3

根据我在该 URL 看到的 JSON 的结构,如果您正在寻找该posts项目,那么您需要像这样访问:

<?php foreach ($feed->posts as $item) { ?>
于 2012-09-17T17:50:44.643 回答