sleestak2@earthlink.net wrote:
> Hi all,
>
> I'm hoping y'all can help me with a bit of a php code problem I'm having.
>
> What I'm trying to do is link to YouTube videos (more than one in a single
> webpage). However, because I'm worried about the videos going away and the
> links going bad, I'd like to use PHP to check if the link is good; if it's
> good, display the video link, else display a static image that I have on my
> servers.
>
> Where I'm having troubles is in the checking of whether the url is good. I
> think part of the problem is YouTube itself. I've tried file() and
> fsockopen(), but neither seem to work.
>
> Below is an example of the block of code I'm trying to write.
>
> <?
> error_reporting(E_ALL ^ E_NOTICE);
> if (the "http://www.youtube.com/v/JqO8ZevPJNk" url is valid) <-- ???
> {
> echo " <object height=\"400\" width=\"475\">\n";
> echo " <param name=\"movie\" value=\"http://www.youtube.com/v/JqO8ZevPJNk\">\n";
> echo " <embed src=\"http://www.youtube.com/v/JqO8ZevPJNk\"\n";
> echo " type=\"application/x-shockwave-flash\" height=\"400\" width=\"475\">\n";
> echo " </object>\n";
> }
> else
> {
> echo " <a href=\"10538overturevideo.jpg\"><img SRC=\"10538overturevideo.jpg\" height=\"130\"></a>\n";
> }
> ?>
>
> Many thanks for any assistance you can provide.
>
> Robert Porter
You want to make a request for the headers, not for the file. If you
get a 200 OK response header, then the file exists.
<?php
$fp = fsockopen("www.youtube.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "HEAD
http://www.youtube.com/v/JqO8ZevPJNk / HTTP/1.1\r\n";
$out .= "Host: www.youtube.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$file_exists = false;
while (!feof($fp)) {
$header = fgets($fp, 128);
if(preg_match('#HTTP/1.1 200 OK#', $header)) {
$file_exists = true;
break;
}
}
fclose($fp);
if($file_exists) echo $header;
}
?>
--
_____________________
Myron Turner
http://www.room535.org
http://www.bstatzero.org
http://www.mturner.org/XML_PullParser/