PHP CURL hickup with HEAD request
| From: | B. Aerts | Date: | Sun, 18 Oct 2015 09:45:34 +0000 |
| Subject: | PHP CURL hickup with HEAD request | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-325209@lists.php.net to get a copy of this message | ||
Dear list,
I recently ran into a small hickup with the PHP CURL library.
I use the library to perform a HTTP HEAD request, supposed to retrieve only the headers for a request.
One of these headers is the "content-length" header - which returns the length of the file to be fetched.
Since it is a HEAD request, there is no response body returned - making the actual content-length 0 bytes.
This yields a CURL error code 18, "partial file returned", while the call performs exactly as intended.
Is there a switch I forgot to suppress this message ?
The code I use is :
$theMethod = "HEAD" ;
$theUrl = "http://localhost/index.html" ;
$theUsr = "user" ;
$thePwd = "xxxxxxxx" ;
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL, $theUrl) ;
curl_setopt($ch, CURLOPT_USERAGENT, "PHP cURL Client" ) ;
if (strlen($theUsr) || strlen($thePwd) )
{
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY ) ;
curl_setopt($ch, CURLOPT_USERPWD, $theUsr . ":" . $thePwd ) ;
}
// no check on method required; will be done by server
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $theMethod);
// if headers were supplied, format them so CURL understands them
if (is_array($extraHeaders) && count($extraHeaders) )
{
foreach ($extraHeaders AS $headerName => $headerValue)
$curlHeaders[] = "$headerName: $headerValue";
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeaders ) ;
}
curl_setopt($ch, CURLOPT_TIMEOUT, 15 ) ;
curl_setopt($ch, CURLOPT_VERBOSE, true ) ;
curl_setopt($ch, CURLOPT_HEADER, true ) ;
// keep return headers
curl_setopt($ch, CURLINFO_HEADER_OUT, true );
// keeps response as string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ) ;
// follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ) ;
// max. 5 redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 5) ;
if ( strlen($theBody) )
curl_setopt($ch, CURLOPT_POSTFIELDS, $theBody) ;
// execute HTTP request
$httpResponse = curl_exec($ch);
// Check if any error occurred
$errno = curl_errno($ch) ;
Thanks in advance,
Bert