skip to Main Content

I am using cURL to get string and content type from external website. For now i am using two cURL function. The problem is it requesting two time to same web page for string and content type.

Content Type :

function get_content_type_curl($url_content_type) {
    
    $agent_content_type = $_SERVER['HTTP_USER_AGENT'];
    $ch_content_type = curl_init();

    curl_setopt($ch_content_type, CURLOPT_URL, $url_content_type);
    curl_setopt($ch_content_type, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch_content_type, CURLOPT_HEADER, 0);
    curl_setopt($ch_content_type, CURLOPT_NOBODY, 1);
    curl_setopt($ch_content_type, CURLOPT_USERAGENT, $agent_content_type);
    curl_setopt($ch_content_type, CURLOPT_FOLLOWLOCATION, 1);

    curl_exec($ch_content_type);
    $content_type = curl_getinfo($ch_content_type, CURLINFO_CONTENT_TYPE);

    curl_close($ch_content_type);

    return $content_type;
}

$content_type = get_content_type_curl("https://example.com");

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

String :

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);    

    $data = curl_exec($ch);
    curl_close($ch);

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

How to make it request one time to example.com and get both string and content_type

2

Answers


  1. You can just return the array from one of the curl method having both content type and the data.

        function get_curl($url_content_type)
        {
            $agent_curl = $_SERVER['HTTP_USER_AGENT'];
    
            $ch = curl_init();
    
            curl_setopt($ch, CURLOPT_AUTOREFERER, true);      
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url_content_type);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_USERAGENT, $agent_curl);
    
            $data         = curl_exec($ch);
            $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    
            curl_close($ch);
    
            return compact('data', 'content_type');
        }
    
        $data = get_curl("https://example.com");
        $homepage     = $data['data'];
        $content_type = $data['content_type'];
    
        header('Content-Type:'.$content_type);
        echo $homepage;
    
    Login or Signup to reply.
  2. To merge two cURL with one HTTP Request

    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");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search