Skip to content

Commit

Permalink
请求增加命名空间区分
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojuntan committed Mar 28, 2019
1 parent 0e53a7b commit a63846e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
],
"psr-4": {
"pddUnionSdk\\": "src"
},
"files": [
"src/Tools/Helpers.php"
]
}
},
"repositories": {
"packagist": {
Expand Down
69 changes: 28 additions & 41 deletions src/Tools/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}
4 changes: 3 additions & 1 deletion src/pddUnionGateWay.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace pddUnionSdk;

use pddUnionSdk\Tools\Helpers;

/**
* 多多客网关
* Class pddUnionGateWay
Expand Down Expand Up @@ -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']);
Expand Down

0 comments on commit a63846e

Please sign in to comment.