Jump to content

get_headers() -- (php 5.2.2)


AquAvia

Recommended Posts

Hi group,

 

I have a small, but significant problem. My website is supposed to test a link to documents to determine wether to present the link, or present the link to a backup document residing on my server. This, obviously to satisfy my visitors. (100% click success rate) but limiting the bandwidth use on my server.

 

I was hoping to use the following code to do just the test:

 

<?php
$url = 'http://documentserver.com/documents/15311090_941_25.pdf';
print_r(get_headers($url, 1));
?>

 

Using the development server, I get the following response:

 

Array ( [0] => HTTP/1.0 200 OK [Content-Type] => text/html [Content-Location] => http://documentserver.com/Gyration404.htm?404;http://documentserver.com:80/documents/15311090_941_25.pdf [Last-Modified] => Mon, 14 May 2007 14:07:11 GMT [ETag] => "902fa32a3196c71:411" [server] => Microsoft-IIS/6.0 [X-Powered-By] => ASP.NET [Date] => Thu, 25 Sep 2008 00:38:53 GMT [Content-Length] => 227 [Connection] => close )

 

 

Now. obviously this is a dead link. However, since the documentserver is returning a 200 OK, I am currently presenting a dead link.... (sucks)

 

Testing for the Content-Type would be the obvious solution ==> application/pdf does not equal text/html

 

However.... I came to find: get_headers() is actually not supported..... or so it seems.

 

So... how to go about this? I need a function that takes a URL and returns "200 OK" and the filetype.... or return another code... if it's a redirect... returns the new URL (and the 300 code so I can update my database of URL's). a 404 obviously would be a permanent removal of the URL from the database and permanently switch to backup-document.

 

All i need is the tester. not the database handling. that's easy.

 

I am also not asking for the complete codesnippet.... (wouldn't mind) but would love some examples.

 

Anyone.... code giants?

 

Warmest regards,

 

Michel

Link to comment
Share on other sites

Yes, my live server is also 5.2.2.

 

php.ini is custom and has the allow_fopen enabled (on).

 

Did not check for curl as I chose this provider partially because of their php version (startlogic.com -> they support php 4 and 5; I have 5 setup as default version and can use both versions by file extension .php4 or .php5. However, I use .php and do not currently care for using .php4 any time soon, unless I have to. (mixing the two would make my coding messy and therefore confuse me a few months or years down the line)

 

However, if there's no other way, I will try.... :S

 

Ideas?

 

Here's the actual code:

 

<?php
function ping($url) {
	$headers = get_headers($url);
	if (is_array($headers)) {
		switch ($headers[0]) {
			case 'HTTP/1.1 200 OK':
			case 'HTTP/1.0 302 Found':
			print $headers[8]."< /br>"; // Just here to see document-type
				return('Y');
			default:
				return('N');
		}
	} else {
		return('N');
	}
}
?>

 

Does not work as is.... and when it does... I will add the document-type check. (then working with key names instead of the currently, shitty array index numbers)

 

Works on my server, but obviously also returns a "Y" even if the document has been 200 OK-ed but really not of the right doc type.

 

:S

 

Warm regards,

 

Michel

 

 

Link to comment
Share on other sites

I tried the following on my xampp server:

<?php
$urls = array('put','your','urls','here');
function ping($url) {
	$headers = @get_headers($url,1);
	if (is_array($headers)) {
		switch ($headers[0]) {
			case 'HTTP/1.1 200 OK':
			case 'HTTP/1.0 302 Found':
			case 'HTTP/1.0 200 OK':
				return(array(true,$headers));
			default:
				return(array(false,$headers));
		}
	} else {
		return(array(false,array()));
	}
}

foreach ($urls as $url) {
echo $url . ' ... ';
list($ret,$retary) = ping($url);
if ($ret) echo '<pre>' . print_r($retary,true) . '</pre>';
else echo 'Not ok<pre>' . print_r($retary,true) . '</pre>';
echo '<br>';
}
?>

This seems to work...

 

Ken

Link to comment
Share on other sites

I think his original problem is that get_headers() returns nothing on the live server.  And the other original problem is that the result code does not indicate the actual result, as the server returns 200 for a 404.

 

AquAvia, is the second argument to get_headers() an issue?  Should it be 1?  In your second post it is missing.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.