PHP - 将 php 数组转换为 JS 对象时遇到问题
PHP - having troubles converting php array to JS object
我是 php 的新手,在将 php array
转换为 JS object
时遇到了一些问题。我正在尝试获取给定 youtube 视频的信息。我能够在客户端接收信息,但只能作为 php array
。我尝试使用 $.parseJSON()
,但数据被退格键和冗余字符污染了。
JS(使用 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代码:
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));
这是我的输出:
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
)
要获取 JS 对象,只需不 json_decode
输出(或再次编码):
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
像下面这样的东西可能会起作用:
PHP代码:
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) { });
注意:据我所知,$http 使用 XMLHttpRequest,因此 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-responsetype 适用。
我是 php 的新手,在将 php array
转换为 JS object
时遇到了一些问题。我正在尝试获取给定 youtube 视频的信息。我能够在客户端接收信息,但只能作为 php array
。我尝试使用 $.parseJSON()
,但数据被退格键和冗余字符污染了。
JS(使用 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代码:
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));
这是我的输出:
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
)
要获取 JS 对象,只需不 json_decode
输出(或再次编码):
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
像下面这样的东西可能会起作用:
PHP代码:
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) { });
注意:据我所知,$http 使用 XMLHttpRequest,因此 https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#xmlhttprequest-responsetype 适用。