Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
69
rated 0 times [  76] [ 7]  / answers: 1 / hits: 16513  / 12 Years ago, mon, march 12, 2012, 12:00:00

In one of my applications I am saving a YouTube video's id... Like "A4fR3sDprOE". I have to display its title in the application. I got the following code for getting its title and also it's working fine.


Now the problem is if any error occurred (in case of a delete of the video) PHP is showing an error. I hust added a condition. But still it's showing the 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 an error it's dying with the following:


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


More From » php

 Answers
2

It may work to use the error control operators before file_get_contents.


Something like:


if($content = @file_get_contents("http://youtube.com/get_video_info?video_id=" . $video_id))

It should remove the error and use it to return false in your if statement.


Else you can just use try/catch statement (see Exceptions):


try{
// Code
}
catch (Exception $e){
// Else code
}

[#86909] Saturday, March 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sageallenv

Total Points: 458
Total Questions: 102
Total Answers: 104

Location: Venezuela
Member since Thu, Jul 15, 2021
3 Years ago
;