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
)