我很难通过 json 和 php 获取 youtube 视频数据。
我花了一整夜和一上午的时间尝试从网络和堆栈溢出中获取代码片段。
他们不工作的事实向我表明我没有使用最新的语法。
我认为问这个问题最清楚的方法是问以下属性在 2012 年 11 月时是否正确。
所以这是我的初始变量声明:
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&alt=json");
$json_data = json_decode($json);
谁能告诉我以下说法是否正确:
1. $video_title = $json_data->{'entry'}->{'title'};
2. $video_date = $json_data->{'entry'}->{'published'};
3. $video_duration = $json_data->{'entry'}->{'media:group'}->{'yt$duration'};
4. $video_views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
5. $video_description = $json_data->{'entry'}->{'content'};
我不想通过提供太多其他代码和信息来淡化问题,但我遇到的错误之一是:
Catchable fatal error: Object of class stdClass could not be converted to string
所以我知道其中一个属性不正确。
谢谢你的帮助,我要去喝杯咖啡,然后再回来处理这个问题!
研究
这些资源是对我正在尝试获取的属性的直接 api 引用,应该可以工作,但它们似乎不是 :(。
提要和条目结构:
一个条目的内容:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_entry
标题标签:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_title
发布标签:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_published
yt:duration 标签:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:duration
yt:statistics > viewCount 标签:
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_yt:statistics
内容标签(视频描述):
https://developers.google.com/youtube/2.0/reference#youtube_data_api_tag_content
代码示例(根据要求)
所以我正在做的是:
这是 insert.php 中的相关代码:
// basic form information
$field1 = $_POST["field1"];
$field2 = $_POST["field2"];
$original_link = $_POST["link"];
// add class and video display information
$random_text = array_pop(explode('/',$original_link));
$final_value = "<a class=\'youtube\' href=\"http://www.youtube.com/embed/".$random_text."?rel=0&autohide=1&showinfo=0&autoplay=0&iv_load_policy=3&wmode=transparent\">link</a>";
//start getting the youtube information
$thumb = "<img src=\"http://i.ytimg.com/vi/".$random_text."/mqdefault.jpg\">";
$json = file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&alt=json");
$json_data = json_decode($json);
$video_title = $json_data->entry->title;
$video_date = $json_data->entry->published;
$video_duration = $json_data->entry->media:group->yt:duration;
$video_views = $json_data->entry->ytstatistics->viewCount;
$video_description = $json_data->entry->content;
// put it all together to create an <li>
$final_li_code = "<li class=\".{$field1} .{$field2}\">{$thumb}<div id=\"video_information\"><h3>{$video_title}</h3><div id=\"video_information_left\"><span id=\"date\">{$video_date}</span><span id=\"duration\">{$video_duration}</span><span id=\"another_id\">{$field2}</span></div><div id=\"video_information_right\"><span id=\"video_views\">{$video_views}</span><span id=\"yet_another_id\">{$field1}</span></div><span id=\"description\">{$video_description}</span></div></li>";
最佳答案
收到您的 SOS 消息。以下是您需要进行的更改:
$video_title = $json_data->{'entry'}->{'title'}->{'$t'};
$video_date = $json_data->{'entry'}->{'published'}->{'$t'};
$video_duration = $json_data->{'entry'}->{'media$group'}->{'yt$duration'}->{'seconds'};
$video_views = $json_data->{'entry'}->{'yt$statistics'}->{'viewCount'};
$video_description = $json_data->{'entry'}->{'media$group'}->{'media$description'}->{'$t'};
请注意,$t 是字面值 $ 后跟 t,而不是名为 $t 的变量。
视频 id gzDS-Kfd5XQ 的示例输出:
string(66) "Sesame Street: Ray Charles Sings "I Got A Song" With Bert & Ernie"
string(24) "2008-08-06T18:56:56.000Z"
string(3) "129"
string(6) "828277"
string(342) "For more videos and games check out our new website at http://www.sesamestreet.org
In this video, Bert and Ernie perform with Ray Charles.
Sesame Street is a production of Sesame Workshop, a nonprofit educational organization which also produces Pinky Dinky Doo, The Electric Company, and other programs for children around the world."
PS:如果您熟悉关联数组,请将 true 作为第二个参数传递给 json_decode:
mixed json_decode(string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0]]])
PPS:var_dump 数据、找到所需的所有位并随后编写代码要容易得多。
关于php - 截至 2012 年 11 月,通过 json 和 php 获取 youtube 数据的正确语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13224694/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput