tags:

views:

279

answers:

2

Using PHP and CURL (unless there is a better alternative then CURL in this case), is it possible to have a php function handle the header response before downloading the file?

For example:

I have a script that downloads and processes urls supplied by the user. I would like to add a check so that if the file is not valid for my process (not a text file, too large, etc),the CURL request would be cancelled before the server wastes time downloading the file.

Update: Solution PEAR class HTTP_Request2: http://pear.php.net/package/HTTP%5FRequest2/ Gives you the ability to set observers to the connection and throw exceptions to cancel anytime. Works perfectly for my needs!

+3  A: 

Using cURL, do a HTTP HEAD request to check the headers, then if it is valid (the status is 200) do the full HTTP GET request.

The basic option you must set is CURLOPT_NOBODY, which changes the requested to the type HEAD

curl_setopt($ch, CURLOPT_NOBODY, true);

Then after executing the query, you need to check the return status which can be done using curl_getinfo()

$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Yacoby
but that even dont help without code example
streetparade
No. It explains exactly how to do it.
Yacoby
That's true, but it does seem wasteful to have to send two requests/connections for every url.
Greg
+1  A: 

This is an example how you can solve it:

// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
    "Authorization: GoogleLogin auth=" . $auth,
    "GData-Version: 3.0",
);

// Make the request
curl_setopt($curl, CURLOPT_URL, "http://docs.google.com/feeds/default/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($curl);
curl_close($curl);

// Parse the response
$response = simplexml_load_string($response);

// Output data
foreach($response->entry as $file)
{
       //now you can do what ever if file type is a txt
        //if($file->title =="txt")
        // do something
        else
        // do soething
    echo "File: " . $file->title . "<br />";
    echo "Type: " . $file->content["type"] . "<br />";
    echo "Author: " . $file->author->name . "<br /><br />";
}
streetparade
That downloads the body of the url in addition to the headers. The point is get the headers and then have the option to stop the connection before the body if needed.
Greg