mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
119 lines
4.2 KiB
PHP
119 lines
4.2 KiB
PHP
<?php
|
||
|
||
// +----------------------------------------------------------------------
|
||
// | ThinkAdmin
|
||
// +----------------------------------------------------------------------
|
||
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
|
||
// +----------------------------------------------------------------------
|
||
// | 官方网站: http://think.ctolog.com
|
||
// +----------------------------------------------------------------------
|
||
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||
// +----------------------------------------------------------------------
|
||
// | github开源项目:https://github.com/zoujingli/ThinkAdmin
|
||
// +----------------------------------------------------------------------
|
||
|
||
namespace service;
|
||
|
||
/**
|
||
* HTTP请求服务
|
||
* Class HttpService
|
||
* @package service
|
||
* @author Anyon <zoujingli@qq.com>
|
||
* @date 2017/03/22 15:32
|
||
*/
|
||
class HttpService
|
||
{
|
||
|
||
/**
|
||
* 以get模拟网络请求
|
||
* @param string $url HTTP请求URL地址
|
||
* @param array $query GET请求参数
|
||
* @param array $options CURL参数
|
||
* @return bool|string
|
||
*/
|
||
public static function get($url, $query = [], $options = [])
|
||
{
|
||
$options['query'] = $query;
|
||
return HttpService::request('get', $url, $options);
|
||
}
|
||
|
||
/**
|
||
* 以get模拟网络请求
|
||
* @param string $url HTTP请求URL地址
|
||
* @param array $data POST请求数据
|
||
* @param array $options CURL参数
|
||
* @return bool|string
|
||
*/
|
||
public static function post($url, $data = [], $options = [])
|
||
{
|
||
$options['data'] = $data;
|
||
return HttpService::request('post', $url, $options);
|
||
}
|
||
|
||
/**
|
||
* CURL模拟网络请求
|
||
* @param string $method 请求方法
|
||
* @param string $url 请求方法
|
||
* @param array $options 请求参数[header,data,ssl_cer,ssl_key]
|
||
* @return bool|string
|
||
*/
|
||
public static function request($method, $url, $options = [])
|
||
{
|
||
$curl = curl_init();
|
||
// GET参数设置
|
||
if (!empty($options['query'])) {
|
||
$url .= stripos($url, '?') !== false ? '&' : '?' . http_build_query($options['query']);
|
||
}
|
||
// POST数据设置
|
||
if (strtolower($method) === 'post') {
|
||
curl_setopt($curl, CURLOPT_POST, true);
|
||
curl_setopt($curl, CURLOPT_POSTFIELDS, self::build($options['data']));
|
||
}
|
||
// 请求超时设置
|
||
$options['timeout'] = isset($options['timeout']) ? $options['timeout'] : 60;
|
||
curl_setopt($curl, CURLOPT_TIMEOUT, $options['timeout']);
|
||
// CURL头信息设置
|
||
if (!empty($options['header'])) {
|
||
curl_setopt($curl, CURLOPT_HTTPHEADER, $options['header']);
|
||
}
|
||
// 证书文件设置
|
||
if (!empty($options['ssl_cer']) && file_exists($options['ssl_cer'])) {
|
||
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
|
||
curl_setopt($curl, CURLOPT_SSLCERT, $options['ssl_cer']);
|
||
}
|
||
if (!empty($options['ssl_key']) && file_exists($options['ssl_key'])) {
|
||
curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
|
||
curl_setopt($curl, CURLOPT_SSLKEY, $options['ssl_key']);
|
||
}
|
||
curl_setopt($curl, CURLOPT_URL, $url);
|
||
curl_setopt($curl, CURLOPT_HEADER, false);
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||
list($content, $status) = [curl_exec($curl), curl_getinfo($curl), curl_close($curl)];
|
||
return (intval($status["http_code"]) === 200) ? $content : false;
|
||
}
|
||
|
||
/**
|
||
* POST数据过滤处理
|
||
* @param array $data
|
||
* @param bool $needBuild
|
||
* @return array
|
||
*/
|
||
private static function build($data, $needBuild = true)
|
||
{
|
||
if (!is_array($data)) {
|
||
return $data;
|
||
}
|
||
foreach ($data as $key => $value) {
|
||
if (is_string($value) && class_exists('CURLFile', false) && stripos($value, '@') === 0) {
|
||
if (($filename = realpath(trim($value, '@'))) && file_exists($filename)) {
|
||
list($needBuild, $data[$key]) = [false, new \CURLFile($filename)];
|
||
}
|
||
}
|
||
}
|
||
return $needBuild ? http_build_query($data) : $data;
|
||
}
|
||
|
||
}
|