0

I'm new here and am having some trouble working through a json using PHP. Obviously, each json is different and this one is causing some issues for myself. The json is here. I have some base php started and this gives me an array/obect but I cannot get to what I need.

I simply need the Artist and the Title of the track

This

$json_string = 'https://api.acrcloud.com/v1/monitor-streams/s-bVX73QK/results?access_key=4709cb1d4bf05a8e782034797f7a8189';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
var_dump(json_decode($json_string, true));
print_r($obj);

Gives me the following

NULL Array ( 
    [0] => Array ( 
        [status] => Array ( 
            [msg] => Success 
            [code] => 0 
            [version] => 1.0 
        ) 
        [result_type] => 0 
        [metadata] => Array ( 
            [type] => real-time 
            [timestamp_utc] => 2018-07-26 03:00:48 
            [music] => Array ( 
                [0] => Array ( 
                    [album] => Array ( 
                        [name] => 17 
                    ) 
                    [play_offset_ms] => 6160 
                    [sample_begin_time_offset_ms] => 3840 
                    [title] => 17 
                    [result_from] => 3 
                    [release_date] => 2017-09-01 
                    [sample_end_time_offset_ms] => 8780 
                    [genres] => Array ( 
                        [0] => Array ( 
                            [name] => Dance 
                        ) 
                    ) 
                    [label] => Columbia 
                    [duration_ms] => 196280 
                    [score] => 100 
                    [db_begin_time_offset_ms] => 0 
                    [artists] => Array ( 
                        [0] => Array ( 
                            [name] => **MK** 
                        ) 
                    ) 
                    [db_end_time_offset_ms] => 5380 
                    [external_ids] => Array ( 
                        [isrc] => GBARL1701492 
                        [upc] => 886446683140 
                    ) 
                    [acrid] => 80538cd22ac21893492c106906c70169 
                    [external_metadata] => Array ( 
                        [spotify] => Array ( 
                            [album] => Array ( 
                                [name] => 17 
                                [id] => 0K1826JxL1dViQBsEKApN5 
                            ) 
                            [track] => Array ( 
                                [name] => 17 
                                [id] => 15DwFznkBJir7AK9PyMyRR 
                            ) 
                            [artists] => Array ( 
                                [0] => Array ( 
                                    [name] => MK 
                                    [id] => 1yqxFtPHKcGcv6SXZNdyT9 
                                ) 
                            ) 
                        ) 
                        [deezer] => Array ( 
                            [album] => Array ( 
                                [name] => 17 
                                [id] => 46039252 
                            ) 
                            [track] => Array ( 
                                [name] => 17 
                                [id] => 393116082 
                            ) 
                            [artists] => Array ( 
                                [0] => Array ( 
                                    [name] => MK 
                                    [id] => 148517 
                                ) 
                            ) 
                        ) 
                    ) 
                ) 
                [1] => Array ( 
                    [album] => Array ( 
                        [name] => All I Need 
                    ) 
                    [play_offset_ms] => 134720 
                    [sample_begin_time_offset_ms] => 0 
                    [title] => All I Need 
                    [result_from] => 3 
                    [release_date] => 2018-05-04 
                    [sample_end_time_offset_ms] => 1940 
                    [label] => Epic Amsterdam 
                    [duration_ms] => 184000 
                    [score] => 91 
                    [db_begin_time_offset_ms] => 125160 
                    [artists] => Array ( 
                        [0] => Array ( 
                            [name] => Dimitri Vegas & Like Mike & Gucci Mane 
                        ) 
                    ) 
                    [db_end_time_offset_ms] => 127100 
                    [external_ids] => Array ( 
                        [isrc] => BEG851800020 
                        [upc] => 886447088173 
                    ) 
                    [acrid] => fc19f90f4d4c6d879288b06e2abec992 
                    [external_metadata] => Array ( 
                        [spotify] => Array ( 
                            [album] => Array ( 
                                [name] => All I Need (with Gucci Mane) 
                                [id] => 7nOmSjsk3WQcuSvkAysvBF 
                            ) 
                            [track] => Array ( 
                                [name] => All I Need (with Gucci Mane) 
                                [id] => 2OSsMwCiZPC44tJ4OPVSZF 
                            ) 
                            [artists] => Array ( 
                                [0] => Array ( 
                                    [name] => Dimitri Vegas & Like Mike 
                                    [id] => 73jBynjsVtofjRpdpRAJGk 
                                ) 
                                [1] => Array ( 
                                    [name] => Gucci Mane 
                                    [id] => 13y7CgLHjMVRMDqxdx0Xdo 
                                ) 
                            ) 
                        ) 
                    ) 
                ) 
            ) 
        ) 
    ) 
) 

How on earth do I get the Artist (in this case MK) and the track name (in this case 17) out and printed?

Geshode
  • 3,600
  • 6
  • 18
  • 32
vinyllicker
  • 83
  • 1
  • 5
  • `var_dump(json_decode($json_string, true));` <- you realise `$json_string` is your URL, right? That's producing the `NULL` (because it cannot be decoded as JSON) – Phil Jul 26 '18 at 06:06
  • `$obj[0]['metadata']['music'][0]['artists'][0]['name']` and `$obj[0]['metadata']['music'][0]['title']` – Phil Jul 26 '18 at 06:12
  • Thanks soooo much this is amazing. Yes the var_dump was a carryover from some testing, shouldn't have been in there. The final solution for anybody interested – vinyllicker Jul 26 '18 at 10:35
  • $json_string = 'https://api.acrcloud.com/v1/monitor-streams/s-bVX73QK/results?access_key=4709cb1d4bf05a8e782034797f7a8189'; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata,true); print_r($obj[0]['metadata']['music'][0]['title']); – vinyllicker Jul 26 '18 at 10:36

0 Answers0