|
@@ -29,30 +29,32 @@ class HttpCurl
|
|
|
* @param array $params 请求参数
|
|
|
* @param array $headers 请求头
|
|
|
*/
|
|
|
- public static function getCurl(string $url,array $params=[],array $headers=[],$ssl=false)
|
|
|
+ public static function getCurl(string $url, array $params = [], array $headers = [], $ssl = false)
|
|
|
{
|
|
|
//判断是否存在traceId
|
|
|
- if(isset($_GET["traceId"])){
|
|
|
+ if (isset($_GET["traceId"])) {
|
|
|
$headers["traceId"] = $_GET["traceId"];
|
|
|
}
|
|
|
|
|
|
self::$curl = $curl = new Curl();
|
|
|
//设置超时时间
|
|
|
- $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
|
|
|
+ $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
|
|
|
//设置连接超时时间
|
|
|
- $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
|
|
|
+ $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
|
|
|
|
|
|
//塞入header头
|
|
|
- foreach ($headers as $key=>$header){
|
|
|
- $curl->setHeader($key,$header);
|
|
|
+ foreach ($headers as $key => $header) {
|
|
|
+ $curl->setHeader($key, $header);
|
|
|
}
|
|
|
- if(!$ssl){
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
|
|
|
+ if (!$ssl) {
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
|
|
|
}
|
|
|
//塞入参数
|
|
|
- $curl->get($url,$params);
|
|
|
- return json_decode($curl->response,true);
|
|
|
+ $curl->get($url, $params);
|
|
|
+ $res = json_decode($curl->response, true);
|
|
|
+ $curl->close();
|
|
|
+ return $res;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -62,34 +64,36 @@ class HttpCurl
|
|
|
* @param array $headers 请求头
|
|
|
* @param bool $asJson 是否json传输(true是 false否)
|
|
|
*/
|
|
|
- public static function postCurl($url,$params=[],$headers=[],$asJson=true,$ssl=false)
|
|
|
+ public static function postCurl($url, $params = [], $headers = [], $asJson = true, $ssl = false)
|
|
|
{
|
|
|
//判断是否存在traceId
|
|
|
- if(isset($_GET["traceId"])){
|
|
|
+ if (isset($_GET["traceId"])) {
|
|
|
$headers["traceId"] = $_GET["traceId"];
|
|
|
}
|
|
|
|
|
|
self::$curl = $curl = new Curl();
|
|
|
//设置超时时间
|
|
|
- $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
|
|
|
+ $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
|
|
|
//设置连接超时时间
|
|
|
- $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
|
|
|
+ $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
|
|
|
|
|
|
//塞入header头
|
|
|
- foreach ($headers as $key=>$header){
|
|
|
- $curl->setHeader($key,$header);
|
|
|
+ foreach ($headers as $key => $header) {
|
|
|
+ $curl->setHeader($key, $header);
|
|
|
}
|
|
|
//判断是否json请求
|
|
|
- if($asJson){
|
|
|
- $curl->setHeader("Content-Type","application/json;charset=utf-8");
|
|
|
+ if ($asJson) {
|
|
|
+ $curl->setHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
}
|
|
|
- if(!$ssl){
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
|
|
|
+ if (!$ssl) {
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
|
|
|
}
|
|
|
//塞入参数
|
|
|
- $curl->post($url,$params,$asJson);
|
|
|
- return json_decode($curl->response,true);
|
|
|
+ $curl->post($url, $params, $asJson);
|
|
|
+ $res = json_decode($curl->response, true);
|
|
|
+ $curl->close();
|
|
|
+ return $res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -97,30 +101,32 @@ class HttpCurl
|
|
|
* @param array $params 请求参数
|
|
|
* @param array $headers 请求头
|
|
|
*/
|
|
|
- public static function getCurlOrigin(string $url,array $params=[],array $headers=[],$ssl=false)
|
|
|
+ public static function getCurlOrigin(string $url, array $params = [], array $headers = [], $ssl = false)
|
|
|
{
|
|
|
//判断是否存在traceId
|
|
|
- if(isset($_GET["traceId"])){
|
|
|
+ if (isset($_GET["traceId"])) {
|
|
|
$headers["traceId"] = $_GET["traceId"];
|
|
|
}
|
|
|
|
|
|
self::$curl = $curl = new Curl();
|
|
|
//设置超时时间
|
|
|
- $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
|
|
|
+ $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
|
|
|
//设置连接超时时间
|
|
|
- $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
|
|
|
+ $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
|
|
|
|
|
|
//塞入header头
|
|
|
- foreach ($headers as $key=>$header){
|
|
|
- $curl->setHeader($key,$header);
|
|
|
+ foreach ($headers as $key => $header) {
|
|
|
+ $curl->setHeader($key, $header);
|
|
|
}
|
|
|
- if(!$ssl){
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
|
|
|
+ if (!$ssl) {
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
|
|
|
}
|
|
|
//塞入参数
|
|
|
- $curl->get($url,$params);
|
|
|
- return $curl->response;
|
|
|
+ $curl->get($url, $params);
|
|
|
+ $res = $curl->response;
|
|
|
+ $curl->close();
|
|
|
+ return $res;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -130,34 +136,36 @@ class HttpCurl
|
|
|
* @param array $headers 请求头
|
|
|
* @param bool $asJson 是否json传输(true是 false否)
|
|
|
*/
|
|
|
- public static function postCurlOrigin($url,$params=[],$headers=[],$asJson=true,$ssl=false)
|
|
|
+ public static function postCurlOrigin($url, $params = [], $headers = [], $asJson = true, $ssl = false)
|
|
|
{
|
|
|
//判断是否存在traceId
|
|
|
- if(isset($_GET["traceId"])){
|
|
|
+ if (isset($_GET["traceId"])) {
|
|
|
$headers["traceId"] = $_GET["traceId"];
|
|
|
}
|
|
|
|
|
|
self::$curl = $curl = new Curl();
|
|
|
//设置超时时间
|
|
|
- $curl->setOpt(CURLOPT_TIMEOUT,self::$timeOut);
|
|
|
+ $curl->setOpt(CURLOPT_TIMEOUT, self::$timeOut);
|
|
|
//设置连接超时时间
|
|
|
- $curl->setOpt(CURLOPT_CONNECTTIMEOUT,self::$connectTimeOut);
|
|
|
+ $curl->setOpt(CURLOPT_CONNECTTIMEOUT, self::$connectTimeOut);
|
|
|
|
|
|
//塞入header头
|
|
|
- foreach ($headers as $key=>$header){
|
|
|
- $curl->setHeader($key,$header);
|
|
|
+ foreach ($headers as $key => $header) {
|
|
|
+ $curl->setHeader($key, $header);
|
|
|
}
|
|
|
//判断是否json请求
|
|
|
- if($asJson){
|
|
|
- $curl->setHeader("Content-Type","application/json;charset=utf-8");
|
|
|
+ if ($asJson) {
|
|
|
+ $curl->setHeader("Content-Type", "application/json;charset=utf-8");
|
|
|
}
|
|
|
- if(!$ssl){
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYPEER,false);
|
|
|
- $curl->setOpt(CURLOPT_SSL_VERIFYHOST,false);
|
|
|
+ if (!$ssl) {
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
|
|
|
+ $curl->setOpt(CURLOPT_SSL_VERIFYHOST, false);
|
|
|
}
|
|
|
//塞入参数
|
|
|
- $curl->post($url,$params,$asJson);
|
|
|
- return $curl->response;
|
|
|
+ $curl->post($url, $params, $asJson);
|
|
|
+ $res = $curl->response;
|
|
|
+ $curl->close();
|
|
|
+ return $res;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -165,27 +173,27 @@ class HttpCurl
|
|
|
* @param string $url
|
|
|
* @param string $filepath
|
|
|
*/
|
|
|
- public static function uploadFile(string $url,string $filepath)
|
|
|
+ public static function uploadFile(string $url, string $filepath)
|
|
|
{
|
|
|
|
|
|
- $file = new \CURLFile($filepath,"application/octet-stream");
|
|
|
+ $file = new \CURLFile($filepath, "application/octet-stream");
|
|
|
$data = [
|
|
|
- "media"=>$file
|
|
|
+ "media" => $file
|
|
|
];
|
|
|
$ch = curl_init($url);
|
|
|
- curl_setopt($ch,CURLOPT_POST,1);
|
|
|
- curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
|
|
|
- curl_setopt($ch,CURLOPT_HTTPHEADER,["multipart/form-data"]);
|
|
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, [ "multipart/form-data" ]);
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
- curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
|
|
|
+ curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
|
|
|
$result = curl_exec($ch);
|
|
|
- if(curl_errno($ch)){
|
|
|
+ if (curl_errno($ch)) {
|
|
|
curl_close($ch);
|
|
|
throw new HttpCustomeException("文件上传失败");
|
|
|
}
|
|
|
curl_close($ch);
|
|
|
- $res = json_decode($result,1);
|
|
|
- if($res["errcode"]==0){
|
|
|
+ $res = json_decode($result, 1);
|
|
|
+ if ($res["errcode"] == 0) {
|
|
|
return $res["media_id"];
|
|
|
}
|
|
|
}
|