<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: shanmugad</title>
    <description>The latest articles on DEV Community by shanmugad (@shanmugad).</description>
    <link>https://dev.to/shanmugad</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1032427%2Fe992063a-224b-48f6-9785-bcaa317fbcf6.png</url>
      <title>DEV Community: shanmugad</title>
      <link>https://dev.to/shanmugad</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shanmugad"/>
    <language>en</language>
    <item>
      <title>PHP cURL for large content and single HTTP request</title>
      <dc:creator>shanmugad</dc:creator>
      <pubDate>Thu, 23 Feb 2023 13:06:43 +0000</pubDate>
      <link>https://dev.to/shanmugad/php-curl-for-large-content-and-single-http-request-4226</link>
      <guid>https://dev.to/shanmugad/php-curl-for-large-content-and-single-http-request-4226</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;   Which code is best from these. or anyway we can make more better&lt;/li&gt;
&lt;li&gt;    I want a code which make single request to website example.com&lt;/li&gt;
&lt;li&gt;    Which code is good for getting large content from external website&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Code 1 :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code 2 :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code 3 :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vibecoding</category>
    </item>
  </channel>
</rss>
