问题
In one of my application I am saving youtube video's id... Like "A4fR3sDprOE" .. I have to display its title in application. I got the following code for getting its title and also its working fine.
Now the problem is if any error occured ( in case of delete the video ) php showing a error. I hust added a condition. But still its showing error.
foreach($videos as $video) {
$video_id = $video->videos;
if($content=file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id)) {
parse_str($content, $ytarr);
$myvideos[$i]['video_title']=$ytarr['title'];
}
else
$myvideos[$i]['video_title']="No title";
$i++;
}
return $myvideos;
In case of error its dying with the folllowing
Severity: Warning
Message: file_get_contents(http://youtube.com/get_video_info?video_id=A4fR3sDprOE) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 402 Payment Required
Filename: models/webs.php
Line Number: 128
Please help
回答1:
does it work to use the @ (php-manual) before file_get_contents?
something like:
if($content = @file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id))
should remove the error and use it to return false in your if statement
else you can just use try/catch statement (php-manual)
try{
// code
}catch (Exception $e){
// else code
}
回答2:
It's not safe to use file_get_contents() with remote URLs. Use cURL instead with Youtube API 2.0:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/'.$video_id);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
$xml = new SimpleXMLElement($response);
$title = (string) $xml->title;
} else {
// Error handling.
}
回答3:
This is my solution. It´s very short.
$id = "VIDEO ID";
$videoTitle = file_get_contents("http://gdata.youtube.com/feeds/api/videos/${id}?v=2&fields=title");
preg_match("/<title>(.+?)<\/title>/is", $videoTitle, $titleOfVideo);
$videoTitle = $titleOfVideo[1];
回答4:
I believe your hosting provider disabled file_get_contents for security purposes. You should use CURL.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://youtube.com/get_video_info?video_id=".$video_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
/* PARSE YOUTUBE'S RESPONSE AS YOU LIKE BELOW */
?>
回答5:
Try:
//use @ to supress warnings
$content=@file_get_contents("http://youtube.com/get_video_info?video_id=".$video_id);
if($content===FALSE) {
..handle error here
}
else{
..rest of your code
}
回答6:
I would like to add to this that the specific error seen here (HTTP 402 - Payment Required, which is otherwise a HTTP status code that servers generally don't have implemented) is what YouTube returns when it has determined that an "excessive" amount of traffic has been coming from your IP address (or IP address range -- they do rather enjoy blocking OVH's IP ranges on a frequent basis[1][2]).
Therefore, if you're accessing youtube.com (not the API) programmatically (using PHP or otherwise), you may eventually find yourself hitting this error. YouTube generally starts off by presenting CAPTCHAs to requests from "excessive traffic" IPs, but if you aren't completing them (which your PHP script won't be), they will switch to these unhelpful 402 responses with essentially no recourse - YouTube doesn't exactly have a customer support desk to call, and if you can't actually access any of their website because of the IP blocking, you have even less chance of contacting them.
Other references:
http://productforums.google.com/forum/#!topic/youtube/tR4WkNBPnUo
http://productforums.google.com/forum/?fromgroups=#!topic/youtube/179aboankVg
回答7:
My solution is:
$xmlInfoVideo = simplexml_load_file("http://gdata.youtube.com/feeds/api/videos/".$videoId."?v=2&fields=title");
foreach($xmlInfoVideo->children() as $title) { $videoTitle = strtoupper((string) $title); }
This get the title of the video.
来源:https://stackoverflow.com/questions/9664482/get-youtube-video-title-in-php