php youtube json 解码
我尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题是使用 PHP 标识符来访问内容。这里最简单的解决方案是获取一个数组而不是一个对象:
这允许通过以下方式访问字段:
如果您想保留对象语法,则可以使用此拼凑:(
但是数组在这里更安全。您不会得到如果格式更改且属性不存在,则会出现致命错误。)
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:
This allows access to fields with:
If you want to keep the object syntax, that's possible with this kludge:
(But arrays are safer here. You don't get a fatal error should the format change and properties be absent.)
这项工作:
This work: