版权声明:本文为博主原创文章,未经博主允许欢迎转载。 https://blog.csdn.net/wuzuyu365/article/details/84889429
<?php
namespace Home\Service;
class CurlService
{
/**
* 模拟post提交json数据,支持gzip编码的服务器
*
* @param $url :网址
* @param $post_data :post数据
* @param int $timeout :超时时间(秒)
*
* @return mixed|string
*/
public static function httpPostJson($url, $post_data, $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if ('' != $post_data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$header = [];
$header[] = 'Content-Type:application/json;charset=utf-8';
$header[] = 'Content-Length: ' . strlen($post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$res = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
//判断是否gzip编码的,如果是,则解码
$prefix = dechex(ord($res[0])) . dechex(ord($res[1]));
$is_gzip = ('1f8b' == strtolower($prefix));
($is_gzip) && $res = gzdecode($res);
curl_close($ch);
return $res;
}
/**
* get\post方式的curl函数
*
* @param string $url 地址
* @param string $method 请求方式post/get
* @param string $data 数据
* @param array $headers 头部
*
* @return [type] [description]
* 例如 $token_headers = [
* x-api-key:0364273820fa2afcea59175355072c6b"
* ];
* $data = CurlService::httpRequest($token_url, 'GET', '', $token_headers);
*/
public static function httpRequest($url, $method = 'GET', $data = '', $headers = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if ($method != 'GET') {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8','Content-Length: ' . strlen($data)));
}
if ($headers) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); // 模拟用户使用的浏览器
$res = curl_exec($ch);
curl_close($ch);
//判断是否gzip编码的,如果是,则解码
$prefix = dechex(ord($res[0])) . dechex(ord($res[1]));
$is_gzip = ('1f8b' == strtolower($prefix));
($is_gzip) && $res = gzdecode($res);
curl_close($ch);
return $res;
}
/**
* 模拟PUT提交
*
* @param $url :网址
* @param string $post_data :数据字符串
* @param int $timeout :超时时间(秒)
*
* @return mixed|string
*/
public static function httpPutJson($url, $post_data = '', $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if ($post_data != '') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$header = array();
$header[] = 'Content-Type:application/json;charset=utf-8';
$header[] = 'Content-Length: ' . strlen($post_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$res = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
//判断是否gzip编码的,如果是,则解码
$prefix = dechex(ord($res[0])) . dechex(ord($res[1]));
$is_gzip = ('1f8b' == strtolower($prefix));
($is_gzip) && $res = gzdecode($res);
return $res;
}
/**
* 模拟delete提交
*
* @param $url :网址
* @param int $timeout :超时时间(秒)
*
* @return mixed|string
*/
public static function httpDelete($url, $timeout = 5)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$res = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
//判断是否gzip编码的,如果是,则解码
$prefix = dechex(ord($res[0])) . dechex(ord($res[1]));
$is_gzip = ('1f8b' == strtolower($prefix));
($is_gzip) && $res = gzdecode($res);
return $res;
}
}