killsite Posted February 7, 2008 Share Posted February 7, 2008 Trying to get a simple test script that will retreive the http headers from the server and output it nicely. Right now I'm using the following.. <?php $url = 'http://www.example.com'; print_r(get_headers($url)); print_r(get_headers($url, 1)); ?> But the output is very very messy.. Array ( [0] => HTTP/1.1 200 OK [1] => Date: Wed, 06 Feb 2008 20:21:36 GMT [2] => Server: Apache/2.2.3 (CentOS) [3] => Last-Modified: Tue, 15 Nov 2005 13:24:10 GMT [4] => ETag: "280100-1b6-80bfd280" [5] => Accept-Ranges: bytes [6] => Content-Length: 438 [7] => Connection: close [8] => Content-Type: text/html; charset=UTF-8 ) Array ( [0] => HTTP/1.1 200 OK [Date] => Wed, 06 Feb 2008 20:21:37 GMT [server] => Apache/2.2.3 (CentOS) [Last-Modified] => Tue, 15 Nov 2005 13:24:10 GMT [ETag] => "280100-1b6-80bfd280" [Accept-Ranges] => bytes [Content-Length] => 438 [Connection] => close [Content-Type] => text/html; charset=UTF-8 ) In the end I want to add this to a test script I'm developing so I'd like the output to be more structured but I'm not sure how to accomplish this with the above code which I got from http://us3.php.net/get_headers. From this page, that's the only code I've gotten to work. The others listed don't have a place for me to enter the URL so I'm assuming it's going to grab the data by itself. I'm running php5 on apache2. Any help on this would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/ Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 echo "<pre>"; print_r("some stuff here"); echo "</pre>"; Or alternatively parse the result into an array and loop/echo the information in a table or otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460686 Share on other sites More sharing options...
killsite Posted February 7, 2008 Author Share Posted February 7, 2008 Tried the above but the output I get is the same. Groups the output in one liners. If anything I'd like it to spit it out like so, just without the "[#] => ". [0] => HTTP/1.1 200 OK [1] => Date: Sat, 29 May 2004 12:28:13 GMT [2] => Server: Apache/1.3.27 (Unix) (Red-Hat/Linux) [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT [4] => ETag: "3f80f-1b6-3e1cb03b" [5] => Accept-Ranges: bytes [6] => Content-Length: 438 [7] => Connection: close [8] => Content-Type: text/html Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460699 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 the echo statements i provided above were supposed to be < pre > html tags, unfortunately this forum likes to process those Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460706 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 if you want it without the => then i suggest you do some legwork and actually parse the results into a table Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460707 Share on other sites More sharing options...
kenrbnsn Posted February 7, 2008 Share Posted February 7, 2008 the echo statements i provided above were supposed to be < pre > html tags, unfortunately this forum likes to process those That is why you should always surround any code you post on this forum with tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460711 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 <pre style=height:100px;width:100px; onmouseover=javascript:alert(String.fromCharCode(88,83,83))>Haha true, but then it was so small i didn't think it really warranted it, especially seeing as the format didn't need to be indented. Nevertheless i covered what i was attempting in the following post.</pre> Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460719 Share on other sites More sharing options...
nethnet Posted February 7, 2008 Share Posted February 7, 2008 The beauty of foreach loops... <?php $headers = get_headers($url); foreach($headers AS $key => $value) echo "[$key]: $value<br />"; ?> Theo Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460720 Share on other sites More sharing options...
haku Posted February 7, 2008 Share Posted February 7, 2008 What is it that you want to output the headers for? If its just for your own personal reference, then I would suggest using the firefox plug in 'Live http headers'. It lets you see the headers in realtime. There is one for IE as well under a different name. I cant remember what its called, but you can find it somewhere in this page. Its where I first read about it. Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460724 Share on other sites More sharing options...
killsite Posted February 7, 2008 Author Share Posted February 7, 2008 Thanks. This worked for me. As for parsing the data into a table to remove the array sequence, I'll look around for something. Why on http://us3.php.net/get_headers did the generic code I used go from that to a whole string of other stuff I can't even read. All of the others break when I try them, however that page suggests you can do more (SSL support, more http headers, filtered arrays, etc etc). I have a feeling this will end up rhetorical in that my own lack of knowledge about php is causing it to break. On that note, as I'm a newbie, I know php in the sense that I can modify a pre-existing script to my liking but I am clueless when it comes to generating my own from scratch. I'll be looking for related topics in this forum to see what else I can learn. This site is really great but I don't know why the forum labeled 'PHP Tutorial Help' has users posting questions about problems they're running into. Should I have posted my topic there instead? I'd love to see that forum with a really long sticky covering the very basics for complete beginners, then another for intermediates, then another for advanced. This way ppl like me will have a place they can go or be directed to learn about php, its structure, and coding practices. I know there are many resources on the net but take for example when I visit http://us3.php.net/manual/en/function.array-filter.php .. it shows info about array filter. Problem is I can't read any of that because I'm still unfamiliar with phps structure and other variables. For right now, I need something in lamens I guess.. lol. Also, haku.. i just read your post. I am using live http headers for FF. I'm building a test script for my site which I want to include as much server data as I can. I have a MySQL tests which will spit out the MySQL runtime info, variables & settings along with the tables of any db I choose or configure. phpinfo is included. With http headers I can get the apache/perl versions and it will help me to see how apache (in general) responds. With that, get requests for headers is standard. With any web host, server behaviour and versions can change and this is the main reason why. Eventually I'll include more tools on the test page but for now the http headers is what I'm focusing on. Thanks for the inquiry. Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460739 Share on other sites More sharing options...
kenrbnsn Posted February 7, 2008 Share Posted February 7, 2008 Why on http://us3.php.net/get_headers did the generic code I used go from that to a whole string of other stuff I can't even read The example is meant to be run from the CLI, not in a browser. When you send output to a browser, the browser uses "<br>" to go to a newline. The print_r() function terminates each line with a newline character, usually "\n", which works fine in the CLI. To make the display look right in a browser, the usual way of doing that is to wrap the output in <pre></pre> tags: <?php $url = 'http://www.example.com'; echo '<pre>' . print_r(get_headers($url),true) . '</pre>'; echo '<pre>' . print_r(get_headers($url, 1),true) . '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460755 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 hence my original answer... Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460758 Share on other sites More sharing options...
kenrbnsn Posted February 7, 2008 Share Posted February 7, 2008 Yes, you're original answer showed how to do it correctly, but I was explaining why in answer to the question I quoted. Ken Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460775 Share on other sites More sharing options...
aschk Posted February 7, 2008 Share Posted February 7, 2008 Touche Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460779 Share on other sites More sharing options...
killsite Posted February 7, 2008 Author Share Posted February 7, 2008 Q. Why on http://us3.php.net/get_headers did the generic code I used go from that to a whole string of other stuff I can't even read A. The example is meant to be run from the CLI, not in a browser. When you send output to a browser, the browser uses "<br>" to go to a newline. The print_r() function terminates each line with a newline character, usually "\n", which works fine in the CLI. To make the display look right in a browser, the usual way of doing that is to wrap the output in <pre></pre> tags: Overall this helps in my understanding. So essentially if I can get a php script to output the data I need -first-.. it's up to me to reformat using html or additional php variables to re-structure or filter as I want. Is this right? Also, with regards to that question, it was actually aimed more for the scripts on that site. Example is if you look at that page it shows the generic template I used and modified: http://us3.php.net/manual/en/function.get-headers.php <?php $url = 'http://www.example.com'; print_r(get_headers($url)); print_r(get_headers($url, 1)); ?> It just went from that to other variations.. none keeping the $url = 'http://www.example.com'; option so it had me completely lost bc for one, none of the others worked then two Where is it breaking? Should I be modifying something? Why does php not output any error? What the heck is the script doing?? Naturally I combed the code but I'm not nearly as knowledgable about php to disect line for line what it's doing. I can only get an idea based on the variables being used but mostly I'm lost. Below are just a few from that site... Example 1. <?php function get_headers_x($url,$format=0, $user='', $pass='', $referer='') { if (!empty($user)) { $authentification = base64_encode($user.':'.$pass); $authline = "Authorization: Basic $authentification\r\n"; } if (!empty($referer)) { $refererline = "Referer: $referer\r\n"; } $url_info=parse_url($url); $port = isset($url_info['port']) ? $url_info['port'] : 80; $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30); if($fp) { $head = "GET ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\n"; if (!empty($url_info['port'])) { $head .= "Host: ".@$url_info['host'].":".$url_info['port']."\r\n"; } else { $head .= "Host: ".@$url_info['host']."\r\n"; } $head .= "Connection: Close\r\n"; $head .= "Accept: */*\r\n"; $head .= $refererline; $head .= $authline; $head .= "\r\n"; fputs($fp, $head); while(!feof($fp) or ($eoheader==true)) { if($header=fgets($fp, 1024)) { if ($header == "\r\n") { $eoheader = true; break; } else { $header = trim($header); } if($format == 1) { $key = array_shift(explode(':',$header)); if($key == $header) { $headers[] = $header; } else { $headers[$key]=substr($header,strlen($key)+2); } unset($key); } else { $headers[] = $header; } } } return $headers; } else { return false; } } ?> Example 2. <?php function my_get_headers($url ) { $url_info=parse_url($url); if (isset($url_info['scheme']) && $url_info['scheme'] == 'https') { $port = 443; @$fp=fsockopen('ssl://'.$url_info['host'], $port, $errno, $errstr, 10); } else { $port = isset($url_info['port']) ? $url_info['port'] : 80; @$fp=fsockopen($url_info['host'], $port, $errno, $errstr, 10); } if($fp) { stream_set_timeout($fp, 10); $head = "HEAD ".@$url_info['path']."?".@$url_info['query']; $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n"; fputs($fp, $head); while(!feof($fp)) { if($header=trim(fgets($fp, 1024))) { $sc_pos = strpos( $header, ':' ); if( $sc_pos === false ) { $headers['status'] = $header; } else { $label = substr( $header, 0, $sc_pos ); $value = substr( $header, $sc_pos+1 ); $headers[strtolower($label)] = trim($value); } } } return $headers; } else { return false; } } ?> Example 3. <?php if(!function_exists('get_headers')) { function get_headers($url,$format=0) { $url=parse_url($url); $end = "\r\n\r\n"; $fp = fsockopen($url['host'], (empty($url['port'])?80:$url['port']), $errno, $errstr, 30); if ($fp) { $out = "GET / HTTP/1.1\r\n"; $out .= "Host: ".$url['host']."\r\n"; $out .= "Connection: Close\r\n\r\n"; $var = ''; fwrite($fp, $out); while (!feof($fp)) { $var.=fgets($fp, 1280); if(strpos($var,$end)) break; } fclose($fp); $var=preg_replace("/\r\n\r\n.*\$/",'',$var); $var=explode("\r\n",$var); if($format) { foreach($var as $i) { if(preg_match('/^([a-zA-Z -]+): +(.*)$/',$i,$parts)) $v[$parts[1]]=$parts[2]; } return $v; } else return $var; } } } ?> Example 4. <? function get_headers2($url,$format=0) { $url_info=parse_url($url); $port = isset($url_info['port']) ? $url_info['port'] : 80; $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30); if($fp) { if(!$url_info['path']){ $url_info['path'] = "/"; } if($url_info['path'] && !$url_info['host']){ $url_info['host'] = $url_info['path']; $url_info['path'] = "/"; } if( $url_info['host'][(strlen($url_info['host'])-1)] == "/" ){ $url_info['host'][(strlen($url_info['host'])-1)] = ""; } if(!$url_array[scheme]){ $url_array[scheme] = "http"; //we always use http links } $head = "HEAD ".@$url_info['path']; if( $url_info['query'] ){ $head .= "?".@$url_info['query']; } print_r($url_info); $head .= " HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n"; echo $head; fputs($fp, $head); while(!feof($fp)) { if($header=trim(fgets($fp, 1024))) { if($format == 1) { $h2 = explode(':',$header); // the first element is the http header type, such as HTTP/1.1 200 OK, // it doesn't have a separate name, so we have to check for it. if($h2[0] == $header) { $headers['status'] = $header; } else { $headers[strtolower($h2[0])] = trim($h2[1]); } } else { $headers[] = $header; } } } return $headers; } else { return false; } } ?> Example 5. <?php if(!function_exists('get_headers')) { /** * @return array * @param string $url * @param int $format * @desc Fetches all the headers * @author cpurruc fh-landshut de * @modified by dotpointer * @modified by aeontech */ function get_headers($url,$format=0) { $url_info=parse_url($url); $port = isset($url_info['port']) ? $url_info['port'] : 80; $fp=fsockopen($url_info['host'], $port, $errno, $errstr, 30); if($fp) { $head = "HEAD ".@$url_info['path']."?".@$url_info['query']." HTTP/1.0\r\nHost: ".@$url_info['host']."\r\n\r\n"; fputs($fp, $head); while(!feof($fp)) { if($header=trim(fgets($fp, 1024))) { if($format == 1) { $key = array_shift(explode(':',$header)); // the first element is the http header type, such as HTTP 200 OK, // it doesn't have a separate name, so we have to check for it. if($key == $header) { $headers[] = $header; } else { $headers[$key]=substr($header,strlen($key)+2); } unset($key); } else { $headers[] = $header; } } } return $headers; } else { return false; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460857 Share on other sites More sharing options...
killsite Posted February 7, 2008 Author Share Posted February 7, 2008 Also, I tried looking for ways to format the output so that it's more friendly. As far as I know of, there isn't anyway to -remove- the additional terms and characters included such as 'Array', '(', '[' and so forth. I'm sure there's a way to do this using php but I don't know. Essentially I want is to go from: This: Array ( [0] => HTTP/1.1 200 OK [1] => Date: Thu, 07 Feb 2008 12:33:04 GMT [2] => Server: Apache/2.2.6 (Unix) PHP/5.2.5 with Suhosin-Patch mod_ssl/2.2.6 OpenSSL/0.9.7m mod_apreq2-20051231/ [3] => Last-Modified: Tue, 13 Nov 2007 13:52:22 GMT [4] => ETag: "2ae3241-6d-c212b180" [5] => Accept-Ranges: bytes [6] => Content-Length: 109 [7] => Connection: close [8] => Content-Type: text/html ) That: HTTP/1.1 200 OK Date: Thu, 07 Feb 2008 12:33:04 GMT Server: Apache/2.2.6 (Unix) PHP/5.2.5 with Suhosin-Patch mod_ssl/2.2.6 OpenSSL/0.9.7m mod_apreq2-20051231/ Last-Modified: Tue, 13 Nov 2007 13:52:22 GMT ETag: "2ae3241-6d-c212b180" Accept-Ranges: bytes Content-Length: 109 Connection: close Content-Type: text/html Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-460936 Share on other sites More sharing options...
killsite Posted February 11, 2008 Author Share Posted February 11, 2008 wow, no response from my last two posts? Last post if anybody wants to add. Quote Link to comment https://forums.phpfreaks.com/topic/89888-get_headers/#findComment-463897 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.