What Is the Easiest Way to Use the Head Command of Http in PHP

What is the easiest way to use the HEAD command of HTTP in PHP?

As an alternative to curl you can use the http context options to set the request method to HEAD. Then open a (http wrapper) stream with these options and fetch the meta data.

$context  = stream_context_create(array('http' =>array('method'=>'HEAD')));
$fd = fopen('http://php.net', 'rb', false, $context);
var_dump(stream_get_meta_data($fd));
fclose($fd);

see also:

http://docs.php.net/stream_get_meta_data

http://docs.php.net/context.http

How to handle HEAD request?

Where you have this data ? try :

echo $_GET['merchantId'];  // or
echo $_REQUEST['merchantId'];

Otherwise you can get it with parse_str :

$str = "first=value&arr[]=foo+bar&arr[]=baz";
parse_str($str, $output);
echo $output['first']; // value
echo $output['arr'][0]; // foo bar
echo $output['arr'][1]; // baz

PHP get_headers() to generate the header file

Your command line invocation using curl is following the 301 redirect and then returning the headers of the redirected page. For a more reliable approach using curl from PHP I suggest you use a variation of my answer over here:

Can PHP cURL retrieve response headers AND body in a single request?

If you wish to transparently follow the redirect as curl is doing on the command line, set the curl option CURLOPT_FOLLOWLOCATION, for example:

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

Since you are not interested in the body, turn off the return transfer option, ie:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);

If you wish to perform a HEAD request set the following option also:

curl_setopt($ch, CURLOPT_NOBODY, true);

Detecting request type in PHP (GET, POST, PUT or DELETE)

By using

$_SERVER['REQUEST_METHOD']

Example

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// The request is using the POST method
}

For more details please see the documentation for the $_SERVER variable.

PHP / Curl: HEAD Request takes a long time on some sites

Try simplifying it a little bit:

print htmlentities(file_get_contents("http://www.arstechnica.com"));

The above outputs instantly on my webserver. If it doesn't on yours, there's a good chance your web host has some kind of setting in place to throttle these kind of requests.

EDIT:

Since the above happens instantly for you, try setting this curl setting on your original code:

curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);

Using the tool you posted, I noticed that http://www.arstechnica.com has a 301 header sent for any request sent to it. It is possible that cURL is getting this and not following the new Location specified to it, thus causing your script to hang.

SECOND EDIT:

Curiously enough, trying the same code you have above was making my webserver hang too. I replaced this code:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'

With this:

curl_setopt($ch, CURLOPT_NOBODY, true);

Which is the way the manual recommends you do a HEAD request. It made it work instantly.

PHP, cURL, and HTTP POST example?

<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");

// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));

// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec($ch);

curl_close($ch);

// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>

Is it possible to find the right server by spending less time?

Try this,
What is the easiest way to use the HEAD command of HTTP in PHP?

you will get HTTP Response Code without BODY. It will save time

Easiest way to grab filesize of remote file in PHP?

Yes. Since the file is remote, you're completely dependent on the value of the Content-Length header (unless you want to download the whole file). You'll want to curl_setopt($ch, CURLOPT_NOBODY, true) and curl_setopt($ch, CURLOPT_HEADER, true).



Related Topics



Leave a reply



Submit