# Handling the returned response
In order to get the parameters of the response for it to be monitored and debugged, we need to set the option CURLOPT_HEADER. For example:
<?php
$url = ""https://www.gutenberg.org/files/41537/41537-h/41537-h.htm";
$file = __DIR__ . DIRECTORY_SEPARATOR . "the_divine_comedy.html";
$handle = curl_init();
$fileHandle = fopen($file, "w");
curl_setopt_array($handle,
array(
CURLOPT_URL => $url,
CURLOPT_FILE => $fileHandle,
CURLOPT_HEADER => true
)
);
$data = curl_exec($handle);
To get additional information about the request, we use the curl_getinfo command that enables us to receive important technical information about the response, including the status code (200 for success) and the size of the downloaded file.
$responseCode =
curl_getinfo($handle,
CURLINFO_HTTP_CODE
);
$downloadLength =
curl_getinfo($handle,
CURLINFO_CONTENT_LENGTH_DOWNLOAD
);
In addition, we can also use the commands: curl_error and curl_errno to debug the response and receive informative error messages.
if(curl_errno($handle))
{
print curl_error($handle);
}
Let's see the full code:
<?php
$url = "https://www.gutenberg.org/files/46852/46852-h/46852-h.htm";
$file = __DIR__ . DIRECTORY_SEPARATOR . "the_divine_comedy.html";
$handle = curl_init();
$fileHandle = fopen($file, "w");
curl_setopt_array($handle,
array(
CURLOPT_URL => $url,
CURLOPT_FILE => $fileHandle,
CURLOPT_HEADER => true
)
);
$data = curl_exec($handle);
$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
$downloadLength = curl_getinfo($handle, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
if(curl_errno($handle))
{
print curl_error($handle);
}
else
{
if($responseCode == "200") echo "successful request";
echo " # download length : " . $downloadLength;
curl_close($handle);
fclose($fileHandle);
}