在2012年11月,通过json和php获取youtube数据的正确语法是什么?

use*_*287 4 php syntax json youtube-api

我很难通过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);
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我,如果以下是正确的:

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'};
Run Code Online (Sandbox Code Playgroud)

我不想通过提供太多其他代码和信息来淡化问题,但我得到的错误之一是:

Catchable fatal error: Object of class stdClass could not be converted to string
Run Code Online (Sandbox Code Playgroud)

所以我知道其中一个属性是不正确的.

谢谢你的帮助,我要去喝咖啡然后回来吧!

研究

这些资源是我想要获得的属性的直接api引用,应该可以工作,但它们似乎不是:(.

饲料和进入结构:

https://developers.google.com/youtube/2.0/developers_guide_protocol_understanding_video_feeds#Understanding_Video_Entries

条目的内容:

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:持续时间标签:

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

代码示例(根据要求)

所以我在做的是:

  • 我有一张表格
  • 提交后,它由php文件(insert.php)处理
  • 这会对数据进行一些更改,然后提交到数据库
  • 我在$ final_li_code开头的行中得到错误消息(但是如果没有包含json变量,则代码有效,所以问题在于json变量)
  • (我被告知这种形式很容易被sql注入,但它不是一个面向公众的形式,即它是受htaccess/htpasswd保护的).

这是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&amp;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>";
Run Code Online (Sandbox Code Playgroud)

Sal*_*n A 5

得到你的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'};
Run Code Online (Sandbox Code Playgroud)

请注意,$t是文字$后跟t,而不是名为的变量$t.

视频ID gzDS-Kfd5XQ的示例输出:

string(66)"Sesame Street:Ray Charles Sings"我有一首歌"Bert&Ernie"

string(24)"2008-08-06T18:56:56.000Z"

string(3)"129"

string(6)"828277"

string(342)"有关更多视频和游戏,请访问我们的新网站 http://www.sesamestreet.org

在这段视频中,Bert和Ernie与Ray Charles合作演出.

芝麻街是芝麻工作室的制作,芝麻工作室是一家非营利性教育机构,也生产Pinky Dinky Doo,The Electric Company以及其他针对全球儿童的节目."

PS:如果您对关联数组感到满意,请将true作为第二个参数传递给json_decode:

mixed json_decode(string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0]]])
Run Code Online (Sandbox Code Playgroud)

PPS:var_dump数据更容易,找到所需的所有位,然后编写代码.