Web lists-archives.com

[PHP] PHP CURL hickup with HEAD request




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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php