0

这是我用来在数组中获取 youtube 详细信息的代码:

$url = "http://gdata.youtube.com/feeds/api/videos/eRUbL_qSEQA?v=2&alt=json";
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

$output = curl_exec($ch); 
curl_close($ch);  

json_decode($output);

这是我从中得到的数组json_decode

stdClass Object ( [version] => 1.0 [encoding] => UTF-8 [entry] => stdClass Object ( [xmlns] => http://www.w3.org/2005/Atom [xmlns$media] => http://search.yahoo.com/mrss/ [xmlns$gd] => http://schemas.google.com/g/2005 [xmlns$yt] => http://gdata.youtube.com/schemas/2007 [gd$etag] => W/"C0ACQ347eCp7I2A9WhBbFUg." [id] => stdClass Object ( [$t] => tag:youtube.com,2008:video:eRUbL_qSEQA ) [published] => stdClass Object ( [$t] => 2013-03-26T09:12:27.000Z ) [updated] => stdClass Object ( [$t] => 2013-05-14T17:16:02.000Z ) [category] => Array ( [0] => stdClass Object ( [scheme] => http://schemas.google.com/g/2005#kind [term] => http://gdata.youtube.com/schemas/2007#video ) [1] => stdClass Object ( [scheme] => http://gdata.youtube.com/schemas/2007/categories.cat [term] => Movies [label] => Movies ) ) [title] => stdClass Object ( [$t] => Vamsodharakudu ) [content] => stdClass Object ( [type] => application/x-shockwave-flash [src] => http://www.youtube.com/v/eRUbL_qSEQA?version=3&f=videos&app=youtube_gdata ) [link] => Array ( [0] => stdClass Object ( [rel] => alternate [type] => text/html [href] => http://www.youtube.com/watch?v=eRUbL_qSEQA&feature=youtube_gdata ) [1] => stdClass Object ( [rel] => http://gdata.youtube.com/schemas/2007#video.responses [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/eRUbL_qSEQA/responses?v=2 ) [2] => stdClass Object ( [rel] => http://gdata.youtube.com/schemas/2007#video.related [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/eRUbL_qSEQA/related?v=2 ) [3] => stdClass Object ( [rel] => http://gdata.youtube.com/schemas/2007#video.trailers [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/eRUbL_qSEQA/trailers?v=2 ) [4] => stdClass Object ( [rel] => http://gdata.youtube.com/schemas/2007#mobile [type] => text/html [href] => http://m.youtube.com/details?v=eRUbL_qSEQA ) [5] => stdClass Object ( [rel] => http://gdata.youtube.com/schemas/2007#uploader [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/users/XjhJbviBl0M4JAC3cxDXqA?v=2 ) [6] => stdClass Object ( [rel] => self [type] => application/atom+xml [href] => http://gdata.youtube.com/feeds/api/videos/eRUbL_qSEQA?v=2 ) ) [author] => Array ( [0] => stdClass Object ( [name] => stdClass Object ( [$t] => teluguone ) [uri] => stdClass Object ( [$t] => http://gdata.youtube.com/feeds/api/users/teluguone ) [yt$userId] => stdClass Object ( [$t] => XjhJbviBl0M4JAC3cxDXqA ) ) ) [yt$accessControl] => Array ( [0] => stdClass Object ( [action] => comment [permission] => allowed ) [1] => stdClass Object ( [action] => commentVote [permission] => allowed ) [2] => stdClass Object ( [action] => videoRespond [permission] => moderated ) [3] => stdClass Object ( [action] => rate [permission] => allowed ) [4] => stdClass Object ( [action] => embed [permission] => allowed ) [5] => stdClass Object ( [action] => list [permission] => allowed ) [6] => stdClass Object ( [action] => autoPlay [permission] => allowed ) [7] => stdClass Object ( [action] => syndicate [permission] => allowed ) ) [yt$availability] => stdClass Object ( [start] => 2013-03-26T06:49:00.000Z [end] => 2037-03-26T12:00:00.000Z ) [gd$comments] => stdClass Object ( [gd$feedLink] => stdClass Object ( [rel] => http://gdata.youtube.com/schemas/2007#comments [href] => http://gdata.youtube.com/feeds/api/videos/eRUbL_qSEQA/comments?v=2 [countHint] => 0 ) ) [yt$firstReleased] => stdClass Object ( [$t] => 2000-01-14T08:00:00.000Z ) [yt$hd] => stdClass Object ( ) [media$group] => stdClass Object ( [media$category] => Array ( [0] => stdClass Object ( [$t] => Movies [label] => Movies [scheme] => http://gdata.youtube.com/schemas/2007/categories.cat ) [1] => stdClass Object ( [$t] => 1 [scheme] => http://gdata.youtube.com/schemas/2007/releasemediums.cat ) [2] => stdClass Object ( [$t] => 1 [scheme] => http://gdata.youtube.com/schemas/2007/mediatypes.cat ) [3] => stdClass Object ( [$t] => 18 [scheme] => http://gdata.youtube.com/schemas/2007/moviegenres.cat ) [4] => stdClass Object ( [$t] => 6 [scheme] => http://gdata.youtube.com/schemas/2007/moviegenres.cat ) [5] => stdClass Object ( [$t] => 8 [scheme] => http://gdata.youtube.com/schemas/2007/moviegenres.cat ) ) [media$content] => Array ( [0] => stdClass Object ( [url] => http://www.youtube.com/v/eRUbL_qSEQA?version=3&f=videos&app=youtube_gdata [type] => application/x-shockwave-flash [medium] => video [isDefault] => true [expression] => full [duration] => 9195 [yt$format] => 5 ) [1] => stdClass Object ( [url] => rtsp://v4.cache2.c.youtube.com/CiILENy73wIaGQkAEZL6LxsVeRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp [type] => video/3gpp [medium] => video [expression] => full [duration] => 9195 [yt$format] => 1 ) [2] => stdClass Object ( [url] => rtsp://v4.cache2.c.youtube.com/CiILENy73wIaGQkAEZL6LxsVeRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp [type] => video/3gpp [medium] => video [expression] => full [duration] => 9195 [yt$format] => 6 ) ) [media$credit] => Array ( [0] => stdClass Object ( [$t] => teluguone [role] => uploader [scheme] => urn:youtube [yt$display] => teluguone [yt$type] => partner ) [1] => stdClass Object ( [$t] => Paruchuri Brothers [role] => Writer [scheme] => urn:ebu ) [2] => stdClass Object ( [$t] => Dr. MS Reddy [role] => Producer [scheme] => urn:ebu ) [3] => stdClass Object ( [$t] => Sarat [role] => Director [scheme] => urn:ebu ) [4] => stdClass Object ( [$t] => Balakrishna [role] => Actor [scheme] => urn:ebu ) [5] => stdClass Object ( [$t] => Sakshi Sivanand [role] => Actor [scheme] => urn:ebu ) [6] => stdClass Object ( [$t] => Ramya Krishna [role] => Actor [scheme] => urn:ebu ) [7] => stdClass Object ( [$t] => Krishnam Raju [role] => Actor [scheme] => urn:ebu ) [8] => stdClass Object ( [$t] => Radhika [role] => Actor [scheme] => urn:ebu ) [9] => stdClass Object ( [$t] => Brahmanandam [role] => Actor [scheme] => urn:ebu ) ) [media$description] => stdClass Object ( [$t] => Suryam (Balaiah) is a physical director in a local college. His mother (Radhika) is a widow. The college in which Suryam work belongs to an Industrialist called Sudarshan Rao (Charan Raj). Sudarshan Rao is the brother-in-law of the business magnate Raja (Krishnam Raju). Raja has two sons, Brahmaji and Ravi Babu. Sudarshan Rao has a son, Srihari and a daughter, Saakshi. Saakshi is a student in the college. Satyabhama (Ramya) works as a junior PD (Physical Director) in the same college. Suryam and Saakshi used to have regular fights (amir heroine gareeb hero). After a few days they fall in love. Satyabhama too loves Suryam. When they visit 'Khajuraho' on a college tour, Satyabhama implicates Suryam that he tried to rape her. After a couple of scenes we come to know that Sudarshan Rao inducted Satyambhama into the college to malign the image of Suryam so that his daughter (Saakshi) hates Suryam in that process. Later on we come to know that Satya is the daughter of a conspirator Giribabu. She is asked to play the trick on Suryam in order to save her ailing father. Sudarshan Rao agrees for the marriage of Suryam and Sakshi. But he wants to know why the mother of Suryam is appearing as widow even though her husband is alive. Suryam caught unawares with this question, as he is not aware that his father is alive. Later on Suryam comes to know that Raja is his father. Suryam's mother used to work as a maid in Raja's house. When Raja gets involved in a murder of a thief, she takes up the responsibility and gets ready to go to the jail. Moved by the unconditional sacrifice done by his maid, Raja married his maid. Sudarshan Rao, brother in law of Raja, makes sure that the maid is thrown out of the house by doing manipulations. The maid comes out of the house and raises her kid (Suryam). After knowing the flashback, Suryam decides to go back to his father and win his laurels and there by set the house right. You gotta watch the film to know hoe Suryam accomplishes it, On Your TeluguOne [type] => plain ) [media$keywords] => stdClass Object ( ) [media$license] => stdClass Object ( [$t] => youtube [type] => text/html [href] => http://www.youtube.com/t/terms ) [media$player] => stdClass Object ( [url] => http://www.youtube.com/watch?v=eRUbL_qSEQA&feature=youtube_gdata_player ) [media$thumbnail] => Array ( [0] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/default.jpg [height] => 90 [width] => 120 [time] => 01:16:37.500 [yt$name] => default ) [1] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/mqdefault.jpg [height] => 180 [width] => 320 [yt$name] => mqdefault ) [2] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/hqdefault.jpg [height] => 360 [width] => 480 [yt$name] => hqdefault ) [3] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/1.jpg [height] => 90 [width] => 120 [time] => 00:38:18.750 [yt$name] => start ) [4] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/2.jpg [height] => 90 [width] => 120 [time] => 01:16:37.500 [yt$name] => middle ) [5] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/3.jpg [height] => 90 [width] => 120 [time] => 01:54:56.250 [yt$name] => end ) [6] => stdClass Object ( [url] => http://i.ytimg.com/vi/eRUbL_qSEQA/movieposter.jpg [height] => 243 [width] => 169 [yt$name] => poster ) ) [media$title] => stdClass Object ( [$t] => Vamsodharakudu [type] => plain ) [yt$aspectRatio] => stdClass Object ( [$t] => widescreen ) [yt$audioTracks] => stdClass Object ( [$t] => te ) [yt$duration] => stdClass Object ( [seconds] => 9195 ) [yt$uploaded] => stdClass Object ( [$t] => 2013-03-26T09:12:27.000Z ) [yt$uploaderId] => stdClass Object ( [$t] => UCXjhJbviBl0M4JAC3cxDXqA ) [yt$videoid] => stdClass Object ( [$t] => eRUbL_qSEQA ) ) [gd$rating] => stdClass Object ( [average] => 3 [max] => 5 [min] => 1 [numRaters] => 8 [rel] => http://schemas.google.com/g/2005#overall ) [yt$statistics] => stdClass Object ( [favoriteCount] => 0 [viewCount] => 6156 ) [yt$rating] => stdClass Object ( [numDislikes] => 4 [numLikes] => 4 ) ) ) 

如何获得观看次数、喜欢 n 不喜欢、评分、运行时间?

4

2 回答 2

1

将结果转换为数组,例如:

$result = json_decode($output, true); //adding true, objects is converted to array 
$viewCount = $result['entry']['yt$statistics']['viewCount']; //view count
$dislikes = $result['entry']['yt$rating']['numDislikes']; //dislikes
$likes = $result['entry']['yt$rating']['numLikes']; //likes
于 2013-06-11T03:43:23.473 回答
1

使用 preg 匹配和文件获取内容

if ( preg_match( '%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $_REQUEST['videourl'], $match ) ) {

              $videotype      = 'youtube';
              $videokey   = $match[1];
              $url                = "http://gdata.youtube.com/feeds/api/videos/" . $match[1] . "?q='youtube'&format=5&v=2&alt=jsonc";
              $json               = file_get_contents( $url, 0, null, null );
              $json_output        = json_decode( $json ); 
              foreach ( $json_output as $val ) {
                  //print_r($val);
                  $videotitle       = $val->title;
                  $videodescription = $val->description;
                  $videothumb       = "https://img.youtube.com/vi/".$videokey."/hqdefault.jpg";
              }

          } else if ( preg_match( '/^https:\/\/(www\.)?vimeo\.com\/(clip\:)?(\d+).*$/', $_REQUEST['videourl'], $match ) ) {

              $videotype             = 'vimeo';
              $videokey         = $match[3];
              $url                      = "http://vimeo.com/api/v2/video/" . $match[3] . ".php";
              $videoData                = unserialize( file_get_contents( $url ) );
              $videotitle      = $videoData[0]['title'];
              $videodescription = $videoData[0]['description'];
              $videothumb       = str_replace('http://', 'https://', $videoData[0]['thumbnail_medium']);
             //print "<pre>";print_r($videoData); print "</pre>";exit;
          }
于 2015-05-14T09:11:16.430 回答