Thursday 15 April 2010

php - Is it ok to terminate a HTTP request in the callback function set by CURLOPT_HEADERFUNCTION? -



php - Is it ok to terminate a HTTP request in the callback function set by CURLOPT_HEADERFUNCTION? -

currently i'm writing php script supposed check if url current (returns http 200 code or redirects such url).

since several of urls tested homecoming file, i'd avoid using normal request, in order not having download file.

i utilize http head method, tests show, many servers don't recognize , homecoming different http code corresponding request.

my thought know create request , utilize curlopt_headerfunction define callback function checks http code in first line of header , terminate request having homecoming 0 (instead of length of header) if it's not redirect code.

my question is: ok, terminate http request that? or have negative effects on server? avoid unnecessary download?

example code (untested):

$url = "http://www.example.com/"; $ch = curl_init($url); curl_setopt_array($ch, array( curlopt_followlocation => true, curlopt_header => true, curlinfo_header_out => true, curlopt_httpget => true, curlopt_returntransfer => true, curlopt_headerfunction => 'requestheadercallback', )); $curlresult = curl_exec($ch); curl_close($ch); function requestheadercallback($ch, $header) { $matches = array(); if (preg_match("/^http/\d.\d (\d{3}) /")) { if ($matches[1] < 300 || $matches[1] >= 400) { homecoming 0; } } homecoming strlen($header); }

yes fine , yes stop transfer right there.

it cause connection disconnected, concern if intend many requests same host keeping connection live performance benefit.

php http libcurl

No comments:

Post a Comment