## 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