DEV Community

shanmugad
shanmugad

Posted on

PHP cURL for large content and single HTTP request

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 :

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;
Enter fullscreen mode Exit fullscreen mode

Code 2 :

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;
Enter fullscreen mode Exit fullscreen mode

Code 3 :

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;


Enter fullscreen mode Exit fullscreen mode

Top comments (0)