Validating a link in php

This is a discussion on Validating a link in php within the PHP General forums, part of the PHP Programming Forums category; Hi all, I'm hoping y'all can help me with a bit of a php code problem I'm ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-01-1970
sleestak2@earthlink.net
 
Posts: n/a
Default Validating a link in php

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
Reply With Quote
  #2 (permalink)  
Old 01-27-2007
Myron Turner
 
Posts: n/a
Default Re: Validating a link in php

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/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:28 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0