PHP Http v3.0.0_00
来源:互联网 发布:javascript用有关var 编辑:程序博客网 时间:2024/05/22 03:03
* 基于PHP curl
* version 3.0.0_00,支持POST数据,支持SSL,支持上传文件。
使用方法。该类没有字段,有6个方法(加构造函数是7个):
__construct;
addPostData;
addFileUploaded;
setReferer;
setCookieFile;
enableDebugMode;
setHeader;
execute;
最简单的:
$client = new Client('http://www.baidu.com/');echo $client->execute();
ssl的跟上面的完全相同:
$client = new Client('https://account.xiaomi.com/');echo $client->execute();
发送POST数据:
addPostData('username', '张三');
文件上传:
addFileUploaded('upfile', 'd:/myfile.zip');
调试模式:
enableDebugMode('d:/log.txt');
会把执行过程中的所有信息,包括非错误信息,记录到log.txt文件中。
其它几个方法也很简单,就不说了。
下面的代码看上去很乱,其实不乱,尽管复制使用即可。也可以到这里下载。
<?php class Client { protected $curl; protected $postData = NULL; public function __construct($url) { $this->curl = curl_init(); if (mb_detect_encoding($url) != 'UTF-8') { $url = mb_convert_encoding($url, 'UTF-8'); } curl_setopt($this->curl, CURLOPT_URL, htmlspecialchars_decode($url)); curl_setopt($this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN)'); $cookieFile = PHP_OS == 'WINNT' ? dirname(__FILE__) . '/' . 'Client.cookie' . '.txt' : dirname(__FILE__) . '/' . 'Client.cookie'; curl_setopt($this->curl, CURLOPT_COOKIEJAR, $cookieFile); curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($this->curl, CURLOPT_TIMEOUT_MS, 1000 * 30); curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, FALSE); } public function addPostData($fieldName, $value) { $this->postData["$fieldName"] = $value; } public function addFileUploaded($fieldName, $file) { $this->postData["$fieldName"] = '@' . realpath($file) . ';type=application/octet-stream'; } public function setReferer($Referer) { curl_setopt($this->curl, CURLOPT_REFERER, $Referer); } public function setCookieFile($cookieFile) { curl_setopt($this->curl, CURLOPT_COOKIEFILE, realpath($cookieFile)); curl_setopt($this->curl, CURLOPT_COOKIEJAR, realpath($cookieFile)); } public function enableDebugMode($logFile = 'php://stderr') { curl_setopt($this->curl, CURLOPT_VERBOSE, TRUE); if ($logFile == 'php://stderr') { return; } $fp = fopen($logFile, 'a+'); fwrite($fp, PHP_EOL . PHP_EOL); curl_setopt($this->curl, CURLOPT_STDERR, $fp); } protected function enableHeader($headerDataArray) { curl_setopt($this->curl, CURLOPT_HEADER, TRUE); curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headerDataArray); } public function execute($return = TRUE) { if ($this->postData !== NULL) { curl_setopt($this->curl, CURLOPT_POST, TRUE); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->postData); } if ($return) { curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); return curl_exec($this->curl); } else { curl_exec($this->curl); } } public function __destruct() { curl_close($this->curl); } } ?>
- PHP Http v3.0.0_00
- HTTP封装 in PHP v3.0.1_00
- 数控基础_00
- JAVA学习_00
- PHP即充宝v3.0实例
- android学习笔记_00 - 前言
- MySQL再学笔记_00
- bootstrap经验http://v3.bootcss.com/
- php http
- php http
- KISS PHP Framework v3 中的 ORM 语法
- php-5.5.1-android.patch.v3
- wdCP v3安装后PHP无法启动
- v3
- 面试题之_00(旋转字符串)
- 极光推送Jpush(v3)服务端PHP版本集成(V3版本只调用推送API)【转】
- !Quick Screen Capture v3.0.0
- 网讯虹桥 v3.0.0.2 绿色
- oracle 标识符无效 字段名和表中一致
- CString转为char *
- 【cocos2d-x】利用CCScrollView, 实现帮助界面
- .Net Socket编程基础 -1
- PHPCMS_V9笔记之模板列表
- PHP Http v3.0.0_00
- Android高效加载大图、多图解决方案,有效避免程序OOM
- find ctime ,mtime,atime使用
- 用Visual C#实现端口监听模拟即时通讯
- CentOS下搭建openstack集群
- poj 3160 Father Christmas flymouse (强连通分量+记忆化搜素)
- Linux命令速查
- web开发tips
- objective-c 函数参数 传地址 传指针 指向指针的指针