I have written small code snippet for getting information from Youtube by PHP with cURL!
The code itself is nothing special, really straightforward manner.
The following getVideoInfo function takes youtube video id, access to youtube api and return back the detail video information.
The code itself is nothing special, really straightforward manner.
The following getVideoInfo function takes youtube video id, access to youtube api and return back the detail video information.
function getVideoInfo($id) { try { $req = request('http://gdata.youtube.com/feeds/api/videos/' . $id); $xmlStr = $req->getResponseData(); $xml = simplexml_load_string($xmlStr); $xml->registerXPathNamespace('x', 'http://www.w3.org/2005/Atom'); $xml->registerXPathNamespace('media', 'http://search.yahoo.com/mrss/'); $xml->registerXPathNamespace('yt', 'http://gdata.youtube.com/schemas/2007'); $title = $xml->xpath('/x:entry/media:group/media:title'); $duration_seconds = $xml->xpath('/x:entry/media:group/yt:duration/@seconds'); if (empty($title) || empty($duration_seconds)) { throw new Exception('Could not get the fields.'); } return array('title' => (string) $title[0], 'duration_seconds' => (string) $duration_seconds[0]); } catch (Exception $e) { throw $e; } } function request($url, $timeout = 30) { //cURL initilize sessosion $curl = curl_init(); //URL curl_setopt($curl, CURLOPT_URL, $url); // hmm option might be too much? curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); $ret = curl_exec($curl); curl_close($curl); return $ret; }
コメント