Home > Back-end >  how to get a specific value from api curl php
how to get a specific value from api curl php

Time:11-05

So i was trying to get "title" and "Artist->name" from all the indexes and i tried many times even used foreach but i am still learning curl and can only get it for the first index or a specific index here is the table

(
    [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

etc.., and here is the code

    $ch = curl_init();
    $url = "https://api.deezer.com/search?q=broken strings";
    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);

this posts all of array

CodePudding user response:

$decoded['data'] will contain an array with all your records, so you want to foreach over that:

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

On each iteration of that loop, $record will be the subarray for each individual record. So on the first iteration, $record will be:

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

Then you can reference each field in that array like $record['id'] or $record['link']. To "drill down" into multi-dimensional arrays, just stack consecutive [] references, like $record['album']['id'] or $record['artist']['link'].

All together:

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