Php 从API检索信息

Php 从API检索信息,php,json,api,Php,Json,Api,我试图从这个API中提取信息: 在使用GoogleMapsAPI之前,我已经这样做了,但是那是使用simplexml\u load\u文件。我尝试将相同的脚本应用于此API,但它就是不喜欢 所以我不知道从哪里开始。本质上,我试图获取每个项目的图像源并显示它。看看php的函数json\u decode:下面是我将如何做的。(我还没有测试过这个): 此时,您已经从URL获取了所有数据,并将其从JSON格式转换为数组。要测试它,您可以尝试以下任一方法: print_r($contents); 或 显

我试图从这个API中提取信息:

在使用GoogleMapsAPI之前,我已经这样做了,但是那是使用
simplexml\u load\u文件
。我尝试将相同的脚本应用于此API,但它就是不喜欢


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

看看php的函数json\u decode:

下面是我将如何做的。(我还没有测试过这个):

此时,您已经从URL获取了所有数据,并将其从JSON格式转换为数组。要测试它,您可以尝试以下任一方法:

print_r($contents);

显然,$contents变量可以定义为您喜欢的任何变量

如果您还有其他问题,请在下面发表评论:)


彼得

我想你需要看看这里:

输出将看起来像

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 ... 图像小:http://userserve-ak.last.fm/serve/34/59939953.png 图像大:http://userserve-ak.last.fm/serve/126/59939953.png 图像小:http://www.blogcdn.com/www.spinner.com/media/2013/02/thao-with-the-get-down-stay-down-320.jpg 图像大:http://www.blogcdn.com/www.spinner.com/media/2013/02/thao-with-the-get-down-stay-down-320.jpg 图像小:http://24.media.tumblr.com/tumblr_mhpvhhmZbD1qz4e0mo1_1360016551_cover.jpg 图像大:http://24.media.tumblr.com/tumblr_mhpvhhmZbD1qz4e0mo1_1360016551_cover.jpg ...
请看。该文件是JSON格式的,您需要用
JSON\u decode
解码它。有一个JSON文件。。。您可以使用file_get_contents($path等)获取json数组,并使用json_decode将其作为对象数组。。。有了它,你可以获取你需要的任何信息。一旦你使用了
json\u decode
file\u get\u contents
,使用
print\u r
对结果进行打印,以找出结果数组的格式。非常感谢!这确实帮助我开始了:)@Alex;不客气。但请记住在解码后执行类似操作:
如果($contents->status_text=='OK'),则处理foreach或…
感谢您的回答,真的很有帮助:)
echo $contents['status_text'];
$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 ...