php - 从 API 检索信息

标签 php json api

我正在尝试从此 API 中提取信息:http://ex.fm/api/v3/user/dan/loved

我在使用 Google Maps API 之前就已完成此操作,但那是使用 simplexml_load_file。我尝试将相同的脚本应用于此 API,但它就是不喜欢它。

所以我有点不知道从哪里开始。本质上,我试图获取每个项目的图像源并显示它。

最佳答案

我认为你需要看看这里:PHP JSON

$contents = file_get_contents("http://ex.fm/api/v3/user/dan/loved");
$contents = json_decode($contents);
// in order to display images
foreach ($contents->songs as $song) {
    printf("Image Small: %s\n", $song->image->small);
    printf("Image Large: %s\n", $song->image->large);
    // ...
}

// in order to store song attributes
/* $songs = array();
foreach ($contents->songs as $song) {
    $songs[] = array(
        'title' => $song->title,
        'image_small' => $song->image->small,
        // add more stuff...
    );
} */

输出看起来像;

Image Small: http://userserve-ak.last.fm/serve/34/59939953.png
Image Large: http://userserve-ak.last.fm/serve/126/59939953.png
Image Small: http://www.blogcdn.com/www.spinner.com/media/2013/02/thao-with-the-get-down-stay-down-320.jpg
Image Large: http://www.blogcdn.com/www.spinner.com/media/2013/02/thao-with-the-get-down-stay-down-320.jpg
Image Small: http://24.media.tumblr.com/tumblr_mhpvhhmZbD1qz4e0mo1_1360016551_cover.jpg
Image Large: http://24.media.tumblr.com/tumblr_mhpvhhmZbD1qz4e0mo1_1360016551_cover.jpg
...

关于php - 从 API 检索信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14945384/

相关文章:

javascript - 使用 $http.get 显示 json 文件中的一个数组项

PHP 在数组中的元素之间插入元素

c - 网络编程通过c编译执行但不打印IP地址...!

javascript - 将 JavaScript 对象属性附加到另一个对象

laravel - 从同一个 Controller 管理 View 和 api

c - 在 linux C 中寻找位图实现 API

php - 如何在 php 中批量取消设置数组元素?

php - 将字符串拆分为自定义格式

PHP/正则表达式。如何替换域后 url 中的所有百分比 (%) 字符?

PHPUnit , PEAR 升级错误