## curl重写php file_get_contents
> file_get_contents在连接不上的时候会提示Connection refused,有时候会带来不便;另外,curl的性能比file_get_contents高,所以用curl重写file_get_contents
复制代码
~~~
function _file_get_contents($s) {
$ret = "";
$ch = curl_init($s);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
$buffer = curl_exec($ch);
curl_close($ch);
if ($buffer === false || empty($buffer)) {
$ret = "";
} else {
$ret = $buffer;
}
return $ret;
}
~~~
~~~
<?php
function curl($url, $ifpost = 0, $datafields = '', $cookiefile = '', $v = false) {
$header = array("Connection: Keep-Alive","Accept: text/html, application/xhtml+xml, */*",
"Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3","User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $v);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$ifpost && curl_setopt($ch, CURLOPT_POST, $ifpost);
$ifpost && curl_setopt($ch, CURLOPT_POSTFIELDS, $datafields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
$cookiefile && curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile);
$cookiefile && curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
~~~
- 常用函数
- 简单的php生成静态html代码
- PHP写文件函数
- PHP生成GUID的函数
- PHP常用正则表达式汇总
- php字符串压缩
- PHP无限分组
- PHP简单 对象(object) 与 数组(array) 的转换
- PHP数组常用函数
- PHP调式测试函数
- PHP常用字符串的操作函数
- mysql 常用内置函数
- PHP通用请求函数CURL封装
- 裁剪图片PHP代码
- PDO操作MYSQL封装类
- 10个实用的PHP代码片段
- 获取访问者IP地址
- PHP实现发红包程序
- PHP把文本转换成图片
- curl重写php file_get_contents
- PHP生成一个随机字符串
- PHP读文件和写文件
- PHP根据key 给二维数组分组
- php中curl模拟post提交多维数组
- 33个超级有用必须要收藏的PHP代码样例
- PHP防XSS 防SQL注入的代码
- php常用数组array函数实例总结
- 用PHP遍历目录下的全部文件
- GBK2UTF8