0

I have a code that sends a request to a PHP page to get it's headers. The thing is, on that page, copy() function is executed and cURL either waits for the whole page to load (finish copying) or returns false if I set timeout to 2-3 seconds. How do I get page headers without waiting for copy() function to finish doing it's job?

My code so far is:

$req='page_with_copy_function_in_it.php';
$ch=curl_init($req);
curl_setopt($ch,CURLOPT_NOBODY,true);
curl_setopt($ch,CURLOPT_HEADER,true);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,2);
$data=curl_exec($ch);
curl_close($ch);
  • possible duplicate of [Header only retrieval in php via curl](http://stackoverflow.com/questions/1378915/header-only-retrieval-in-php-via-curl) – Mp0int Apr 27 '13 at 13:52
  • Apache is forced to execute the whole php page (because it may override the http headers), so it takes long even if you request only header. If the `page_with_copy_function_in_it.php` page is yours, you should edit it instead. – Alain Tiemblo Apr 27 '13 at 13:57
  • @Ninsuo yes, that page is mine. Any idea how could I edit it to get its headers and still be able to use copy()? – user2036968 Apr 27 '13 at 14:02

2 Answers2

2

You should use a HEAD request if you don't want to load the page content.

From PHP Doc

CURLOPT_NOBODY: Set TRUE to exclude the body from the output. Request method is then set to HEAD. Changing this to FALSE does not change it to GET.

$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt ($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

// Only calling the head
curl_setopt($ch, CURLOPT_HEADER, true); // header will be at output

//HERE IS THE MAGIC LINE
curl_setopt($ch, CURLOPT_NOBODY, true); // HTTP request is 'HEAD'

$content = curl_exec ($ch);
curl_close ($ch);

curl_setopt Doc

Mohit Padalia
  • 1,549
  • 13
  • 10
1

When you use cURL to access the headers of a page, the whole PHP file will be executed, even if there is long-running tasks inside. That's because HTTP headers may be overrided by the header function.

If you don't want to hang up, my suggestion is to use a command-line instead of a function to copy your file : instead of copy($source, $target), run the following if you're on a Linux system :

$source = escapeshellarg($source);
$target = escapeshellarg($target);
exec("cp $source $target &");

The & symbol will execute the command in background (so if the copy takes 3 secondes, it will be run in background and not hang your PHP file).

Alain Tiemblo
  • 36,099
  • 17
  • 121
  • 153