123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- /**
- * HttpCurl Curl模拟Http工具类
- *
- * @author gaoming13 <gaoming13@yeah.net>
- * @link https://github.com/gaoming13/wechat-php-sdk
- * @link http://me.diary8.com/
- */
- namespace echowx\utils;
- class HttpCurl {
- /**
- * 模拟GET请求
- *
- * @param string $url
- * @param string $data_type
- *
- * @return mixed
- *
- * Examples:
- * ```
- * HttpCurl::get('http://api.example.com/?a=123&b=456', 'json');
- * ```
- */
- static public function get($url, $data_type='text')
- {
- $cl = curl_init();
- if(stripos($url, 'https://') !== FALSE) {
- curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($cl, CURLOPT_SSLVERSION, 1);
- }
- curl_setopt($cl, CURLOPT_URL, $url);
- curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
- $content = curl_exec($cl);
- $status = curl_getinfo($cl);
- curl_close($cl);
- if (isset($status['http_code']) && $status['http_code'] == 200) {
- if ($data_type == 'json') {
- $content = json_decode($content, true);
- }
- return $content;
- } else {
- return FALSE;
- }
- }
- /**
- * 模拟POST请求
- *
- * @param string $url
- * @param array $fields
- * @param string $data_type
- *
- * @return mixed
- *
- * Examples:
- * ```
- * HttpCurl::post('http://api.example.com/?a=123', array('abc'=>'123', 'efg'=>'567'), 'json');
- * HttpCurl::post('http://api.example.com/', '这是post原始内容', 'json');
- * 文件post上传
- * HttpCurl::post('http://api.example.com/', array('abc'=>'123', 'file1'=>'@/data/1.jpg'), 'json');
- * ```
- */
- static public function post($url, $fields, $data_type='text')
- {
- $cl = curl_init();
- if(stripos($url, 'https://') !== FALSE) {
- curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($cl, CURLOPT_SSLVERSION, 1);
- }
- if (class_exists('\CURLFile')) {
- if (isset($fields['media'])) {
- $fields = array('media' => new \CURLFile(realpath(ltrim($fields['media'], '@'))));
- }
- } else {
- if (defined('CURLOPT_SAFE_UPLOAD')) {
- curl_setopt($cl, CURLOPT_SAFE_UPLOAD, false);
- }
- }
- curl_setopt($cl, CURLOPT_URL, $url);
- curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt($cl, CURLOPT_POST, true);
- curl_setopt($cl, CURLOPT_POSTFIELDS, $fields);
- $content = curl_exec($cl);
- $status = curl_getinfo($cl);
- curl_close($cl);
- if (isset($status['http_code']) && $status['http_code'] == 200) {
- if ($data_type == 'json') {
- $content = json_decode($content, true);
- }
- return $content;
- } else {
- return FALSE;
- }
- }
- }
|