From a63846e7898b2cf6b3a50bc81c47b4059e656624 Mon Sep 17 00:00:00 2001 From: shaojuntan Date: Thu, 28 Mar 2019 11:48:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E5=A2=9E=E5=8A=A0=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E7=A9=BA=E9=97=B4=E5=8C=BA=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 5 +-- src/Tools/Helpers.php | 69 +++++++++++++++++------------------------ src/pddUnionGateWay.php | 4 ++- 3 files changed, 32 insertions(+), 46 deletions(-) diff --git a/composer.json b/composer.json index dd45b95..7095748 100644 --- a/composer.json +++ b/composer.json @@ -15,10 +15,7 @@ ], "psr-4": { "pddUnionSdk\\": "src" - }, - "files": [ - "src/Tools/Helpers.php" - ] + } }, "repositories": { "packagist": { diff --git a/src/Tools/Helpers.php b/src/Tools/Helpers.php index 2044d77..dbc627b 100644 --- a/src/Tools/Helpers.php +++ b/src/Tools/Helpers.php @@ -6,18 +6,35 @@ * Time: 10:10 */ -/** - * @api 发送post请求 - * @param $url - * @param $post_data - * @param array $header - * @return bool|string - */ -if (!function_exists('curl_post')) { - function curl_post($url, $post_data, $header = []) +namespace pddUnionSdk\Tools; + +class Helpers +{ + public static function fpm_curl_post($url, $post_data, $header = []) + { + $ch = \curl_init(); + \curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + // https请求 不验证证书和hosts + \curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); + \curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); + \curl_setopt($ch, CURLOPT_URL, $url); + \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 要求结果为字符串且输出到屏幕上 + if (!empty($header)) { + \curl_setopt($ch, CURLOPT_HTTPHEADER, $header); + } else { + \curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率 + } + \curl_setopt($ch, CURLOPT_TIMEOUT, 15); + $output = curl_exec($ch); + curl_close($ch); + return $output; + } + + public static function curl_post($url, $post_data, $header = []) { if (!extension_loaded('swoole')) { - $output = \fpm_curl_post($url, $post_data, $header); + $output = self::fpm_curl_post($url, $post_data, $header); } else { if (PHP_SAPI == 'cli') { $urlsInfo = \parse_url($url); @@ -36,39 +53,9 @@ function curl_post($url, $post_data, $header = []) }); $output = $chan->pop(); } else { - $output = \fpm_curl_post($url, $post_data, $header); + $output = self::fpm_curl_post($url, $post_data, $header); } } return $output; } -} - -/** - * fpm下发送post - * @param $url - * @param $post_data - * @param array $header - * @return bool|string - */ -if (!function_exists('fpm_curl_post')) { - function fpm_curl_post($url, $post_data, $header = []) - { - $ch = \curl_init(); - \curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); - // https请求 不验证证书和hosts - \curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); - \curl_setopt($ch, CURLOPT_POST, 1); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); - \curl_setopt($ch, CURLOPT_URL, $url); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 要求结果为字符串且输出到屏幕上 - if (!empty($header)) { - \curl_setopt($ch, CURLOPT_HTTPHEADER, $header); - } else { - \curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率 - } - \curl_setopt($ch, CURLOPT_TIMEOUT, 15); - $output = curl_exec($ch); - curl_close($ch); - return $output; - } } \ No newline at end of file diff --git a/src/pddUnionGateWay.php b/src/pddUnionGateWay.php index 274f550..8630994 100644 --- a/src/pddUnionGateWay.php +++ b/src/pddUnionGateWay.php @@ -8,6 +8,8 @@ namespace pddUnionSdk; +use pddUnionSdk\Tools\Helpers; + /** * 多多客网关 * Class pddUnionGateWay @@ -81,7 +83,7 @@ public function send($method, $params, $data_type = 'JSON') $params['timestamp'] = strval(time()); $params['sign'] = $this->signature($params); try { - $response = $this->isCurl == false ? \curl_post(self::URL, $params) : \fpm_curl_post(self::URL, $params); + $response = $this->isCurl == false ? Helpers::curl_post(self::URL, $params): Helpers::fpm_curl_post(self::URL, $params); $info = strtolower($data_type) == 'json' ? json_decode($response, true) : $response; if (isset($info['error_response'])) { $this->pddUnionFactory->setError($info['error_response']['error_msg']);