php youtube json 解码

发布于 2024-10-24 17:50:37 字数 685 浏览 0 评论 0原文

我尝试使用 JSON 解码来获取 youtube API feed。但是,当我将 JSON 结果粘贴到 http://www.jsonlint.com/ 中时,我注意到类似

"media$group": {
  "media$category": [

不幸的是,某些符号被 php 拒绝了。这是我的代码,我尝试删除这个 $ 符号,但可能不成功。我该如何解决这个问题?

$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('$','', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) { 
...
}

I tried to use JSON decode to get the youtube API feed. However, when I paste the JSON result in http://www.jsonlint.com/ I noticed something like

"media$group": {
  "media$category": [

Unfortunately some symbols are rejected by php. Here is my code, I tried to remove this $ symbol, but maybe not success. How do I solve this?

$url = 'http://gdata.youtube.com/feeds/api/videosq=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('
,'', $body1);
curl_close($ch);
$data = json_decode($body);
foreach ($data->feed->entry as $result) { 
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-10-31 17:50:37

您的问题是使用 PHP 标识符来访问内容。这里最简单的解决方案是获取一个数组而不是一个对象:

$data = json_decode ( $json , $assoc = true );

这允许通过以下方式访问字段:

echo $result['media$group']['media$description'];

如果您想保留对象语法,则可以使用此拼凑:(

echo $result->{'media$group'}->{'media$category'};

但是数组在这里更安全。您不会得到如果格式更改且属性不存在,则会出现致命错误。)

Your problem is the usage of PHP identifiers to access the contents. The simplest solution here would be to get an array instead of an object:

$data = json_decode ( $json , $assoc = true );

This allows access to fields with:

echo $result['media$group']['media$description'];

If you want to keep the object syntax, that's possible with this kludge:

echo $result->{'media$group'}->{'media$category'};

(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)

清引 2024-10-31 17:50:37

这项工作:

<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('
,'', $body1);
curl_close($ch);
$data = json_decode($body);

foreach ($data->feed->entry as $result) { 
    var_dump($result);
}
?>

This work:

<?php
$url = 'http://gdata.youtube.com/feeds/api/videos?q=football&orderby=published&v=2&alt=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $url);
$body1 = curl_exec($ch);
$body = str_replace('
,'', $body1);
curl_close($ch);
$data = json_decode($body);

foreach ($data->feed->entry as $result) { 
    var_dump($result);
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文