我想使用免费的 YouTube 播放 mp3 API,但我只收到字符串作为响应。如何在 object/json/array 中传输响应? PHP

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

我尝试根据 json_encode($response) 转换 ARRAY,但它仍然是一个字符串。我切断了字符串,所以我得到了它的正确部分,但它只在这个视频中有效,所以我必须使用 $response[position_I_want]。我该如何解决这个问题? 感谢大家的帮助!

$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>"}" 

json_decode 用于 解码 JSON 字符串格式。 https://www.php.net/json_decode

您使用 json_encode() 以 JSON 字符串格式进行编码。 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
        }


    }