加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php 浅析file_get_contents、curl函数用法

发布时间:2020-05-25 06:23:00 所属栏目:PHP 来源:互联网
导读:php 浅析file_get_contents、curl函数用法

对file_get_contents和curl函数用法感兴趣的小伙伴,下面一起跟随脚本之家 jb51.cc的小编两巴掌来看看吧!
file_get_contents ()应用很简单,但是有的服务器php.ini设置如果关闭allow_url_fopen,这个函数就失效了,一般个人服务器可以设置,但是如果是虚拟主机就不在自己掌控范围内了。但是curl 是另外一个打开远程页面的内容的函数用法如下:

<?php
// 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);
?>

当然此功能也有被关闭的可能。
使用以上2个方法可以使用function_exists()判断使用
 
/**
 * @param 
 * @author 脚本之家 jb51.cc jb51.cc
**/
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch,$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,$timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读