【问题标题】:json API data with all of its contentjson API 数据及其所有内容
【发布时间】:2016-06-22 08:55:51
【问题描述】:

我需要在网站上显示 json api 数据。但我无法这样做,我使用以下脚本。这只是以 json 格式返回数据。我想以适当的格式展示新闻和图片。

<?php 
$json_url = "http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=570&count=3&maxlength=300&format=json";
$json = file_get_contents($json_url);
$json=str_replace('},

]',"}

]",$json);
$data = json_decode($json, true);

echo "<pre>";
print_r($data);
echo "</pre>";
?>

【问题讨论】:

    标签: steam


    【解决方案1】:

    根据您提出的问题,我希望您会发现这对您有所帮助:

    CSS 粘贴在&lt;head&gt;&lt;style&gt;here&lt;/style&gt;&lt;/head&gt;之间

    #header {
        background-color:black;
        color:white;
        text-align:center;
        padding:5px;
    }
    .news-entry {
        color: #606060;
        font: 11px/18px Arial,Verdana,sans-serif;
        padding: 1px 0;
        width: 630px;
    }
    .news-title {
        color: #848484;
        font: 10px Arial,Verdana,sans-serif;
        width: 630px;
    }
    .news-title h2 {
        border-bottom: 1px dotted #1c4670;
        color: #2771a5;
        font: 24px/30px "Trebuchet MS",Arial,Verdana,sans-serif;
    }
    .news-title-info {
        height: 22px;
        width: 630px;
    }
    .news-date {
        float: left;
        line-height: 22px;
        padding-left: 18px;
    }
    .news-author {
        float: right !important;
        line-height: 22px;
        padding-left: 16px;
    	color:#00C;
    }
    .featured-news {
        float: left;
        margin-bottom: 10px;
        width: 630px;
    }
    #footer {
        background-color:black;
        color:white;
        clear:both;
        text-align:center;
        padding:5px;
    }

    代码 粘贴在&lt;body&gt;here&lt;/body&gt;之间

    <?php 
    $json_url	=	"http://api.steampowered.com/ISteamNews/GetNewsForApp/v0002/?appid=570&count=3&maxlength=300&format=json";
    $json		=	file_get_contents($json_url);
    $json		=	str_replace('},]',"}]",$json);
    $data		=	json_decode($json, true);
    $appnews	=	$data['appnews']['newsitems'];
    ?>
    <div id="header">
    	<h1>News</h1>
    </div>
    <div class="featured-news">
    <?php
        foreach($appnews as $news)
        {
        ?>
        <div class="news-entry">
            <h2><a href=""><?php echo $news['title']; ?></a></h2>
            <div class="news-title-info">
                <div class="news-date"><?php echo date('l jS \of F Y h:i:s A', $news['date']); ?></div>
                <?php
    			if(!empty($news['author'])){
    			?>	
                    <div class="news-author">By <?php echo $news['author']; ?></a></div>
            	<?php
    			}
    			?>
            </div>
        </div>
        <div class="news-entry">
            <p>
                <img width="72" height="90" align="left" alt="Dota 2" src="http://cdn.mos.cms.futurecdn.net/DavzDRpQ36gGQGmiwELPs5-650-80.jpg" style="margin-left: 5px; margin-right: 5px;">
                <?php echo $news['contents']; ?>
            </p>
            <p>
                <a class="" target="_blank" href="<?php echo $news['url']; ?>">Read more</a>
            </p>
        </div>
        <?php
        }
        ?>
    </div>
    <div id="footer">
    Some footer here...
    </div>

    PS:返回的json中没有news image的字段,所以需要把你喜欢的图片改成图片的路径。

    【讨论】:

    • 感谢您的回复!代码工作正常。但我想显示与新闻相关的图像。它在每条新闻上都显示相同的图像。在 RSS 提要中,您可以拥有与新闻相关的图像,而 API 无法获取任何图像。
    • 图片url返回的JSON数据中没有任何字段,所以我只是放了一些随机的图片路径。
    猜你喜欢
    • 2015-04-12
    • 2021-11-15
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多