cURL 常用操作封装
2014-08-23 17:15
162 查看
原文地址:http://www.cnblogs.com/oshine/p/3895998.html /** * cURL 常用操作封装 * * @author oShine */ class ExtendedCurl { /** * 返回 JSON 内容为对象 */ const JSON_OBJECT = 0; /** * 返回 JSON 内容为数组 */ const JSON_ARRAY = 1; /** * cURL Handle * * @var resource */ private $curl; /** * 当前(默认) cURL 参数 * * @var array */ private $options = array( CURLOPT_FAILONERROR => false, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_CONNECTTIMEOUT => 15, CURLOPT_TIMEOUT => 60, //CURLOPT_COOKIESESSION => 1, ); /** * 最后一次请求的错误信息 * * @var null|string */ private $error = null; /** * @var int */ private $httpCode = null; /** * @param array $defaultOptions * @internal param array $options 可选的覆盖默认 cURL 参数 */ public function __construct(array $defaultOptions = array()) { $this->curl = curl_init(); if (!empty($defaultOptions)) { $this->options = $defaultOptions; } } /** * 设置 cURL 多个选项 * * @param array $options */ public function setOptions(array $options) { foreach ($options as $key => $value) { $this->setOption($key, $value); } } /** * 设置 cURL 单个选项 * * @param $key * @param $value */ public function setOption($key, $value) { $this->options[$key] = $value; } /** * 发送 GET 请求并返回解析后的 JSON 内容 * * @param $url * @param array $data * @param int $type * @return null|object|array */ public function getJson($url, array $data = array(), $type = self::JSON_ARRAY) { $content = $this->get($url, $data); return json_decode($content, $type); } /** * 发送 GET 请求 * * @param $url * @param array $data * @return null|string */ public function get($url, array $data = array()) { if (!empty($data)) { if (false === strpos($url, '?')) { $url .= '?'; } else { $url .= '&'; } $url .= http_build_query($data); } $options = array( CURLOPT_URL => $url, ); return $this->request($options); } /** * 发送 cURL 请求 * * @param array $options * @return mixed */ private function request(array $options = array()) { $this->setOptions($options); curl_setopt_array($this->curl, $this->options); $result = curl_exec($this->curl); $errorNo = curl_errno($this->curl); $response = curl_getinfo( $this->curl ); if ($errorNo) { $this->error = '[' . $errorNo . '] ' . curl_error($this->curl); } else { $this->error = null; } if(isset($response['http_code'])){ $this->httpCode = $response['http_code']; } return $result; } /** * 发送 POST 请求并返回解析后的 JSON 内容 * * @param $url * @param array $data * @param int $type * @return null|object|array */ public function postJson($url, array $data = array(), $return = self::JSON_ARRAY) { $content = $this->post($url, $data); return json_decode($content, $return); } /** * 发送 POST 请求 * * @param $url * @param array $data * @return null|string */ public function post($url, array $data = array()) { $options = array( CURLOPT_URL => $url, CURLOPT_POST => 1, ); if (!empty($data)) { if ($this->isMultiPart($data)) { $options[CURLOPT_POSTFIELDS] = $data; } else { $options[CURLOPT_POSTFIELDS] = http_build_query($data); } } return $this->request($options); } private function isMultiPart($data) { foreach ($data as $value) { if ('@' == $value[0]) return true; } return false; } /** * 判断最后一次请求是否有错误 * * @return bool */ public function hasError() { return null !== $this->error; } /** * 获取最后一次请求的错误信息 * * @return null|string */ public function getError() { return $this->error; } public function getHttpCode() { return $this->httpCode; } }
相关文章推荐
- 两个做项目最常用功能操作的封装类
- 做项目最常用功能操作的封装类 (转载)
- 共享两个做项目最常用功能操作的封装类
- 共享两个做项目最常用功能操作的封装类 (zhuan)
- VB、ASP 数据库操作工具类:DataTool,封装了常用的数据库操作
- c# socket 编程——对常用的网络操作进行封装
- 日期操作工具类之 Calendar的常用方法封装
- 封装ASP.NET操作ACCESS数据库常用操作的类
- xml操作类,封装了常用的对XML文件的操作功能....
- [网络收集]ASP.NET操作SQL数据库封装的常用操作
- 项目最常用功能操作的封装类
- c# socket 编程——对常用的网络操作进行封装
- 共享两个做项目最常用功能操作的封装类
- 共享两个做项目最常用功能操作的封装类
- 两个做项目最常用功能操作的封装类 (1)
- 做项目最常用功能操作的封装类 (转载)
- 共享两个做项目最常用功能操作的封装类
- php简单封装了一些常用JS操作
- 此类中封装一些常用的字符串操作(和业务无关)。
- VB、ASP 数据库操作工具类:DataTool,封装了常用的数据库操作