public function send(Message $message) { $this->assertRequiredOptions(static::MESSAGE_OPTIONS, $message->toArray()); $protocolVersion = $this->getProtocolVersion(); $url = $this->getUrl($message); $method = $this->getRequestMethod(); $requestHeaders = $this->getRequestHeaders(); $requestPayload = $this->getRequestPayload($message); $request = $this->createRequest($method, $url, $requestHeaders, $requestPayload, $protocolVersion, $message); $message->setHttpRequest($request); $httpClient = $this->httpClient; try { $response = $httpClient->send($request); } catch (GuzzleException $error) { $response = $error; } try { $parsedResponse = $this->handleResponse($response); $message->setResponse($parsedResponse); $message->setStatus(MessageStatus::STATUS_SENT); } catch (ProviderException $exception) { $message->setResponse($exception->getResponseBody()); $message->setError($exception); $message->setStatus(MessageStatus::STATUS_FAILED); } $message->setHttpResponse($response); if (isset($exception)) { return false; } else { return true; } }
/** * 返回请求短信接口时的 payload * * @param Message $message * @return string * @throws \RuntimeException */ protected function getRequestPayload(Message $message) { $recipients = implode(',', $message->getRecipients()); $data = $message->getData(); return json_encode(['to' => $recipients, 'appId' => $this->appId] + $data); }
/** * 返回请求短信接口时的 payload * * @param Message $message * @return string * @throws \RuntimeException */ protected function getRequestPayload(Message $message) { $recipients = implode(',', $message->getRecipients()); $templateId = (string) $message->getTemplateId(); $data = $message->getData(); array_walk($data, function (&$item) { $item = (string) $item; }); $data = array_values($data); return json_encode(['to' => $recipients, 'appId' => $this->appId, 'templateId' => $templateId, 'datas' => $data]); }