-1

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>"}" 
  • 1
    You probably shouldn’t have included your API key here - anyone can now use it and dry up your quota, should they so desire. You should be invalidating that key as soon as possible, and know that even if you edit the key out now that it’s likely cached by Google and other search engines, and will always be available in this post’s edit history. – esqew Oct 08 '21 at 15:09
  • @esqew Good point. Marshel111 you will need to change your API key. – Patfreeze Oct 08 '21 at 15:14

1 Answers1

0

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
        }


    }


Patfreeze
  • 680
  • 6
  • 16
  • yeah i tried it and i got this-> – marshel111 Oct 07 '21 at 16:08
  • string(363) ""{\"Status\":\"Success\",\"Status_Code\":404,\"Title\":\"50 Cent - Many Men (Wish Death) (Dirty Version)\",\"Download_Size\":1898696,\"Video_Duration\":297,\"Video_Thumbnail\":\"https:\/\/img.youtube.com\/vi\/5D3crqpClPY\/hqdefault.jpg\",\"Full_Video_Link\":\"https:\/\/www.youtube.com\/watch?v=5D3crqpClPY\",\"Download_url\":\"https:\/\/tinyurl.com\/yhnem9k9\"}"" Also a string – marshel111 Oct 07 '21 at 16:08
  • I don't see in your example how you read `$response = $YouTubeLink->loader($_GET["ID"]);` Could you output only the `$response` from `curl_exec` – Patfreeze Oct 07 '21 at 17:07
  • Suspect that you need to do something before `json_decode` it. – Patfreeze Oct 07 '21 at 17:14
  • $response = loader($_GET["ID"]) my first line in the text above is not in the class i build. I seperatet the whole curl function in a class seperatet in a FUNCTION.php. The first page give me per GET the Video ID and i submitet it via YouTubeLink = new Downloader(). Then i use my curl funktion YouTubeLink->loader($_GET["ID"]). But every time i get a string. i tried to put some "[" brackets "]" on the start and on the end but it dosent work at all. i have to cut the string every time and use explode() and str_replace(). But that cant be a good solution – marshel111 Oct 08 '21 at 04:44
  • Sorry for my bad English. Could you, in your original POST, copy/paste the `Orignal response` from the API, without manipulate it. This will be a good start to help you better. – Patfreeze Oct 08 '21 at 13:58
  • Nothing to sorry about...thanks for your help anyway. I added the original response in the question above – marshel111 Oct 08 '21 at 14:36
  • I was searching and found nothing special... Your output seems not the original from the `curl_exec` if so, it was the URL API that you call that don't return a valid json string format. Do you have used `var_dump` to output this? https://weichie.com/blog/curl-api-calls-with-php/ – Patfreeze Oct 08 '21 at 14:59
  • Yes I use var_dump(response). I try to separate it with explode(", ", $response). It gives me an array with seperatet strings but I have to delete some signs in each array..... Thank you so much for help – marshel111 Oct 09 '21 at 09:09
  • Could you provide also the code that is using the function `$YouTubeLink->loader($_GET["ID"]);` – Patfreeze Oct 12 '21 at 16:00
  • It's in the main text above. There is the curl request and the return of the response – marshel111 Oct 17 '21 at 17:43
  • `$response = $YouTubeLink->loader($_GET["ID"]);` that's it... What do you do **with** the variable `$response`? like `$response->Status` must output `Success`. – Patfreeze Oct 18 '21 at 13:02
  • $response = $YouTubeLink->loader($_GET["ID"]); $searchArray =["\\", "{", "}", "\""]; $convertArray = explode(",",$response); $convertArray = str_replace($searchArray,"",$convertArray); – marshel111 Oct 21 '21 at 02:44
  • first i load my ID in the loader-Funktion, the response i get is a string so i delete all \\ , { , } , \ signs to get a clean string. Next i try to cut the response with explode(",",$response) an bring it in array form. – marshel111 Oct 21 '21 at 02:49
  • there is no response header i can check....i only get the long string with ALL informations in it. – marshel111 Oct 21 '21 at 02:51
  • string(363) ""{\"Status\":\"Success\.......is the first part of the endless string. I really think there is something laggy with this API – marshel111 Oct 21 '21 at 02:55
  • Look closer my code.. Pretty sure you don't use the good function to read from the API. Also I update my POST to add how to read some data. – Patfreeze Oct 21 '21 at 15:44
  • 1
    Wow super! thanks a lot....you helped me – marshel111 Oct 22 '21 at 16:54