0

I'm new to php and having some troubles converting php array to JS object. I'm trying to get information of a given youtube video. I'm able to receive the info on the client side, but only as php array. I tried using $.parseJSON() but the data gets polluted with backspaces and redundant characters.
JS (using angular $http):

$http({
    url: "assets/controllers/youtubeInfo.php",
    method: "POST",
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: $.param({ getInfo: videoUrl })
}).success(function(data, status, headers, config) {
    //    console.log(JSON.parse(data));
    console.log(data);
}).error(function(data, status, headers, config) { });

PHP code:

function get_youtube($url) {
    $youtube = "http://www.youtube.com/oembed?url=".$url."&format=json";

    $curl = curl_init($youtube);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);
    return json_decode($return, true);
}

$url = $videoKey;

// Display Data 
print_r(get_youtube($url));

This is my output:

Array
(
    [title] => StarCraft - OST
    [html] => <iframe width="459" height="344" src="https://www.youtube.com/embed/pNt0iVG2VOA?feature=oembed" frameborder="0" allowfullscreen></iframe>
    [provider_name] => YouTube
    [thumbnail_height] => 360
    [author_url] => https://www.youtube.com/user/JisengSo
    [provider_url] => https://www.youtube.com/
    [type] => video
    [height] => 344
    [thumbnail_url] => https://i.ytimg.com/vi/pNt0iVG2VOA/hqdefault.jpg
    [version] => 1.0
    [author_name] => Jiseng So
    [width] => 459
    [thumbnail_width] => 480
)
3
  • 1
    json_encode() your array before you 'send' it back to client: echo json_encode(get_youtube($url)); Commented Mar 24, 2016 at 10:13
  • This is the output: ... \"https:\\\/\\\/i.ytimg.com\\\/vi\\\/pNt0iVG2VOA\\\/hqdefault.jpg\", \"width\": 459, \"author_url\": \"https:\\\/\\\/www.youtube.com\\\/user\\\/JisengSo\"}" ... I cant access individual properties, even if i JSON.parse it Commented Mar 24, 2016 at 10:21
  • I just saw that you already retrieve the data as json, then you decode it (in your function). Unless you want to work with that data in php you don't need to do that, rather echo the pure json string Commented Mar 24, 2016 at 10:24

2 Answers 2

2

To get a JS object, just don't json_decode the output (or encode it again):

function get_youtube($url) {
    $youtube = "http://www.youtube.com/oembed?url=".$url."&format=json";

    $curl = curl_init($youtube);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;    // change here
}

$url = $videoKey;

// Display Data 
echo get_youtube($url);    // change here
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Could you post an example how to get the data without the curl detour?
Sorry, I was wrong. It turns out youtube currently does not support JSONP. So you need to make the detour after all. I removed the claim from my answer.
1

Something like the following will probably work:

PHP code:

function get_youtube($url) {
    $youtube = "http://www.youtube.com/oembed?url=".$url."&format=json";

    $curl = curl_init($youtube);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $return = curl_exec($curl);
    curl_close($curl);
    return $return; //If you're only returing it to forward it there's no point decoding it before re-encoding it.
}

$url = $videoKey;

// Display Data 
echo get_youtube($url);

JavaScript:

$http({
    url: "assets/controllers/youtubeInfo.php",
    method: "POST",
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    data: $.param({ getInfo: videoUrl }),
    responseType: "json", //Tells it to expect JSON in the response 
}).success(function(data, status, headers, config) {
    console.log(data);
}).error(function(data, status, headers, config) { });

Note: as far as I know, $http uses XMLHttpRequest so https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-responsetype is applicable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.