get_headers คำสั่งที่มีประโยชน์มากๆ ใน PHP5

PHP5 ที่เราใช้นั้น เท่าที่ผมเห็น และพิสูจน์ด้วยตัวเอง มันมีข้อดีกว่า PHP4 มากๆ นอกเหนือจากการสนับสนุน OOP เต็มรูปแบบแล้ว ยังเพิ่ม built in function อีกมากมาย หนึ่งในนั้นก็คือคำสั่ง get_headers ที่ผมช้อบชอบ

ผมเห็นหลายๆคนยังชอบเช็ค file ที่มาจากถายนอกว่ามี หนือไม่ ด้วยคำสั่ง $fopen($url, ‘r’) ซึ่งผมบอกได้เลยว่า มันกินทรัพยากรเครื่องมาก ขนาดทำให้ล่มได้ด้วยคำสั่งเดียวเลยล่ะ (โครตน่ากลัว) เจอกับตัวเองมาแล้ว

ถ้าคุณเป็นคนนึงที่ใช้ PHP5 ในการพัฒนาเวบไซด์แล้วล่ะก็ เปลี่ยนมาใช้คำสั่งนี้กันดีกว่า

  1. <?php
  2. $url = ‘http://www.example.com’;
  3.  
  4. print_r(get_headers($url));
  5.  
  6. print_r(get_headers($url, 1));
  7. ?>

ซึ่งผลที่ได้คืนมาจะอยู่ในรูปแบบ array ถ้าใส่ parameter 1 เข้าไปจะคืนมาในลักษณะ ของ associate array

ดังนี้

  1.  
  2. (
  3.     [0] => HTTP/1.1 200 OK
  4.     [1] => Date: Sat, 29 May 2004 12:28:13 GMT
  5.     [2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
  6.     [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
  7.     [4] => ETag: "3f80f-1b6-3e1cb03b"
  8.     [5] => Accept-Ranges: bytes
  9.     [6] => Content-Length: 438
  10.     [7] => Connection: close
  11.     [8] => Content-Type: text/html
  12. )
  13.  
  14. (
  15.     [0] => HTTP/1.1 200 OK
  16.     [Date] => Sat, 29 May 2004 12:28:14 GMT
  17.     [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
  18.     [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
  19.     [ETag] => "3f80f-1b6-3e1cb03b"
  20.     [Accept-Ranges] => bytes
  21.     [Content-Length] => 438
  22.     [Connection] => close
  23.     [Content-Type] => text/html
  24. )
  25.  

ดังนี้เราก็สามารถตรวจสอบรายละเอียด ของ url ที่เราไปเรียกได้แล้ว โดยไม่ต้องใช้คำสั่ง ฆ่าตัวตาย แบบตอนแรก
HTTP/1.1 200 OK

ตัวข้างบนจะเป็นการบอก status ของ url ที่เราไปเรียก โดย 200 คือ การตรวจสอบแล้วว่าไฟล์มีจริง สามารถใช้งานได้

แต่ถ้าคุณยัยืนยันที่จะใช้ PHP4 เหมือนเดิม ผมก็ขอแนะนำให้เขียนแบบนี้จะดีกว่า

  1. <?php
  2. if(!function_exists(‘get_headers’))
  3. {
  4.     function get_headers($url,$format=0)
  5.     {
  6.         $url=parse_url($url);
  7.         $end = "\r\n\r\n";
  8.         $fp = fsockopen($url[‘host’], (empty($url[‘port’])?80:$url[‘port’]), $errno, $errstr, 30);
  9.         if ($fp)
  10.         {
  11.             $out  = "GET / HTTP/1.1\r\n";
  12.             $out .= "Host: ".$url[‘host’]."\r\n";
  13.             $out .= "Connection: Close\r\n\r\n";
  14.             $var  = ;
  15.             fwrite($fp, $out);
  16.             while (!feof($fp))
  17.             {
  18.                 $var.=fgets($fp, 1280);
  19.                 if(strpos($var,$end))
  20.                     break;
  21.             }
  22.             fclose($fp);
  23.  
  24.             $var=preg_replace("/\r\n\r\n.*\$/",,$var);
  25.             $var=explode("\r\n",$var);
  26.             if($format)
  27.             {
  28.                 foreach($var as $i)
  29.                 {
  30.                     if(preg_match(‘/^([a-zA-Z -]+): +(.*)$/’,$i,$parts))
  31.                         $v[$parts[1]]=$parts[2];
  32.                 }
  33.                 return $v;
  34.             }
  35.             else
  36.                 return $var;
  37.         }
  38.     }
  39. }
  40. ?>

แต่มันก็ยังกิน resource มากกว่าเยอะ อยู่ดี -*-

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments »

 
 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>