Home  >  Article  >  Backend Development  >  File_get_contents and curl function usage_PHP tutorial

File_get_contents and curl function usage_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:35970browse

The file_get_contents () application is very simple, but if some server php.ini settings turn off allow_url_fopen, this function will be invalid. Generally, personal servers can set it, but if it is a virtual host, it is not within their control. But curl is another function that opens the content of the remote page. The usage is as follows:

// create a new curl resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close curl resource, and free up system resources
curl_close($ch);
?>

Of course, this feature may also be turned off.

Using the above two methods, you can use function_exists() to determine the use

if(function_exists(file_get_contents)) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
if(function_exists(file_get_contents)) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486557.htmlTechArticlefile_get_contents () application is very simple, but if some server php.ini settings turn off allow_url_fopen, this function will be invalid. , generally a personal server can be set up, but if it is...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn