PHP cURL for large content and single HTTP request

Joined
Feb 23, 2023
Messages
1
Reaction score
0
I am trying to get MIME Type and body as content using cURL. After checking on Google and Stackoverflow, i have got multiple code which make this operation successful done. But there is some confusion, choose the most reliable, speed and single HTTP request code.
  1. Which code is best from these. or anyway we can make more better
  2. I want a code which make single request to website example.com
  3. Which code is good for getting large content from external website
Code 1 :

PHP:
function file_get_contents_curl($url_curl) {
$agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 1); // include headers in response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url_curl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);

// Get the content type and content
$response_curl = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
$data_curl = substr($response_curl, $header_size);

// Set the content type header (MIME Type)
header('Content-Type:' . $content_type);

curl_close($ch);

return $data_curl;
}

$homepage = file_get_contents_curl("https://example.com");
echo $homepage;


Code 2 :

PHP:
function file_get_contents_curl($url_curl){
$agent_curl = $_SERVER['HTTP_USER_AGENT'];

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url_curl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent_curl);

$data_curl = curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

curl_close($ch);

return compact('data_curl', 'content_type');
}

$data_webpage = file_get_contents_curl("https://example.com");
$homepage = $data_webpage['data_curl'];
$content_type = $data_webpage['content_type'];
header('Content-Type:'.$content_type);
echo $homepage;


Code 3 :


PHP:
function file_get_contents_curl($url) {
$agent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_VERBOSE, true);

// Get the content type
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

// Get the content
curl_setopt($ch, CURLOPT_NOBODY, 0);
$data = curl_exec($ch);
curl_close($ch);

// Set the content type header
header('Content-Type:' . $content_type);

return $data;
}

$homepage = file_get_contents_curl("https://example.com");
echo $homepage;
 
Joined
Mar 5, 2023
Messages
36
Reaction score
12
All three codes are valid and will work in getting the MIME type and body content of a webpage using cURL. However, I would suggest using code 2 as it is the most concise and efficient, making a single cURL request and returning both the data and content type as an array. Additionally, it doesn't include unnecessary header information in the response, making the output cleaner. You can modify this code to meet your specific needs.

There are a few modifications you can make to code 2 to improve its performance when handling large amounts of data:

  1. Enable the CURLOPT_BUFFERSIZE option to set the size of the buffer used for transferring data. By default, cURL uses a buffer size of 16KB. You can increase this value to improve performance when downloading large files.
  2. Use the CURLOPT_RANGE option to download only a specific range of bytes instead of the entire file. This is useful when you only need to download a specific portion of a large file.
  3. Increase the timeout value using the CURLOPT_TIMEOUT option to allow more time for the transfer to complete.
Here's an example of how you can modify code 2 to include these optimizations:

PHP:
function file_get_contents_curl($url_curl, $range = '') {
    $agent_curl = $_SERVER['HTTP_USER_AGENT'];

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url_curl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent_curl);
    curl_setopt($ch, CURLOPT_BUFFERSIZE, 1024*1024); // 1MB buffer size
    curl_setopt($ch, CURLOPT_TIMEOUT, 600); // 10 minutes timeout

    if (!empty($range)) {
        curl_setopt($ch, CURLOPT_RANGE, $range);
    }

    $data_curl = curl_exec($ch);
    $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

    curl_close($ch);

    return compact('data_curl', 'content_type');
}

// Example usage:
$data_webpage = file_get_contents_curl("https://example.com");
$homepage = $data_webpage['data_curl'];
$content_type = $data_webpage['content_type'];

header('Content-Type:'.$content_type);
echo $homepage;

Note that enabling the CURLOPT_RANGE option may not work for all websites, as some servers may not support partial content requests. Also, keep in mind that downloading large amounts of data can take time and use significant amounts of bandwidth, so it's best to only download what you need.

I hope this answer your question, feel free to reply if you need more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top