Search code examples
phpjsonstringresponserapidapi

I want to use a free YouTube to mp3 API but i got only string as response. How i can transfer the response in an object/json/array? PHP


I tried to convert the ARRAY per json_encode($response) but it still a string. I cut the string so i get the right parts of it but its working only in this video so i have to use the $response[position_I_want]. How i can solve the problem? Thanks for all help!

$response = $YouTubeLink->loader($_GET["ID"]);        //my class with the curl function loader

function loader($ID)
    {


        $curl = curl_init();

        curl_setopt_array($curl, [
            CURLOPT_URL => "https://youtube-to-mp32.p.rapidapi.com/api/yt_to_mp3?video_id=$ID", // hier wird die Video ID übergeben
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_HTTPHEADER => [
                "x-rapidapi-host: youtube-to-mp32.p.rapidapi.com",
                "x-rapidapi-key: a3ab095a57msh2d08de201b482cbp1559dcjsnaa6636261faa"
            ],
        ]);

        // speichert die api antwort in die Variable response
        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);

        if ($err) {
            echo "cURL Error #:" . $err;
        } else {
            return json_encode($response); // <---This dosent work it gives only string back
        }


    }

here is the original response
string(377) "{"Status":"Success","Status_Code":404,"Title":"2Pac, Pop Smoke - Write This Down ft. Biggie, DMX, Eazy E, Ice Cube, Dr Dre, NWA, Nipsey, Snoop Dogg","Download_Size":5161074,"Video_Duration":798,"Video_Thumbnail":"https://img.youtube.com/vi/HI6gMkfRjE0/hqdefault.jpg","Full_Video_Link":"https://www.youtube.com/watch?v=HI6gMkfRjE0","Download_url":"<here is the download link>"}" 

Solution

  • json_decode for decode JSON string format. https://www.php.net/json_decode

    You use json_encode() to encode in JSON string format. https://www.php.net/json_encode

    <?php
    /***************************************
    * How to read data from json_decode
    ***************************************/
    $response = $YouTubeLink->loader($_GET["ID"]);        //my class with the curl function loader
    echo $response->Status."<br />\n"; // should output 'Success'
    echo $response->Title."<br />\n"; // should output '2Pac, Pop Smoke - Write This Down ft. Biggie, DMX, Eazy E, Ice Cube, Dr Dre, NWA, Nipsey, Snoop Dogg'
    
    
    function loader($ID)
        {
    
    
            $curl = curl_init();
    
            curl_setopt_array($curl, [
                CURLOPT_URL => "https://youtube-to-mp32.p.rapidapi.com/api/yt_to_mp3?video_id=$ID", // hier wird die Video ID übergeben
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_HTTPHEADER => [
                    "x-rapidapi-host: youtube-to-mp32.p.rapidapi.com",
                    "x-rapidapi-key: YOUR_KEY"
                ],
            ]);
    
            // speichert die api antwort in die Variable response
            $response = curl_exec($curl);
            $err = curl_error($curl);
    
            curl_close($curl);
    
            if ($err) {
                echo "cURL Error #:" . $err;
            } else {
                //return json_decode($response, true); // If you want an array instead of object
                return json_decode($response); // <---Use the good function
            }
    
    
        }