[solved] PHP CURL hickup with HEAD request

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



found the solution in the mailing list on a related problem :

if ( strtolower($theMethod) === 'head')
   // i.c.o. a HEAD request, specify no body is required.
   // This will enfore the method to HEAD anyway - see PHP documentation
   curl_setopt($ch, CURLOPT_NOBODY, TRUE ) ;
else if ( strlen($theBody) )
   // if a body was specified, set it
   curl_setopt($ch, CURLOPT_POSTFIELDS, $theBody) ;


On 18/10/15 11:45, B. Aerts wrote:
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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux