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
You can just return the array from one of the curl method having both content type and the data.
To merge two cURL with one HTTP Request