123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Service\YunXuan;
- use App\Log;
- use App\Service\HttpService;
- use App\Support\EmailQueue;
- class YXRequestService
- {
- const FORM_URLENCODED = "application/x-www-form-urlencoded";
- public static function getSortedParameterStr($paramArr)
- {
- ksort($paramArr);
- $tmpArr = array();
- foreach ($paramArr as $k => $v) {
- $tmp = $k;
- if (!empty($v)) {
- $tmp .= ("=" . $v);
- }
- array_push($tmpArr, $tmp);
- }
- return join('&', $tmpArr);
- }
- /**
- * Send a HTTP request with App Authorization
- *
- * @param $apiAppKey string AppKey
- * @param $apiAppSecret string AppSecret
- * @param $method string HTTP Method of API
- * @param $url string Request URL of API, note that environment path (/release) is not allowed
- * @param $contentType string Request Content-Type header, set empty if request body is not needed
- * @param $acceptHeader string Accept HTTP request header
- * @param $reqBody string Request Body, set null if request body is not needed
- * @param $formParam array form parameters array, set null if not form request
- * @param $algorithm string Encryption algorithm: sha1, sha256, sha384, sha512, SM3, default to sha1
- * @param $customHeaders array Custom HTTP Headers, such as `array('x-header-a' => 1)`
- */
- public static function sendRequestWithAppAuth(
- $apiAppKey, $apiAppSecret, $method, $url, $contentType, $acceptHeader, $reqBody=null,
- $formParam=null, $algorithm=null, $customHeaders=null
- )
- {
- $contentMD5 = "";
- $isForm = ($contentType == self::FORM_URLENCODED);
- if ($isForm) {
- assert(!is_null($formParam), "formParam is required for form request");
- // generate request body from form parameters
- $reqBody = self::getSortedParameterStr($formParam);
- } elseif (!is_null($reqBody)) {
- // get content md5 for signing the request later
- $contentMD5 = base64_encode(md5($reqBody));
- }
- if (null === $algorithm) {
- $algorithm = "sha1";
- }
- $paramArr = array();
- $parsedUrl = parse_url($url);
- if (isset($parsedUrl['query']) && !is_null($parsedUrl['query']) && !empty($parsedUrl['query'])) {
- parse_str($parsedUrl['query'], $paramArr);
- }
- if (!empty($formParam)) {
- $paramArr = array_merge($paramArr, $formParam);
- }
- $pathAndParam = $parsedUrl['path'];
- if (!empty($paramArr)) {
- $pathAndParam = $pathAndParam . '?' . self::getSortedParameterStr($paramArr);
- }
- $xDateHeader = gmstrftime('%a, %d %b %Y %T %Z', time());
- $strToSign = sprintf(
- "x-date: %s\n%s\n%s\n%s\n%s\n%s", $xDateHeader, $method, $acceptHeader, $contentType, $contentMD5, $pathAndParam
- );
- // $strToSignDebug = str_replace("\n", "#", $strToSign);
- $sign = base64_encode(hash_hmac($algorithm, $strToSign, $apiAppSecret, TRUE));
- $authHeader = sprintf(
- 'hmac id="%s", algorithm="hmac-%s", headers="x-date", signature="%s"',
- $apiAppKey, $algorithm, $sign
- );
- $headers = array(
- 'Host:' . $parsedUrl['host'],
- 'Accept:' . $acceptHeader,
- 'X-Date:' . $xDateHeader,
- 'Authorization:' . $authHeader,
- );
- if (!empty($contentType)) {
- array_push($headers, "Content-Type:" . $contentType);
- }
- if (!empty($contentMD5)) {
- array_push($headers, "Content-MD5:" . $contentMD5);
- }
- if (!is_null($customHeaders) && is_array($customHeaders)) {
- foreach ($customHeaders as $k => $v) {
- array_push($headers, $k . ":" . $v);
- }
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- if (!empty($reqBody)) {
- curl_setopt($ch, CURLOPT_POSTFIELDS, $reqBody); // only required if request body is present
- }
- curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
- $data = curl_exec($ch);
- if (curl_errno($ch)) {
- Log::logError('云选联盟接口请求失败', ['error' => curl_error($ch)], 'YXRequest');
- return false;
- }
- Log::logError('云选联盟接口返回结果', [
- 'method' => $method, 'url' => $url, 'form_param' => $formParam, 'data' => $data, 'req_body' => $reqBody
- ], 'YXRequest');
- curl_close($ch);
- return $data;
- }
- }
|