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/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
代码示例(根据要求)
所以我在做的是:
这是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>";
Run Code Online (Sandbox Code Playgroud)
得到你的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数据更容易,找到所需的所有位,然后编写代码.