Get headers by using curl in php

Votes : 3.6
1895
You can get any sites,pages or files headers by using get_headers(...) function in PHP.

Why do needed to get headers?

Sometimes you will have to check a site,page or file existing in online server, get a file or a page length or content type. So try to download or get content by using curl or file_get_content() function to get these details. If it is a large file or page, It will be unnecessary thing. But these details allready contain in header. Header is smaller content. why do wasting time to download page or file?

So i wrote this article explain to execute get_headers() function.
First, see this example of below, print_r(get_headers('url')); /* Array ( [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 ) */ print_r(get_headers('url', 1)); /* Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 29 May 2004 12:28:14 GMT [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux) [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT [ETag] => "3f80f-1b6-3e1cb03b" [Accept-Ranges] => bytes [Content-Length] => 438 [Connection] => close [Content-Type] => text/html ) */ get_headers function not work sometimes in server, but you can get headers by using curl. So i wrote a code to get same headers results like get_headers return parameters. I think it is helpful for you. function curl_getheaders($url) { /* * Get headers by using curl * @param (string|optional) $url - The target absolute url of site,page or file. * * @return (array) headers. * * source path : http://codrate.com/ */ $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_HEADER => true, CURLOPT_NOBODY => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_URL => $url)); $headers = array(); foreach (explode("\n", curl_exec($curl)) as $key => $header) { if (!$key) { $headers[] = $header; } else { $header = explode(':', $header); $headers[trim($header[0])] = isset($header[1]) ? trim($header[1]) : ''; } } curl_close($curl); return count($headers) < 2 ? false : $headers; } print_r(curl_getheaders('url')); /* Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 29 May 2004 12:28:14 GMT [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux) [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT [ETag] => "3f80f-1b6-3e1cb03b" [Accept-Ranges] => bytes [Content-Length] => 438 [Connection] => close [Content-Type] => text/html ) */
asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila