【问题标题】:how to get a specific value from api curl php如何从 api curl php 获取特定值
【发布时间】:2021-12-19 02:35:44
【问题描述】:

所以我试图从所有索引中获取“title”和“Artist->name” 我尝试了很多次,甚至使用了 foreach,但我仍在学习 curl,只能为第一个索引或特定索引获取它,这是表格

(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 2298089
                    [readable] => 1
                    [title] => Broken Strings
                    [title_short] => Broken Strings
                    [title_version] => 
                    [link] => https://www.deezer.com/track/2298089
                    [duration] => 250
                    [rank] => 749501
                    [explicit_lyrics] => 
                    [explicit_content_lyrics] => 0
                    [explicit_content_cover] => 0
                    [preview] => https://cdns-preview-1.dzcdn.net/stream/c-140e953bb38254bc8db4fe4dc97dd689-8.mp3
                    [md5_image] => 71698262cce4bb1c0e3d25e0707ed092
                    [artist] => Array
                        (
                            [id] => 4523
                            [name] => James Morrison
                            [link] => https://www.deezer.com/artist/4523
                            [picture] => https://api.deezer.com/artist/4523/image
                            [picture_small] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/56x56-000000-80-0-0.jpg
                            [picture_medium] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/250x250-000000-80-0-0.jpg
                            [picture_big] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/500x500-000000-80-0-0.jpg
                            [picture_xl] => https://e-cdns-images.dzcdn.net/images/artist/794afe1359f598a28dc12c5496fefc2e/1000x1000-000000-80-0-0.jpg
                            [tracklist] => https://api.deezer.com/artist/4523/top?limit=50
                            [type] => artist
                        )

                    [album] => Array
                        (
                            [id] => 229099
                            [title] => Songs For You, Truths For Me (International Exclusive Bundle)
                            [cover] => https://api.deezer.com/album/229099/image
                            [cover_small] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/56x56-000000-80-0-0.jpg
                            [cover_medium] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/250x250-000000-80-0-0.jpg
                            [cover_big] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/500x500-000000-80-0-0.jpg
                            [cover_xl] => https://e-cdns-images.dzcdn.net/images/cover/71698262cce4bb1c0e3d25e0707ed092/1000x1000-000000-80-0-0.jpg
                            [md5_image] => 71698262cce4bb1c0e3d25e0707ed092
                            [tracklist] => https://api.deezer.com/album/229099/tracks
                            [type] => album
                        )

                    [type] => track
                )


            [1] => Array
                (
                    [id] => 1196110412

等等,这里是代码

    $ch = curl_init();
    $url = "https://api.deezer.com/search?q=broken%20strings";
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $resp = curl_exec($ch);
    if($e = curl_error($ch)){
        echo $e;
    }else{
        $decoded = json_decode($resp,true);
        print_r($decoded);
    }
    curl_close($ch);

这个帖子所有的数组

【问题讨论】:

    标签: php api curl


    【解决方案1】:

    $decoded['data'] 将包含一个包含您所有记录的数组,因此您希望 foreach 超过它:

    foreach ($decoded['data'] as $record) {
    

    在该循环的每次迭代中,$record 将是每个单独记录的子数组。所以在第一次迭代中,$record 将是:

    Array(
        [id] => 2298089
        [readable] => 1
        [title] => Broken Strings
        [title_short] => Broken Strings
        [title_version] => 
        [link] => https://www.deezer.com/track/2298089
        [duration] => 250
        [rank] => 749501
        ...
    

    然后您可以引用该数组中的每个字段,例如$record['id']$record['link']。要“深入”到多维数组,只需堆叠连续的 [] 引用,例如 $record['album']['id']$record['artist']['link']

    大家一起:

    foreach ($decoded['data'] as $record) {
        printf("Title: %s\n", $record['title']);
        printf("Artist: %s\n\n", $record['artist']['name']);
    }
    

    【讨论】:

    • 非常感谢它的工作原理:D
    • 也感谢您的快速回复,我真的很感激
    猜你喜欢
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2017-07-14
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    相关资源
    最近更新 更多