diff --git a/composer.json b/composer.json index 04057a1..7215999 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ "license": "MIT", "require": { "php": ">=7.1", + "ext-curl": "*", "ext-fileinfo": "*", "ext-openssl": "*", "ext-json": "*", diff --git a/src/Kernel/AppClient.php b/src/Kernel/AppClient.php index fa15e5b..7990e31 100755 --- a/src/Kernel/AppClient.php +++ b/src/Kernel/AppClient.php @@ -8,8 +8,8 @@ use EasyAlipay\Kernel\Exceptions\InvalidConfigException; use EasyAlipay\Kernel\Exceptions\InvalidSignException; use EasyAlipay\Kernel\Support\Collection; use EasyAlipay\Kernel\Traits\SingData; -use function EasyAlipay\Kernel\encrypt; use function EasyAlipay\Kernel\decrypt; +use function EasyAlipay\Kernel\encrypt; class AppClient extends BaseClient { @@ -161,7 +161,19 @@ class AppClient extends BaseClient //签名 $sysParams["sign"] = $this->generateSign(array_merge($apiParams, $sysParams), $this->signType); $requestUrl = $this->buildRequestUrl($sysParams); - $result = $this->httpPost($requestUrl, $apiParams); + $files = []; + $postMultipart = false; + $newParams = []; + foreach ($apiParams as $k => $v) { + if ("@" == substr($v, 0, 1)) { + $postMultipart = true; + } + } + if ($postMultipart) { + $result = $this->curl($requestUrl,$apiParams); + } else { + $result = $this->httpPost($requestUrl, $apiParams); + } if (isset($result[$this->ERROR_RESPONSE])) { //返回错误 throw new BadRequestException( 'Get Alipay API Error:' . $result[$this->ERROR_RESPONSE]['msg'] . @@ -308,4 +320,58 @@ class AppClient extends BaseClient $str = preg_match('/[\x80-\xff]/', $this->appId) ? $this->appId : print_r($request, true); $this->fileCharset = mb_detect_encoding($str, "UTF-8, GBK") == 'UTF-8' ? 'UTF-8' : 'GBK'; } + + /** + * @param $url + * @param null $postFields + * @return mixed + * @throws \Exception + */ + protected function curl($url, $postFields = null) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->gatewayUrl.$url); + curl_setopt($ch, CURLOPT_FAILONERROR, false); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + $postBodyString = ""; + $encodeArray = Array(); + $postMultipart = false; + if (is_array($postFields) && 0 < count($postFields)) { + foreach ($postFields as $k => $v) { + if ("@" != substr($v, 0, 1)) //判断是不是文件上传 + { + $postBodyString .= "$k=" . urlencode($this->characet($v, $this->postCharset)) . "&"; + $encodeArray[$k] = $this->characet($v, $this->postCharset); + } else //文件上传用multipart/form-data,否则用www-form-urlencoded + { + $postMultipart = true; + $encodeArray[$k] = new \CURLFile(substr($v, 1)); + } + } + unset ($k, $v); + curl_setopt($ch, CURLOPT_POST, true); + if ($postMultipart) { + curl_setopt($ch, CURLOPT_POSTFIELDS, $encodeArray); + } else { + curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1)); + } + } + if (!$postMultipart) { + $headers = array('content-type: application/x-www-form-urlencoded;charset=' . $this->postCharset); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } + $reponse = curl_exec($ch); + + if (curl_errno($ch)) { + throw new \Exception(curl_error($ch), 0); + } else { + $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + if (200 !== $httpStatusCode) { + throw new \Exception($reponse, $httpStatusCode); + } + } + curl_close($ch); + return json_decode($reponse,1); + } }