From 17da6239f3b9ea871bc086bff05056a53ff6d7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:15:02 +0800 Subject: [PATCH 01/16] 3 --- wchat/wx/Message.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index 88716ad..3be9acb 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -16,8 +16,8 @@ class Message extends SmallProgram const MINIPROGRAMPAGE = 6; const WXCARD = 7; - private $openid = ''; - private $msgData = []; + private string $openid = ''; + private array $msgData = []; /** @@ -302,6 +302,7 @@ class Message extends SmallProgram $this->request->setMethod(HttpClient::POST); $this->request->setErrorField('errcode'); $this->request->setErrorMsgField('errmsg'); + $this->request->setHost('api.weixin.qq.com'); /** @var Result $body */ $body = $this->request->post($url, $data); From 02997c347674027181961e48d1ad901e5b71f3bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:17:15 +0800 Subject: [PATCH 02/16] 3 --- wchat/wx/Message.php | 508 +++++++++++++++++++++---------------------- 1 file changed, 254 insertions(+), 254 deletions(-) diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index 3be9acb..b63c6e5 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -5,311 +5,311 @@ namespace wchat\wx; use wchat\common\HttpClient; use wchat\common\Result; + class Message extends SmallProgram { - const TEXT = 0; - const IMAGE = 1; - const VOICE = 2; - const NEWS = 3; - const VIDEO = 4; - const MUSIC = 5; - const MINIPROGRAMPAGE = 6; - const WXCARD = 7; + const TEXT = 0; + const IMAGE = 1; + const VOICE = 2; + const NEWS = 3; + const VIDEO = 4; + const MUSIC = 5; + const MINIPROGRAMPAGE = 6; + const WXCARD = 7; private string $openid = ''; private array $msgData = []; - /** - * @param string $openid - */ - public function setOpenid(string $openid) - { - $this->openid = $openid; - $this->msgData['touser'] = $openid; - } + /** + * @param string $openid + */ + public function setOpenid(string $openid) + { + $this->openid = $openid; + $this->msgData['touser'] = $openid; + } - /** - * @param string $content - * @return Result - * @throws \Exception - */ - public function sendTextNews(string $content) - { - $this->msgData['msgtype'] = 'text'; - $this->msgData['text[content]'] = $content; + /** + * @param string $content + * @return Result + * @throws \Exception + */ + public function sendTextNews(string $content) + { + $this->msgData['msgtype'] = 'text'; + $this->msgData['text[content]'] = $content; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param $media_id - * @return Result - * @throws \Exception - */ - public function sendImageNews(string $media_id) - { - $this->msgData['msgtype'] = 'image'; - $this->msgData['image[media_id]'] = $media_id; + /** + * @param $media_id + * @return Result + * @throws \Exception + */ + public function sendImageNews(string $media_id) + { + $this->msgData['msgtype'] = 'image'; + $this->msgData['image[media_id]'] = $media_id; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param $media_id - * @return Result - * @throws \Exception - */ - public function sendVoiceNews(string $media_id) - { - $this->msgData['msgtype'] = 'voice'; - $this->msgData['voice[media_id]'] = $media_id; + /** + * @param $media_id + * @return Result + * @throws \Exception + */ + public function sendVoiceNews(string $media_id) + { + $this->msgData['msgtype'] = 'voice'; + $this->msgData['voice[media_id]'] = $media_id; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param $media_id - * @return Result - * @throws \Exception - */ - public function sendMpNewsNews(string $media_id) - { - $this->msgData['msgtype'] = 'mpnews'; - $this->msgData['mpnews[media_id]'] = $media_id; + /** + * @param $media_id + * @return Result + * @throws \Exception + */ + public function sendMpNewsNews(string $media_id) + { + $this->msgData['msgtype'] = 'mpnews'; + $this->msgData['mpnews[media_id]'] = $media_id; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param string $title - * @param string $description - * @param string $url - * @param string $picurl - * @return Result - * @throws \Exception - */ - public function sendNewsNews(string $title, string $description, string $url, string $picurl) - { - $this->msgData['msgtype'] = 'news'; - $this->msgData['news[articles][0][title]'] = $title; - $this->msgData['news[articles][0][description]'] = $description; - $this->msgData['news[articles][0][url]'] = $url; - $this->msgData['news[articles][0][picurl]'] = $picurl; + /** + * @param string $title + * @param string $description + * @param string $url + * @param string $picurl + * @return Result + * @throws \Exception + */ + public function sendNewsNews(string $title, string $description, string $url, string $picurl) + { + $this->msgData['msgtype'] = 'news'; + $this->msgData['news[articles][0][title]'] = $title; + $this->msgData['news[articles][0][description]'] = $description; + $this->msgData['news[articles][0][url]'] = $url; + $this->msgData['news[articles][0][picurl]'] = $picurl; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param string $title - * @return Result - * @throws \Exception - */ - public function sendCardNews(string $title) - { - $this->msgData['msgtype'] = 'wxcard'; - $this->msgData['wxcard[card_id]'] = $title; + /** + * @param string $title + * @return Result + * @throws \Exception + */ + public function sendCardNews(string $title) + { + $this->msgData['msgtype'] = 'wxcard'; + $this->msgData['wxcard[card_id]'] = $title; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param string $media_id - * @param string $thumb_media_id - * @param string $title - * @param string $description - * @return Result - * @throws \Exception - */ - public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) - { - $this->msgData['msgtype'] = 'video'; - $this->msgData['video[media_id]'] = $media_id; - $this->msgData['video[thumb_media_id]'] = $thumb_media_id; - $this->msgData['video[title]'] = $title; - $this->msgData['video[description]'] = $description; + /** + * @param string $media_id + * @param string $thumb_media_id + * @param string $title + * @param string $description + * @return Result + * @throws \Exception + */ + public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) + { + $this->msgData['msgtype'] = 'video'; + $this->msgData['video[media_id]'] = $media_id; + $this->msgData['video[thumb_media_id]'] = $thumb_media_id; + $this->msgData['video[title]'] = $title; + $this->msgData['video[description]'] = $description; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param string $musicurl - * @param string $hqmusicurl - * @param string $thumb_media_id - * @param string $title - * @param string $description - * @return Result - * @throws \Exception - */ - public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) - { - $this->msgData['msgtype'] = 'music'; - $this->msgData['music[title]'] = $title; - $this->msgData['music[description]'] = $description; - $this->msgData['music[musicurl]'] = $musicurl; - $this->msgData['music[hqmusicurl]'] = $hqmusicurl; - $this->msgData['music[thumb_media_id]'] = $thumb_media_id; + /** + * @param string $musicurl + * @param string $hqmusicurl + * @param string $thumb_media_id + * @param string $title + * @param string $description + * @return Result + * @throws \Exception + */ + public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) + { + $this->msgData['msgtype'] = 'music'; + $this->msgData['music[title]'] = $title; + $this->msgData['music[description]'] = $description; + $this->msgData['music[musicurl]'] = $musicurl; + $this->msgData['music[hqmusicurl]'] = $hqmusicurl; + $this->msgData['music[thumb_media_id]'] = $thumb_media_id; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param string $head_content - * @param string $tail_content - * @param array $menus - * @return Result - * @throws \Exception - */ - public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) - { - $this->msgData['msgtype'] = 'msgmenu'; - $this->msgData['msgmenu[head_content]'] = $head_content; - $this->msgData['msgmenu[tail_content]'] = $tail_content; + /** + * @param string $head_content + * @param string $tail_content + * @param array $menus + * @return Result + * @throws \Exception + */ + public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) + { + $this->msgData['msgtype'] = 'msgmenu'; + $this->msgData['msgmenu[head_content]'] = $head_content; + $this->msgData['msgmenu[tail_content]'] = $tail_content; - if (empty($menus) || !is_array($menus) || count($menus) < 2) { - throw new \Exception('菜单选项必须有2个'); - } + if (empty($menus) || !is_array($menus) || count($menus) < 2) { + throw new \Exception('菜单选项必须有2个'); + } - foreach ($menus as $key => $val) { - $this->addNewsMenu($val['id'], $val['name']); - } + foreach ($menus as $key => $val) { + $this->addNewsMenu($val['id'], $val['name']); + } - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - private $index = 0; + private $index = 0; - /** - * @param $id - * @param $menuName - * @return $this - */ - public function addNewsMenu($id, $menuName) - { - $this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id; - $this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName; + /** + * @param $id + * @param $menuName + * @return $this + */ + public function addNewsMenu($id, $menuName) + { + $this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id; + $this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName; - ++$this->index; + ++$this->index; - return $this; - } + return $this; + } - /** - * @param $title - * @param $appid - * @param $pagepath - * @param $thumb_media_id - * @return Result - * @throws \Exception - */ - public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) - { - $this->msgData['msgtype'] = 'msgmenu'; - $this->msgData['miniprogrampage[title]'] = $title; - $this->msgData['miniprogrampage[appid]'] = $appid; - $this->msgData['miniprogrampage[pagepath]'] = $pagepath; - $this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id; + /** + * @param $title + * @param $appid + * @param $pagepath + * @param $thumb_media_id + * @return Result + * @throws \Exception + */ + public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) + { + $this->msgData['msgtype'] = 'msgmenu'; + $this->msgData['miniprogrampage[title]'] = $title; + $this->msgData['miniprogrampage[appid]'] = $appid; + $this->msgData['miniprogrampage[pagepath]'] = $pagepath; + $this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id; - return $this->sendKefuMsg(); - } + return $this->sendKefuMsg(); + } - /** - * @param string $filePath - * @param string $type - * @param bool $isPermanent - * @param string $title - * @param string $introduction - * @return mixed - * @throws \Exception - */ - public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '') - { - if (!file_exists($filePath)) { - throw new \Exception('文件不存在'); - } + /** + * @param string $filePath + * @param string $type + * @param bool $isPermanent + * @param string $title + * @param string $introduction + * @return mixed + * @throws \Exception + */ + public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '') + { + if (!file_exists($filePath)) { + throw new \Exception('文件不存在'); + } - if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) { - throw new \Exception('暂不支持的文件类型'); - } + if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) { + throw new \Exception('暂不支持的文件类型'); + } - $token = $this->getAccessToken(); - if ($isPermanent) { - $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}"; - } else { - $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}"; - } + $token = $this->getAccessToken(); + if ($isPermanent) { + $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$token}&type={$type}"; + } else { + $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type={$type}"; + } - $mime = mime_content_type($filePath); + $mime = mime_content_type($filePath); - $real_path = new \CURLFile(realpath($filePath)); + $real_path = new \CURLFile(realpath($filePath)); - $data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime); - if ($isPermanent && $mime == 'video/mp3') { - $data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction]; - } + $data = array("media" => $real_path, 'form-data[filename]' => $filePath, 'form-data[Content-Type]' => $mime); + if ($isPermanent && $mime == 'video/mp3') { + $data = ['media' => $real_path, 'description[title]' => $title, 'description[introduction]' => $introduction]; + } - $this->request->setMethod(HttpClient::POST); + $this->request->setMethod(HttpClient::POST); - /** @var Result $body */ - $data = $this->request->post($url, $data); - if (!$data->isResultsOK()) { - throw new \Exception($data->getMessage()); - } + /** @var Result $body */ + $data = $this->request->post($url, $data); + if (!$data->isResultsOK()) { + throw new \Exception($data->getMessage()); + } - return $data->getData(); - } + return $data->getData(); + } - /** - * @param $mime - * @throws \Exception - */ - private function checkExtinfo($mime) - { - switch (strtolower($mime)) { - case 'image/bmp': - case 'image/png': - case 'image/jpeg': - case 'image/jpg': - case 'image/gif': - break; - case 'mp3/wma/wav/amr': - break; - case 'mp4'; - break; - case 'jpg'; - break; - default: - throw new \Exception('不支持的文件格式'); - } - } + /** + * @param $mime + * @throws \Exception + */ + private function checkExtinfo($mime) + { + switch (strtolower($mime)) { + case 'image/bmp': + case 'image/png': + case 'image/jpeg': + case 'image/jpg': + case 'image/gif': + case 'mp3/wma/wav/amr': + case 'mp4'; + case 'jpg'; + break; + default: + throw new \Exception('不支持的文件格式'); + } + } - /** - * @param $data - * @return Result - * @throws \Exception - */ - private function sendKefuMsg() - { - $data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE); + /** + * @param $data + * @return Result + * @throws \Exception + */ + private function sendKefuMsg() + { + $data = json_encode($this->msgData, JSON_UNESCAPED_UNICODE); - $url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken(); - $this->request->setMethod(HttpClient::POST); - $this->request->setErrorField('errcode'); - $this->request->setErrorMsgField('errmsg'); - $this->request->setHost('api.weixin.qq.com'); + $url = '/cgi-bin/message/custom/send?access_token=' . $this->getAccessToken(); + $this->request->setMethod(HttpClient::POST); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $this->request->setHost('api.weixin.qq.com'); - /** @var Result $body */ - $body = $this->request->post($url, $data); + $this->request->addHeader('Content-Type', 'application/json'); - if (!$body->isResultsOK()) { - throw new \Exception($body->getMessage()); - } - return $body; - } + /** @var Result $body */ + $body = $this->request->post($url, $data); + + if (!$body->isResultsOK()) { + throw new \Exception($body->getMessage()); + } + return $body; + } } From e9d9b305ebb72273f6d5efe1353267fe2be96585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:18:27 +0800 Subject: [PATCH 03/16] 3 --- wchat/common/HttpClient.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index e778df9..8fae737 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -195,7 +195,6 @@ class HttpClient */ public function setHost(string $host) { - var_dump($host); $this->host = $this->replaceHost($host); $match_quest = '/^[a-zA-Z\-]+(\.[a-zA-Z\-])+/'; if (preg_match($match_quest, $this->host)) { @@ -365,7 +364,6 @@ class HttpClient } if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) { - var_dump($this->host); $this->host = System::gethostbyname($this->host); } @@ -537,6 +535,7 @@ class HttpClient } if (!empty($this->_data)) { + var_dump($this->_data); $client->setData($this->_data); } $client->execute($url); From 6377d7e1ee0b219d540a5be3c7bb9ea7ac838816 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:19:24 +0800 Subject: [PATCH 04/16] 3 --- wchat/common/HttpClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 8fae737..6498fcc 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -525,6 +525,7 @@ class HttpClient $this->header['User-Agent'] = $this->agent; } if (!empty($this->header)) { + var_dump($this->header); $client->setHeaders($this->header); } $client->setMethod(strtoupper($this->method)); @@ -535,7 +536,6 @@ class HttpClient } if (!empty($this->_data)) { - var_dump($this->_data); $client->setData($this->_data); } $client->execute($url); From 59e72de410c588469531686762097c2ad033bf89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:28:41 +0800 Subject: [PATCH 05/16] 3 --- wchat/wx/Message.php | 74 +++++++++++++++++++++++--------------------- 1 file changed, 38 insertions(+), 36 deletions(-) diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index b63c6e5..85ed206 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -38,7 +38,7 @@ class Message extends SmallProgram public function sendTextNews(string $content) { $this->msgData['msgtype'] = 'text'; - $this->msgData['text[content]'] = $content; + $this->msgData['text'] = ['content' => $content]; return $this->sendKefuMsg(); } @@ -51,7 +51,7 @@ class Message extends SmallProgram public function sendImageNews(string $media_id) { $this->msgData['msgtype'] = 'image'; - $this->msgData['image[media_id]'] = $media_id; + $this->msgData['image'] = ['media_id' => $media_id]; return $this->sendKefuMsg(); } @@ -65,7 +65,7 @@ class Message extends SmallProgram public function sendVoiceNews(string $media_id) { $this->msgData['msgtype'] = 'voice'; - $this->msgData['voice[media_id]'] = $media_id; + $this->msgData['voice'] = ['media_id' => $media_id]; return $this->sendKefuMsg(); } @@ -78,7 +78,7 @@ class Message extends SmallProgram public function sendMpNewsNews(string $media_id) { $this->msgData['msgtype'] = 'mpnews'; - $this->msgData['mpnews[media_id]'] = $media_id; + $this->msgData['mpnews'] = ['media_id' => $media_id]; return $this->sendKefuMsg(); } @@ -95,11 +95,12 @@ class Message extends SmallProgram public function sendNewsNews(string $title, string $description, string $url, string $picurl) { $this->msgData['msgtype'] = 'news'; - $this->msgData['news[articles][0][title]'] = $title; - $this->msgData['news[articles][0][description]'] = $description; - $this->msgData['news[articles][0][url]'] = $url; - $this->msgData['news[articles][0][picurl]'] = $picurl; - + $this->msgData['news'] = ['articles' => [[ + 'title' => $title, + 'description' => $description, + 'url' => $url, + 'picurl' => $picurl + ]]]; return $this->sendKefuMsg(); } @@ -112,7 +113,7 @@ class Message extends SmallProgram public function sendCardNews(string $title) { $this->msgData['msgtype'] = 'wxcard'; - $this->msgData['wxcard[card_id]'] = $title; + $this->msgData['wxcard'] = ['card_id' => $title]; return $this->sendKefuMsg(); } @@ -129,11 +130,12 @@ class Message extends SmallProgram public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) { $this->msgData['msgtype'] = 'video'; - $this->msgData['video[media_id]'] = $media_id; - $this->msgData['video[thumb_media_id]'] = $thumb_media_id; - $this->msgData['video[title]'] = $title; - $this->msgData['video[description]'] = $description; - + $this->msgData['video'] = ['media_id' => [ + 'media_id' => $media_id, + 'thumb_media_id' => $thumb_media_id, + 'title' => $title, + 'description' => $description + ]]; return $this->sendKefuMsg(); } @@ -150,12 +152,13 @@ class Message extends SmallProgram public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) { $this->msgData['msgtype'] = 'music'; - $this->msgData['music[title]'] = $title; - $this->msgData['music[description]'] = $description; - $this->msgData['music[musicurl]'] = $musicurl; - $this->msgData['music[hqmusicurl]'] = $hqmusicurl; - $this->msgData['music[thumb_media_id]'] = $thumb_media_id; - + $this->msgData['music'] = [ + 'title' => $title, + 'description' => $description, + 'musicurl' => $musicurl, + 'hqmusicurl' => $hqmusicurl, + 'thumb_media_id' => $thumb_media_id + ]; return $this->sendKefuMsg(); } @@ -170,21 +173,20 @@ class Message extends SmallProgram public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) { $this->msgData['msgtype'] = 'msgmenu'; - $this->msgData['msgmenu[head_content]'] = $head_content; - $this->msgData['msgmenu[tail_content]'] = $tail_content; - + $this->msgData['msgmenu'] = [ + 'head_content' => $head_content, + 'tail_content' => $tail_content, + ]; if (empty($menus) || !is_array($menus) || count($menus) < 2) { throw new \Exception('菜单选项必须有2个'); } - foreach ($menus as $key => $val) { $this->addNewsMenu($val['id'], $val['name']); } - return $this->sendKefuMsg(); } - private $index = 0; + private int $index = 0; /** * @param $id @@ -193,11 +195,10 @@ class Message extends SmallProgram */ public function addNewsMenu($id, $menuName) { - $this->msgData['msgmenu[list][' . $this->index . '][id]'] = $id; - $this->msgData['msgmenu[list][' . $this->index . '][content]'] = $menuName; - + $lists['id'] = $id; + $lists['content'] = $menuName; + $this->msgData['msgmenu']['list'][$this->index] = $lists; ++$this->index; - return $this; } @@ -212,11 +213,12 @@ class Message extends SmallProgram public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) { $this->msgData['msgtype'] = 'msgmenu'; - $this->msgData['miniprogrampage[title]'] = $title; - $this->msgData['miniprogrampage[appid]'] = $appid; - $this->msgData['miniprogrampage[pagepath]'] = $pagepath; - $this->msgData['miniprogrampage[thumb_media_id]'] = $thumb_media_id; - + $this->msgData['miniprogrampage'] = [ + 'title' => $title, + 'appid' => $appid, + 'pagepath' => $pagepath, + 'thumb_media_id' => $thumb_media_id, + ]; return $this->sendKefuMsg(); } From 650b4d9f19bce9b9243e63e1e4e0b4fef1a2b659 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:30:35 +0800 Subject: [PATCH 06/16] 3 --- wchat/wx/Message.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index 85ed206..832e164 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -289,6 +289,13 @@ class Message extends SmallProgram } } + + public function getContents() + { + return $this->msgData; + } + + /** * @param $data * @return Result From 7e3a959096801e2d19a4ad16e76e0bcee554ce3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:33:13 +0800 Subject: [PATCH 07/16] 3 --- wchat/wx/Message.php | 86 +++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 57 deletions(-) diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index 832e164..e74fa34 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -32,55 +32,51 @@ class Message extends SmallProgram /** * @param string $content - * @return Result - * @throws \Exception + * @return Message */ public function sendTextNews(string $content) { $this->msgData['msgtype'] = 'text'; $this->msgData['text'] = ['content' => $content]; - return $this->sendKefuMsg(); + return $this; } /** - * @param $media_id - * @return Result - * @throws \Exception + * @param string $media_id + * @return Message */ public function sendImageNews(string $media_id) { $this->msgData['msgtype'] = 'image'; $this->msgData['image'] = ['media_id' => $media_id]; - return $this->sendKefuMsg(); + return $this; } /** - * @param $media_id - * @return Result - * @throws \Exception + * @param string $media_id + * @return Message */ public function sendVoiceNews(string $media_id) { $this->msgData['msgtype'] = 'voice'; $this->msgData['voice'] = ['media_id' => $media_id]; - return $this->sendKefuMsg(); + return $this; } /** - * @param $media_id - * @return Result - * @throws \Exception + * @param string $media_id + * @return Message */ public function sendMpNewsNews(string $media_id) { $this->msgData['msgtype'] = 'mpnews'; $this->msgData['mpnews'] = ['media_id' => $media_id]; - return $this->sendKefuMsg(); + return $this; } @@ -89,8 +85,7 @@ class Message extends SmallProgram * @param string $description * @param string $url * @param string $picurl - * @return Result - * @throws \Exception + * @return Message */ public function sendNewsNews(string $title, string $description, string $url, string $picurl) { @@ -101,21 +96,20 @@ class Message extends SmallProgram 'url' => $url, 'picurl' => $picurl ]]]; - return $this->sendKefuMsg(); + return $this; } /** * @param string $title - * @return Result - * @throws \Exception + * @return Message */ public function sendCardNews(string $title) { $this->msgData['msgtype'] = 'wxcard'; $this->msgData['wxcard'] = ['card_id' => $title]; - return $this->sendKefuMsg(); + return $this; } @@ -124,8 +118,7 @@ class Message extends SmallProgram * @param string $thumb_media_id * @param string $title * @param string $description - * @return Result - * @throws \Exception + * @return Message */ public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) { @@ -136,7 +129,7 @@ class Message extends SmallProgram 'title' => $title, 'description' => $description ]]; - return $this->sendKefuMsg(); + return $this; } @@ -146,8 +139,7 @@ class Message extends SmallProgram * @param string $thumb_media_id * @param string $title * @param string $description - * @return Result - * @throws \Exception + * @return Message */ public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) { @@ -159,7 +151,7 @@ class Message extends SmallProgram 'hqmusicurl' => $hqmusicurl, 'thumb_media_id' => $thumb_media_id ]; - return $this->sendKefuMsg(); + return $this; } @@ -167,7 +159,7 @@ class Message extends SmallProgram * @param string $head_content * @param string $tail_content * @param array $menus - * @return Result + * @return Message * @throws \Exception */ public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) @@ -183,7 +175,7 @@ class Message extends SmallProgram foreach ($menus as $key => $val) { $this->addNewsMenu($val['id'], $val['name']); } - return $this->sendKefuMsg(); + return $this; } private int $index = 0; @@ -203,12 +195,11 @@ class Message extends SmallProgram } /** - * @param $title - * @param $appid - * @param $pagepath - * @param $thumb_media_id - * @return Result - * @throws \Exception + * @param string $title + * @param string $appid + * @param string $pagepath + * @param string $thumb_media_id + * @return Message */ public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) { @@ -219,7 +210,7 @@ class Message extends SmallProgram 'pagepath' => $pagepath, 'thumb_media_id' => $thumb_media_id, ]; - return $this->sendKefuMsg(); + return $this; } /** @@ -268,28 +259,10 @@ class Message extends SmallProgram return $data->getData(); } + /** - * @param $mime - * @throws \Exception + * @return array */ - private function checkExtinfo($mime) - { - switch (strtolower($mime)) { - case 'image/bmp': - case 'image/png': - case 'image/jpeg': - case 'image/jpg': - case 'image/gif': - case 'mp3/wma/wav/amr': - case 'mp4'; - case 'jpg'; - break; - default: - throw new \Exception('不支持的文件格式'); - } - } - - public function getContents() { return $this->msgData; @@ -297,7 +270,6 @@ class Message extends SmallProgram /** - * @param $data * @return Result * @throws \Exception */ From b556ebffa2d89c5ef097ee104d364a696e6a6f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 02:36:05 +0800 Subject: [PATCH 08/16] 3 --- wchat/wx/Message.php | 64 +++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index e74fa34..861a0ba 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -32,51 +32,55 @@ class Message extends SmallProgram /** * @param string $content - * @return Message + * @return Result + * @throws \Exception */ public function sendTextNews(string $content) { $this->msgData['msgtype'] = 'text'; $this->msgData['text'] = ['content' => $content]; - return $this; + return $this->sendKefuMsg(); } /** - * @param string $media_id - * @return Message + * @param $media_id + * @return Result + * @throws \Exception */ public function sendImageNews(string $media_id) { $this->msgData['msgtype'] = 'image'; $this->msgData['image'] = ['media_id' => $media_id]; - return $this; + return $this->sendKefuMsg(); } /** - * @param string $media_id - * @return Message + * @param $media_id + * @return Result + * @throws \Exception */ public function sendVoiceNews(string $media_id) { $this->msgData['msgtype'] = 'voice'; $this->msgData['voice'] = ['media_id' => $media_id]; - return $this; + return $this->sendKefuMsg(); } /** - * @param string $media_id - * @return Message + * @param $media_id + * @return Result + * @throws \Exception */ public function sendMpNewsNews(string $media_id) { $this->msgData['msgtype'] = 'mpnews'; $this->msgData['mpnews'] = ['media_id' => $media_id]; - return $this; + return $this->sendKefuMsg(); } @@ -85,7 +89,8 @@ class Message extends SmallProgram * @param string $description * @param string $url * @param string $picurl - * @return Message + * @return Result + * @throws \Exception */ public function sendNewsNews(string $title, string $description, string $url, string $picurl) { @@ -96,20 +101,21 @@ class Message extends SmallProgram 'url' => $url, 'picurl' => $picurl ]]]; - return $this; + return $this->sendKefuMsg(); } /** * @param string $title - * @return Message + * @return Result + * @throws \Exception */ public function sendCardNews(string $title) { $this->msgData['msgtype'] = 'wxcard'; $this->msgData['wxcard'] = ['card_id' => $title]; - return $this; + return $this->sendKefuMsg(); } @@ -118,7 +124,8 @@ class Message extends SmallProgram * @param string $thumb_media_id * @param string $title * @param string $description - * @return Message + * @return Result + * @throws \Exception */ public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) { @@ -129,7 +136,7 @@ class Message extends SmallProgram 'title' => $title, 'description' => $description ]]; - return $this; + return $this->sendKefuMsg(); } @@ -139,7 +146,8 @@ class Message extends SmallProgram * @param string $thumb_media_id * @param string $title * @param string $description - * @return Message + * @return Result + * @throws \Exception */ public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) { @@ -151,7 +159,7 @@ class Message extends SmallProgram 'hqmusicurl' => $hqmusicurl, 'thumb_media_id' => $thumb_media_id ]; - return $this; + return $this->sendKefuMsg(); } @@ -159,7 +167,7 @@ class Message extends SmallProgram * @param string $head_content * @param string $tail_content * @param array $menus - * @return Message + * @return Result * @throws \Exception */ public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) @@ -175,7 +183,7 @@ class Message extends SmallProgram foreach ($menus as $key => $val) { $this->addNewsMenu($val['id'], $val['name']); } - return $this; + return $this->sendKefuMsg(); } private int $index = 0; @@ -195,11 +203,12 @@ class Message extends SmallProgram } /** - * @param string $title - * @param string $appid - * @param string $pagepath - * @param string $thumb_media_id - * @return Message + * @param $title + * @param $appid + * @param $pagepath + * @param $thumb_media_id + * @return Result + * @throws \Exception */ public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) { @@ -210,7 +219,7 @@ class Message extends SmallProgram 'pagepath' => $pagepath, 'thumb_media_id' => $thumb_media_id, ]; - return $this; + return $this->sendKefuMsg(); } /** @@ -270,6 +279,7 @@ class Message extends SmallProgram /** + * @param $data * @return Result * @throws \Exception */ From 8319fac4b77f68fbef2434f23528f76b2bd700eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:03:41 +0800 Subject: [PATCH 09/16] 3 --- wchat/common/HttpClient.php | 2001 ++++++++++++++++++----------------- 1 file changed, 1002 insertions(+), 999 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 6498fcc..88f072c 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -11,1003 +11,1006 @@ use Swoole\Coroutine\Client; class HttpClient { - private string $host = ''; - - private array $header = []; - - private int $timeout = 0; - - private ?\Closure $callback = null; - private string $method = 'get'; - - private bool $isSSL = false; - private string $agent = ''; - private string $errorCodeField = ''; - private string $errorMsgField = ''; - private bool $use_swoole = false; - - private string $ssl_cert_file = ''; - private string $ssl_key_file = ''; - private string $ca = ''; - private int $port = 80; - - /** @var string $_message 错误信息 */ - private string $_message = ''; - private ?string $_data = ''; - - private int $connect_timeout = 1; - - const GET = 'get'; - const PUT = 'put'; - const POST = 'post'; - const DELETE = 'delete'; - const OPTIONS = 'option'; - - /** - * @return int - */ - public function getConnectTimeout(): int - { - return $this->connect_timeout; - } - - /** - * @param int $connect_timeout - */ - public function setConnectTimeout(int $connect_timeout): void - { - $this->connect_timeout = $connect_timeout; - } - - - /** - * @return string - */ - public function getCa(): string - { - return $this->ca; - } - - /** - * @param string $ca - */ - public function setCa(string $ca): void - { - $this->ca = $ca; - } - - - /** - * @return int - */ - public function getPort(): int - { - return $this->port; - } - - /** - * @param int $port - */ - public function setPort(int $port): void - { - $this->port = $port; - } - - - /** - * @param $data - */ - public function setData($data) - { - $this->_data = $data; - } - - /** - * @return string - */ - public function getSslCertFile(): string - { - return $this->ssl_cert_file; - } - - /** - * @return bool - */ - public function hasSslCertFile(): bool - { - return !empty($this->ssl_cert_file) && file_exists($this->ssl_cert_file); - } - - /** - * @return bool - */ - public function hasSslKeyFile(): bool - { - return !empty($this->ssl_key_file) && file_exists($this->ssl_key_file); - } - - /** - * @param string $ssl_cert_file - */ - public function setSslCertFile(string $ssl_cert_file) - { - $this->ssl_cert_file = $ssl_cert_file; - } - - /** - * @return string - */ - public function getSslKeyFile(): string - { - return $this->ssl_key_file; - } - - /** - * @param string $ssl_key_file - */ - public function setSslKeyFile(string $ssl_key_file) - { - $this->ssl_key_file = $ssl_key_file; - } - - /** - */ - public static function NewRequest() - { - return new HttpClient(); - } - - /** - * @param string $name - * @return $this - */ - public function setErrorField(string $name) - { - $this->errorCodeField = $name; - return $this; - } - - /** - * @param $bool - * @return $this - */ - public function setUseSwoole($bool) - { - $this->use_swoole = $bool; - if ($this->use_swoole) { - function_exists('setCli') && setCli(true); - } - return $this; - } - - /** - * @param string $name - * @return $this - */ - public function setErrorMsgField(string $name) - { - $this->errorMsgField = $name; - return $this; - } - - /** - * @param string $host - */ - public function setHost(string $host) - { - $this->host = $this->replaceHost($host); - $match_quest = '/^[a-zA-Z\-]+(\.[a-zA-Z\-])+/'; - if (preg_match($match_quest, $this->host)) { - $this->addHeader('Host', $this->host); - } - } - - - /** - * @param $path - * @param array $data - * @param int $type - * @return Result - */ - public function sendTo($path, array $data, $type = SWOOLE_TCP) - { - $client = new Client($type); - if (empty($this->host) || empty($this->port)) { - return new Result(['code' => 500, 'message' => 'Host and port is null']); - } - if (!$client->connect($this->host, $this->port, $this->connect_timeout)) { - return new Result(['code' => 500, 'message' => $client->errMsg]); - } - $params['body'] = $data; - $params['path'] = '/' . $this->port . '/' . ltrim($path, '/'); - $params['header']['request_uri'] = $params['path']; - $params['header']['request_method'] = 'receive'; - if ($client->send(serialize($params))) { - $recv = $this->timeout > 0 ? $client->recv($this->timeout) : $client->recv(); - $param = $this->structure(Help::toArray($recv), $data, null, 200); - } else { - $param = new Result(['code' => 500, 'message' => $client->errMsg]); - } - $client->close(); - return $param; - } - - /** - * @param int $sec - * 设置超时时间 - */ - public function setTimeout(int $sec) - { - $this->timeout = $sec; - } - - - /** - * @param $key - * @param $value - */ - public function setHeader($key, $value) - { - $this->header[$key] = $value; - } - - /** - * @param $key - * @param $value - */ - public function addHeader($key, $value) - { - $this->header[$key] = $value; - } - - /** - * @param null $callback - */ - public function setCallback($callback) - { - $this->callback = $callback; - } - - /** - * @param string $method - */ - public function setMethod(string $method) - { - $this->method = $method; - } - - /** - * @param string $agent - */ - public function setAgent(string $agent) - { - $this->agent = $agent; - } - - /** - * @param bool $isSSL - */ - public function setIsSSL(bool $isSSL) - { - $this->isSSL = $isSSL; - if ($this->isSSL) { - $this->port = 443; - } - } - - /** - * @return bool - */ - public function getIsSSL() - { - return $this->isSSL; - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - */ - private function request($url, $data = []) - { - $data = $this->paramEncode($data); - if ($this->use_swoole) { - return $this->coroutine($this->matchHost($url), $data); - } else { - return $this->useCurl($url, $data); - } - } - - /** - * @return bool - */ - private function isCli() - { - return function_exists('getIsCli') && getIsCli(); - } - - /** - * @param string $string - * @return bool|string - * @throws Exception - */ - private function matchHost($string = '') - { - if (empty($string)) { - return false; - } - - if ($this->isHttp($string)) { - $string = str_replace('http://', '', $string); - $hostAndUrls = explode('/', $string); - - $this->host = array_shift($hostAndUrls); - $string = implode('/', $hostAndUrls); - } else if ($this->isHttps($string)) { - $string = str_replace('https://', '', $string); - $this->setIsSSL(true); - - $hostAndUrls = explode('/', $string); - - $this->host = array_shift($hostAndUrls); - $string = implode('/', $hostAndUrls); - } else if (empty($this->host)) { - $hostAndUrls = explode('/', $string); - $this->host = array_shift($hostAndUrls); - - $string = implode('/', $hostAndUrls); - } - - if (strpos($this->host, ':') !== false) { - [$this->host, $this->port] = explode(':', $this->host); - } - - if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) { - $this->host = System::gethostbyname($this->host); - } - - if (!$this->checkIsIp($this->host) && !$this->isDomainName($this->host)) { - throw new Exception('Client Host error.'); - } - - return $string; - } - - /** - * @param $name - * @return bool|mixed - */ - private function isDomainName($name) - { - if (!preg_match('/^[a-zA-Z\-0-9]+(\.[a-zA-Z\-0-9]+)+[^\/]?/', $name, $out)) { - return false; - } - return $out[0]; - } - - /** - * @param $url - * @param $data - * @return array|Result|mixed - * @throws - */ - private function useCurl($url, $data) - { - if ($this->isHttp($url) || $this->isHttps($url)) { - return $this->curl($url, $data); - } - $url = $this->matchHost(ltrim($url, '/')); - if (!empty($this->port)) { - $this->host .= ':' . $this->port; - } - if ($this->isSSL) { - return $this->curl('https://' . $this->host . '/' . $url, $data); - } else { - return $this->curl('http://' . $this->host . '/' . $url, $data); - } - } - - /** - * @param $host - * @return string|string[] - */ - private function replaceHost($host) - { - if ($this->isHttp($host)) { - return str_replace('http://', '', $host); - } - if ($this->isHttps($host)) { - return str_replace('https://', '', $host); - } - return $host; - } - - /** - * @param $url - * @return false|int - */ - private function checkIsIp($url) - { - return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url); - } - - /** - * @param $url - * @return bool - */ - private function isHttp($url) - { - return strpos($url, 'http://') === 0; - } - - /** - * @param $url - * @return bool - */ - private function isHttps($url) - { - return strpos($url, 'https://') === 0; - } - - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - * 使用swoole协程方式请求 - */ - private function coroutine($url, $data = []) - { - try { - $client = $this->generate_client($this->host, $url, $data); - if ($client->statusCode < 0) { - throw new Exception($client->errMsg); - } - unset($this->_data); - - $body = $this->resolve($client->getHeaders(), $client->body); - if (!in_array($client->getStatusCode(), [200, 201])) { - if (is_string($body)) { - $message = 'Request error code ' . $client->getStatusCode(); - } else { - $message = $this->searchMessageByData($body); - } - $response['code'] = $client->getStatusCode(); - $response['message'] = $message; - $response['data'] = $body; - $response['header'] = $client->getHeaders(); - - $response = new Result($response); - } else { - $response = $this->structure($body, $data, $client->getHeaders()); - } - } catch (\Throwable $exception) { - $response['code'] = 500; - $response['message'] = $exception->getMessage(); - $response['data'] = array_slice($exception->getTrace(), 0, 6); - $response['header'] = []; - - $response = new Result($response); - } - return $response; - } - - /** - * @return int - */ - private function getHostPort() - { - if (!empty($this->port)) { - return $this->port; - } - $port = 80; - if ($this->isSSL) $port = 443; - return $port; - } - - /** - * @param $host - * @param $url - * @param $data - * @return SClient - */ - private function generate_client($host, $url, $data = []) - { - $client = new SClient($host, $this->getHostPort(), $this->isSSL); - if (strpos($url, '/') !== 0) { - $url = '/' . $url; - } - - $client->set($this->settings()); - if (!empty($this->agent)) { - $this->header['User-Agent'] = $this->agent; - } - if (!empty($this->header)) { - var_dump($this->header); - $client->setHeaders($this->header); - } - $client->setMethod(strtoupper($this->method)); - if (strtolower($this->method) == self::GET && !empty($data)) { - $url .= '?' . $data; - } else { - $this->_data = $this->mergeParams($data); - } - - if (!empty($this->_data)) { - $client->setData($this->_data); - } - $client->execute($url); - $client->close(); - return $client; - } - - /** - * @param $newData - * @return mixed - */ - private function mergeParams($newData) - { - if (empty($this->_data)) { - return $this->toRequest($newData); - } else if (empty($newData)) { - return $this->toRequest($this->_data); - } - - $newData = Help::toArray($newData); - $array = Help::toArray($this->_data); - - $params = array_merge($array, $newData); - - return $this->toRequest($params); - } - - - /** - * @param $data - * @return false|mixed|string - */ - private function toRequest($data) - { - if (is_string($data)) { - return $data; - } - - $contentType = 'application/x-www-form-urlencoded'; - if (isset($this->header['Content-Type'])) { - $contentType = $this->header['Content-Type']; - } else if (isset($this->header['content-type'])) { - $contentType = $this->header['content-type']; - } - - if (strpos($contentType, 'json') !== false) { - return Help::toJson($data); - } else if (strpos($contentType, 'xml') !== false) { - return Help::toXml($data); - } else { - return http_build_query($data); - } - } - - /** - * @return array - */ - private function settings() - { - $sslCert = $this->getSslCertFile(); - $sslKey = $this->getSslKeyFile(); - $sslCa = $this->getCa(); - - $params = []; - if ($this->connect_timeout > 0) { - $params['timeout'] = $this->connect_timeout; - } - if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { - return $params; - } - - $params['ssl_host_name'] = $this->host; - $params['ssl_cert_file'] = $this->getSslCertFile(); - $params['ssl_key_file'] = $this->getSslKeyFile(); - $params['ssl_verify_peer'] = true; - $params['ssl_cafile'] = $sslCa; - - return $params; - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - */ - private function curl($url, $data = []) - { - try { - $output = $this->curlParse($url, $this->mergeParams($data)); - if ($output === FALSE) { - return new Result(['code' => 500, 'message' => $output]); - } - [$header, $body, $status] = $this->explode($output); - if (!in_array($status, [200, 201])) { - $data = new Result(['code' => $status, 'message' => $body, 'header' => $header]); - } else { - $data = $this->structure($body, $data, $header); - } - return $data; - } catch (\Throwable $exception) { - $response['code'] = 500; - $response['message'] = $exception->getMessage(); - $response['data'] = array_slice($exception->getTrace(), 0, 6); - $response['header'] = []; - return new Result($response); - } - } - - /** - * @param $url - * @param $data - * @return bool|string - * @throws Exception - */ - private function curlParse($url, $data) - { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data)); - if ($this->timeout > 0) { - curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); // 超时设置 - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); // 超时设置 - } - curl_setopt($ch, CURLOPT_HEADER, true); - - if ($headers = $this->parseHeaderMat()) { - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } - if (!empty($this->agent)) { - curl_setopt($ch, CURLOPT_USERAGENT, $this->agent); - } - if (file_exists($cert = $this->getSslCertFile())) { - curl_setopt($ch, CURLOPT_SSLCERT, $cert); - } - if (file_exists($key = $this->getSslKeyFile())) { - curl_setopt($ch, CURLOPT_SSLKEY, $key); - } - - curl_setopt($ch, CURLOPT_NOBODY, FALSE); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 - curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); - - if ($this->method == self::POST) { - curl_setopt($ch, CURLOPT_POST, 1); - } - - if ($this->method != self::GET) { - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - } - - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method)); - $output = curl_exec($ch); - if ($output === false) { - throw new Exception(curl_error($ch)); - } - curl_close($ch); - return $output; - } - - - /** - * @param $url - * @param $params - * @return array|mixed|Result - * 上传文件 - */ - public function upload($url, $params) - { - try { - $this->method = self::POST; - $output = $this->curlParse($url, $params); - if ($output === FALSE) { - return new Result(['code' => 500, 'message' => $output]); - } - [$header, $body, $status] = $this->explode($output); - if ($status != 200 && $status != 201) { - $data = new Result(['code' => $status, 'message' => $body, 'header' => $header]); - } else { - $data = $this->structure($body, $params, $header); - } - return $data; - } catch (\Throwable $exception) { - $response['code'] = 500; - $response['message'] = $exception->getMessage(); - $response['data'] = array_slice($exception->getTrace(), 0, 6); - $response['header'] = []; - return new Result($response); - } - } - - - /** - * @param $output - * @return array - */ - private function explode($output) - { - [$header, $body] = explode("\r\n\r\n", $output, 2); - if ($header == 'HTTP/1.1 100 Continue') { - [$header, $body] = explode("\r\n\r\n", $body, 2); - } else if (strpos($body, "\r\n\r\n") !== false) { - [$header, $body] = explode("\r\n\r\n", $body, 2); - } - $header = explode("\r\n", $header); - - unset($output); - - $status = (int)explode(' ', trim($header[0]))[1]; - $header = $this->headerFormat($header); - - return [$header, $this->resolve($header, $body), $status]; - } - - /** - * @param $url - * @param $data - * @return string - */ - private function createRequestUrl($url, $data) - { - if ($this->isGet()) { - return $url . '?' . $data; - } - return $url; - } - - /** - * @param $data - * @param $body - * @return mixed - */ - private function resolve($data, $body) - { - if (is_array($body)) { - return $body; - } - $type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html'; - if (strpos($type, 'text/html') !== false) { - return $body; - } else if (strpos($type, 'json') !== false) { - return json_decode($body, true); - } else if (strpos($type, 'xml') !== false) { - return Help::xmlToArray($body); - } else if (strpos($type, 'plain') !== false) { - return Help::toArray($body); - } - return $body; - } - - /** - * @param $headers - * @return array - */ - private function headerFormat($headers) - { - $_tmp = []; - foreach ($headers as $key => $val) { - $trim = explode(': ', trim($val)); - - $_tmp[strtolower($trim[0])] = $trim[1] ?? ''; - } - return $_tmp; - } - - /** - * @param $body - * @param $_data - * @param $header - * @param $statusCode - * @return array|mixed|Result - * 构建返回体 - */ - private function structure($body, $_data, $header = [], $statusCode = 200) - { - $this->setIsSSL(false); - $this->setHeaders([]); - - if ($this->callback !== NULL) { - $result = call_user_func($this->callback, $body, $_data, $header); - $this->setCallback(null); - - return $result; - } - if (is_string($body)) { - $result['code'] = 0; - $result['message'] = ''; - } else { - $result['code'] = $body[$this->errorCodeField] ?? 0; - $result['message'] = $this->searchMessageByData($body); - } - $result['data'] = $body; - $result['header'] = $header; - $result['httpStatus'] = $statusCode; - - return new Result($result); - } - - /** - * @param $body - * @return array|mixed|string - */ - private function searchMessageByData($body) - { - $parent = []; - if (empty($this->errorMsgField)) { - return 'system success.'; - } - $explode = explode('.', $this->errorMsgField); - if (!isset($body[$explode[0]])) { - return 'system success.'; - } - foreach ($explode as $item) { - if (empty($item)) { - continue; - } - if (empty($parent)) { - $parent = $body[$item]; - continue; - } - if (is_string($parent) || !isset($parent[$item])) { - break; - } - $parent = $parent[$item]; - } - return !empty($parent) ? $parent : 'system success.'; - } - - - /** - * @return bool - * check isPost Request - */ - public function isPost() - { - return strtolower($this->method) === self::POST; - } - - /** - * @return bool - * - * check isGet Request - */ - public function isGet() - { - return strtolower($this->method) === self::GET; - } - - /** - * @param $arr - * - * @return array|string - * 将请求参数进行编码 - */ - private function paramEncode($arr) - { - if (!is_array($arr)) { - return $arr; - } - $_tmp = []; - foreach ($arr as $Key => $val) { - $_tmp[$Key] = $val; - } - if ($this->isGet()) { - return http_build_query($_tmp); - } - return $_tmp; - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws - */ - public function post($url, $data = []) - { - $this->setMethod(self::POST); - return $this->request($url, $data); - } - - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws - */ - public function put($url, $data = []) - { - $this->setMethod(self::PUT); - return $this->request($url, $data); - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws - */ - public function get($url, $data = []) - { - $this->setMethod(self::GET); - return $this->request($url, $data); - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - */ - public function option($url, $data = []) - { - $this->setMethod(self::OPTIONS); - return $this->request($url, $data); - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - */ - public function delete($url, $data = []) - { - $this->setMethod(self::DELETE); - return $this->request($url, $data); - } - - /** - * @param $url - * @param array $data - * @return array|mixed|Result - * @throws Exception - */ - public function send($url, $data = []) - { - return $this->request($url, $data); - } - - /** - * @return array - */ - private function parseHeaderMat() - { - if ($this->use_swoole) { - return $this->header; - } - $headers = []; - foreach ($this->header as $key => $val) { - $header = $key . ':' . $val; - if (in_array($header, $headers)) { - continue; - } - $headers[] = $header; - } - $this->header = []; - return $headers; - } - - /** - * @param array $headers - * @return array - */ - public function setHeaders(array $headers) - { - if (empty($headers)) { - return []; - } - foreach ($headers as $key => $val) { - $this->header[$key] = $val; - } - return $this->header; - } + private string $host = ''; + + private array $header = []; + + private int $timeout = 0; + + private ?\Closure $callback = null; + private string $method = 'get'; + + private bool $isSSL = false; + private string $agent = ''; + private string $errorCodeField = ''; + private string $errorMsgField = ''; + private bool $use_swoole = false; + + private string $ssl_cert_file = ''; + private string $ssl_key_file = ''; + private string $ca = ''; + private int $port = 80; + + /** @var string $_message 错误信息 */ + private string $_message = ''; + private ?string $_data = ''; + + private int $connect_timeout = 1; + + const GET = 'get'; + const PUT = 'put'; + const POST = 'post'; + const DELETE = 'delete'; + const OPTIONS = 'option'; + + /** + * @return int + */ + public function getConnectTimeout(): int + { + return $this->connect_timeout; + } + + /** + * @param int $connect_timeout + */ + public function setConnectTimeout(int $connect_timeout): void + { + $this->connect_timeout = $connect_timeout; + } + + + /** + * @return string + */ + public function getCa(): string + { + return $this->ca; + } + + /** + * @param string $ca + */ + public function setCa(string $ca): void + { + $this->ca = $ca; + } + + + /** + * @return int + */ + public function getPort(): int + { + return $this->port; + } + + /** + * @param int $port + */ + public function setPort(int $port): void + { + $this->port = $port; + } + + + /** + * @param $data + */ + public function setData($data) + { + $this->_data = $data; + } + + /** + * @return string + */ + public function getSslCertFile(): string + { + return $this->ssl_cert_file; + } + + /** + * @return bool + */ + public function hasSslCertFile(): bool + { + return !empty($this->ssl_cert_file) && file_exists($this->ssl_cert_file); + } + + /** + * @return bool + */ + public function hasSslKeyFile(): bool + { + return !empty($this->ssl_key_file) && file_exists($this->ssl_key_file); + } + + /** + * @param string $ssl_cert_file + */ + public function setSslCertFile(string $ssl_cert_file) + { + $this->ssl_cert_file = $ssl_cert_file; + } + + /** + * @return string + */ + public function getSslKeyFile(): string + { + return $this->ssl_key_file; + } + + /** + * @param string $ssl_key_file + */ + public function setSslKeyFile(string $ssl_key_file) + { + $this->ssl_key_file = $ssl_key_file; + } + + /** + */ + public static function NewRequest() + { + return new HttpClient(); + } + + /** + * @param string $name + * @return $this + */ + public function setErrorField(string $name) + { + $this->errorCodeField = $name; + return $this; + } + + /** + * @param $bool + * @return $this + */ + public function setUseSwoole($bool) + { + $this->use_swoole = $bool; + if ($this->use_swoole) { + function_exists('setCli') && setCli(true); + } + return $this; + } + + /** + * @param string $name + * @return $this + */ + public function setErrorMsgField(string $name) + { + $this->errorMsgField = $name; + return $this; + } + + /** + * @param string $host + */ + public function setHost(string $host) + { + $this->host = $this->replaceHost($host); + $match_quest = '/^[a-zA-Z\-]+(\.[a-zA-Z\-])+/'; + if (preg_match($match_quest, $this->host)) { + $this->addHeader('Host', $this->host); + } + } + + + /** + * @param $path + * @param array $data + * @param int $type + * @return Result + */ + public function sendTo($path, array $data, $type = SWOOLE_TCP) + { + $client = new Client($type); + if (empty($this->host) || empty($this->port)) { + return new Result(['code' => 500, 'message' => 'Host and port is null']); + } + if (!$client->connect($this->host, $this->port, $this->connect_timeout)) { + return new Result(['code' => 500, 'message' => $client->errMsg]); + } + $params['body'] = $data; + $params['path'] = '/' . $this->port . '/' . ltrim($path, '/'); + $params['header']['request_uri'] = $params['path']; + $params['header']['request_method'] = 'receive'; + if ($client->send(serialize($params))) { + $recv = $this->timeout > 0 ? $client->recv($this->timeout) : $client->recv(); + $param = $this->structure(Help::toArray($recv), $data, null, 200); + } else { + $param = new Result(['code' => 500, 'message' => $client->errMsg]); + } + $client->close(); + return $param; + } + + /** + * @param int $sec + * 设置超时时间 + */ + public function setTimeout(int $sec) + { + $this->timeout = $sec; + } + + + /** + * @param $key + * @param $value + */ + public function setHeader($key, $value) + { + $this->header[$key] = $value; + } + + /** + * @param $key + * @param $value + */ + public function addHeader($key, $value) + { + $this->header[$key] = $value; + } + + /** + * @param null $callback + */ + public function setCallback($callback) + { + $this->callback = $callback; + } + + /** + * @param string $method + */ + public function setMethod(string $method) + { + $this->method = $method; + } + + /** + * @param string $agent + */ + public function setAgent(string $agent) + { + $this->agent = $agent; + } + + /** + * @param bool $isSSL + */ + public function setIsSSL(bool $isSSL) + { + $this->isSSL = $isSSL; + if ($this->isSSL) { + $this->port = 443; + } + } + + /** + * @return bool + */ + public function getIsSSL() + { + return $this->isSSL; + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + */ + private function request($url, $data = []) + { + $data = $this->paramEncode($data); + if ($this->use_swoole) { + return $this->coroutine($this->matchHost($url), $data); + } else { + return $this->useCurl($url, $data); + } + } + + /** + * @return bool + */ + private function isCli() + { + return function_exists('getIsCli') && getIsCli(); + } + + /** + * @param string $string + * @return bool|string + * @throws Exception + */ + private function matchHost($string = '') + { + if (empty($string)) { + return false; + } + + if ($this->isHttp($string)) { + $string = str_replace('http://', '', $string); + $hostAndUrls = explode('/', $string); + + $this->host = array_shift($hostAndUrls); + $string = implode('/', $hostAndUrls); + } else if ($this->isHttps($string)) { + $string = str_replace('https://', '', $string); + $this->setIsSSL(true); + + $hostAndUrls = explode('/', $string); + + $this->host = array_shift($hostAndUrls); + $string = implode('/', $hostAndUrls); + } else if (empty($this->host)) { + $hostAndUrls = explode('/', $string); + $this->host = array_shift($hostAndUrls); + + $string = implode('/', $hostAndUrls); + } + + if (strpos($this->host, ':') !== false) { + [$this->host, $this->port] = explode(':', $this->host); + } + + if ($this->use_swoole) { + return $string; + } + + if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) { + $this->host = System::gethostbyname($this->host); + } + + if (!$this->checkIsIp($this->host) && !$this->isDomainName($this->host)) { + throw new Exception('Client Host error.'); + } + + return $string; + } + + /** + * @param $name + * @return bool|mixed + */ + private function isDomainName($name) + { + if (!preg_match('/^[a-zA-Z\-0-9]+(\.[a-zA-Z\-0-9]+)+[^\/]?/', $name, $out)) { + return false; + } + return $out[0]; + } + + /** + * @param $url + * @param $data + * @return array|Result|mixed + * @throws + */ + private function useCurl($url, $data) + { + if ($this->isHttp($url) || $this->isHttps($url)) { + return $this->curl($url, $data); + } + $url = $this->matchHost(ltrim($url, '/')); + if (!empty($this->port)) { + $this->host .= ':' . $this->port; + } + if ($this->isSSL) { + return $this->curl('https://' . $this->host . '/' . $url, $data); + } else { + return $this->curl('http://' . $this->host . '/' . $url, $data); + } + } + + /** + * @param $host + * @return string|string[] + */ + private function replaceHost($host) + { + if ($this->isHttp($host)) { + return str_replace('http://', '', $host); + } + if ($this->isHttps($host)) { + return str_replace('https://', '', $host); + } + return $host; + } + + /** + * @param $url + * @return false|int + */ + private function checkIsIp($url) + { + return preg_match('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $url); + } + + /** + * @param $url + * @return bool + */ + private function isHttp($url) + { + return strpos($url, 'http://') === 0; + } + + /** + * @param $url + * @return bool + */ + private function isHttps($url) + { + return strpos($url, 'https://') === 0; + } + + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + * 使用swoole协程方式请求 + */ + private function coroutine($url, $data = []) + { + try { + $client = $this->generate_client($this->host, $url, $data); + if ($client->statusCode < 0) { + throw new Exception($client->errMsg); + } + unset($this->_data); + + $body = $this->resolve($client->getHeaders(), $client->body); + if (!in_array($client->getStatusCode(), [200, 201])) { + if (is_string($body)) { + $message = 'Request error code ' . $client->getStatusCode(); + } else { + $message = $this->searchMessageByData($body); + } + $response['code'] = $client->getStatusCode(); + $response['message'] = $message; + $response['data'] = $body; + $response['header'] = $client->getHeaders(); + + $response = new Result($response); + } else { + $response = $this->structure($body, $data, $client->getHeaders()); + } + } catch (\Throwable $exception) { + $response['code'] = 500; + $response['message'] = $exception->getMessage(); + $response['data'] = array_slice($exception->getTrace(), 0, 6); + $response['header'] = []; + + $response = new Result($response); + } + return $response; + } + + /** + * @return int + */ + private function getHostPort() + { + if (!empty($this->port)) { + return $this->port; + } + $port = 80; + if ($this->isSSL) $port = 443; + return $port; + } + + /** + * @param $host + * @param $url + * @param $data + * @return SClient + */ + private function generate_client($host, $url, $data = []) + { + $client = new SClient($host, $this->getHostPort(), $this->isSSL); + if (strpos($url, '/') !== 0) { + $url = '/' . $url; + } + + $client->set($this->settings()); + if (!empty($this->agent)) { + $this->header['User-Agent'] = $this->agent; + } + if (!empty($this->header)) { + $client->setHeaders($this->header); + } + $client->setMethod(strtoupper($this->method)); + if (strtolower($this->method) == self::GET && !empty($data)) { + $url .= '?' . $data; + } else { + $this->_data = $this->mergeParams($data); + } + + if (!empty($this->_data)) { + $client->setData($this->_data); + } + $client->execute($url); + $client->close(); + return $client; + } + + /** + * @param $newData + * @return mixed + */ + private function mergeParams($newData) + { + if (empty($this->_data)) { + return $this->toRequest($newData); + } else if (empty($newData)) { + return $this->toRequest($this->_data); + } + + $newData = Help::toArray($newData); + $array = Help::toArray($this->_data); + + $params = array_merge($array, $newData); + + return $this->toRequest($params); + } + + + /** + * @param $data + * @return false|mixed|string + */ + private function toRequest($data) + { + if (is_string($data)) { + return $data; + } + + $contentType = 'application/x-www-form-urlencoded'; + if (isset($this->header['Content-Type'])) { + $contentType = $this->header['Content-Type']; + } else if (isset($this->header['content-type'])) { + $contentType = $this->header['content-type']; + } + + if (strpos($contentType, 'json') !== false) { + return Help::toJson($data); + } else if (strpos($contentType, 'xml') !== false) { + return Help::toXml($data); + } else { + return http_build_query($data); + } + } + + /** + * @return array + */ + private function settings() + { + $sslCert = $this->getSslCertFile(); + $sslKey = $this->getSslKeyFile(); + $sslCa = $this->getCa(); + + $params = []; + if ($this->connect_timeout > 0) { + $params['timeout'] = $this->connect_timeout; + } + if (empty($sslCert) || empty($sslKey) || empty($sslCa)) { + return $params; + } + + $params['ssl_host_name'] = $this->host; + $params['ssl_cert_file'] = $this->getSslCertFile(); + $params['ssl_key_file'] = $this->getSslKeyFile(); + $params['ssl_verify_peer'] = true; + $params['ssl_cafile'] = $sslCa; + + return $params; + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + */ + private function curl($url, $data = []) + { + try { + $output = $this->curlParse($url, $this->mergeParams($data)); + if ($output === FALSE) { + return new Result(['code' => 500, 'message' => $output]); + } + [$header, $body, $status] = $this->explode($output); + if (!in_array($status, [200, 201])) { + $data = new Result(['code' => $status, 'message' => $body, 'header' => $header]); + } else { + $data = $this->structure($body, $data, $header); + } + return $data; + } catch (\Throwable $exception) { + $response['code'] = 500; + $response['message'] = $exception->getMessage(); + $response['data'] = array_slice($exception->getTrace(), 0, 6); + $response['header'] = []; + return new Result($response); + } + } + + /** + * @param $url + * @param $data + * @return bool|string + * @throws Exception + */ + private function curlParse($url, $data) + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data)); + if ($this->timeout > 0) { + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); // 超时设置 + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); // 超时设置 + } + curl_setopt($ch, CURLOPT_HEADER, true); + + if ($headers = $this->parseHeaderMat()) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + } + if (!empty($this->agent)) { + curl_setopt($ch, CURLOPT_USERAGENT, $this->agent); + } + if (file_exists($cert = $this->getSslCertFile())) { + curl_setopt($ch, CURLOPT_SSLCERT, $cert); + } + if (file_exists($key = $this->getSslKeyFile())) { + curl_setopt($ch, CURLOPT_SSLKEY, $key); + } + + curl_setopt($ch, CURLOPT_NOBODY, FALSE); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// 跟踪重定向 + curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); + + if ($this->method == self::POST) { + curl_setopt($ch, CURLOPT_POST, 1); + } + + if ($this->method != self::GET) { + curl_setopt($ch, CURLOPT_POSTFIELDS, $data); + } + + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($this->method)); + $output = curl_exec($ch); + if ($output === false) { + throw new Exception(curl_error($ch)); + } + curl_close($ch); + return $output; + } + + + /** + * @param $url + * @param $params + * @return array|mixed|Result + * 上传文件 + */ + public function upload($url, $params) + { + try { + $this->method = self::POST; + $output = $this->curlParse($url, $params); + if ($output === FALSE) { + return new Result(['code' => 500, 'message' => $output]); + } + [$header, $body, $status] = $this->explode($output); + if ($status != 200 && $status != 201) { + $data = new Result(['code' => $status, 'message' => $body, 'header' => $header]); + } else { + $data = $this->structure($body, $params, $header); + } + return $data; + } catch (\Throwable $exception) { + $response['code'] = 500; + $response['message'] = $exception->getMessage(); + $response['data'] = array_slice($exception->getTrace(), 0, 6); + $response['header'] = []; + return new Result($response); + } + } + + + /** + * @param $output + * @return array + */ + private function explode($output) + { + [$header, $body] = explode("\r\n\r\n", $output, 2); + if ($header == 'HTTP/1.1 100 Continue') { + [$header, $body] = explode("\r\n\r\n", $body, 2); + } else if (strpos($body, "\r\n\r\n") !== false) { + [$header, $body] = explode("\r\n\r\n", $body, 2); + } + $header = explode("\r\n", $header); + + unset($output); + + $status = (int)explode(' ', trim($header[0]))[1]; + $header = $this->headerFormat($header); + + return [$header, $this->resolve($header, $body), $status]; + } + + /** + * @param $url + * @param $data + * @return string + */ + private function createRequestUrl($url, $data) + { + if ($this->isGet()) { + return $url . '?' . $data; + } + return $url; + } + + /** + * @param $data + * @param $body + * @return mixed + */ + private function resolve($data, $body) + { + if (is_array($body)) { + return $body; + } + $type = $data['content-type'] ?? $data['Content-Type'] ?? 'text/html'; + if (strpos($type, 'text/html') !== false) { + return $body; + } else if (strpos($type, 'json') !== false) { + return json_decode($body, true); + } else if (strpos($type, 'xml') !== false) { + return Help::xmlToArray($body); + } else if (strpos($type, 'plain') !== false) { + return Help::toArray($body); + } + return $body; + } + + /** + * @param $headers + * @return array + */ + private function headerFormat($headers) + { + $_tmp = []; + foreach ($headers as $key => $val) { + $trim = explode(': ', trim($val)); + + $_tmp[strtolower($trim[0])] = $trim[1] ?? ''; + } + return $_tmp; + } + + /** + * @param $body + * @param $_data + * @param $header + * @param $statusCode + * @return array|mixed|Result + * 构建返回体 + */ + private function structure($body, $_data, $header = [], $statusCode = 200) + { + $this->setIsSSL(false); + $this->setHeaders([]); + + if ($this->callback !== NULL) { + $result = call_user_func($this->callback, $body, $_data, $header); + $this->setCallback(null); + + return $result; + } + if (is_string($body)) { + $result['code'] = 0; + $result['message'] = ''; + } else { + $result['code'] = $body[$this->errorCodeField] ?? 0; + $result['message'] = $this->searchMessageByData($body); + } + $result['data'] = $body; + $result['header'] = $header; + $result['httpStatus'] = $statusCode; + + return new Result($result); + } + + /** + * @param $body + * @return array|mixed|string + */ + private function searchMessageByData($body) + { + $parent = []; + if (empty($this->errorMsgField)) { + return 'system success.'; + } + $explode = explode('.', $this->errorMsgField); + if (!isset($body[$explode[0]])) { + return 'system success.'; + } + foreach ($explode as $item) { + if (empty($item)) { + continue; + } + if (empty($parent)) { + $parent = $body[$item]; + continue; + } + if (is_string($parent) || !isset($parent[$item])) { + break; + } + $parent = $parent[$item]; + } + return !empty($parent) ? $parent : 'system success.'; + } + + + /** + * @return bool + * check isPost Request + */ + public function isPost() + { + return strtolower($this->method) === self::POST; + } + + /** + * @return bool + * + * check isGet Request + */ + public function isGet() + { + return strtolower($this->method) === self::GET; + } + + /** + * @param $arr + * + * @return array|string + * 将请求参数进行编码 + */ + private function paramEncode($arr) + { + if (!is_array($arr)) { + return $arr; + } + $_tmp = []; + foreach ($arr as $Key => $val) { + $_tmp[$Key] = $val; + } + if ($this->isGet()) { + return http_build_query($_tmp); + } + return $_tmp; + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws + */ + public function post($url, $data = []) + { + $this->setMethod(self::POST); + return $this->request($url, $data); + } + + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws + */ + public function put($url, $data = []) + { + $this->setMethod(self::PUT); + return $this->request($url, $data); + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws + */ + public function get($url, $data = []) + { + $this->setMethod(self::GET); + return $this->request($url, $data); + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + */ + public function option($url, $data = []) + { + $this->setMethod(self::OPTIONS); + return $this->request($url, $data); + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + */ + public function delete($url, $data = []) + { + $this->setMethod(self::DELETE); + return $this->request($url, $data); + } + + /** + * @param $url + * @param array $data + * @return array|mixed|Result + * @throws Exception + */ + public function send($url, $data = []) + { + return $this->request($url, $data); + } + + /** + * @return array + */ + private function parseHeaderMat() + { + if ($this->use_swoole) { + return $this->header; + } + $headers = []; + foreach ($this->header as $key => $val) { + $header = $key . ':' . $val; + if (in_array($header, $headers)) { + continue; + } + $headers[] = $header; + } + $this->header = []; + return $headers; + } + + /** + * @param array $headers + * @return array + */ + public function setHeaders(array $headers) + { + if (empty($headers)) { + return []; + } + foreach ($headers as $key => $val) { + $this->header[$key] = $val; + } + return $this->header; + } } From 13a56f3555bde2ee65e11c69a2a4d97ca781811e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:04:25 +0800 Subject: [PATCH 10/16] 3 --- wchat/common/HttpClient.php | 4 --- wchat/qq/pay/Cash_Bonus.php | 30 +++++++++++----------- wchat/wx/Account.php | 14 +++------- wchat/wx/Message.php | 51 +++++++++++++++++-------------------- wchat/wx/Recharge.php | 8 +++--- wchat/wx/SecCheck.php | 25 +++++++++++++----- wchat/wx/SmallProgram.php | 9 ++++--- wchat/wx/Subject.php | 6 ++++- wchat/wx/Template.php | 2 +- 9 files changed, 75 insertions(+), 74 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 88f072c..33fe097 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -363,10 +363,6 @@ class HttpClient [$this->host, $this->port] = explode(':', $this->host); } - if ($this->use_swoole) { - return $string; - } - if (!$this->checkIsIp($this->host) && Coroutine::getuid() > 0) { $this->host = System::gethostbyname($this->host); } diff --git a/wchat/qq/pay/Cash_Bonus.php b/wchat/qq/pay/Cash_Bonus.php index fd042cc..4fd4375 100644 --- a/wchat/qq/pay/Cash_Bonus.php +++ b/wchat/qq/pay/Cash_Bonus.php @@ -17,23 +17,23 @@ use wchat\wx\SmallProgram; class Cash_Bonus extends SmallProgram { - public $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi'; + public string $_cash = '/cgi-bin/hongbao/qpay_hb_mch_send.cgi'; - private $_errors = [ - '66228701' => '红包个数超出限制', - '66228705' => '总金额超出限制', - '66228706' => '总金额不足以按最小金额领取每个红包', - '66228707' => '商户签名校验失败', - '66228708' => '重入??', - '66228709' => 'openid转换uin失败', - '66228711' => '商户订单中的商户号有误', - '66228712' => '商户订单中的日期超过范围', - '66228713' => '余额不足', - '66228715' => '用户未关注公众号,发送AIO消息失败', - '66229716' => '用户禁用公众号,发AIO消息失败' - ]; + private array $_errors = [ + '66228701' => '红包个数超出限制', + '66228705' => '总金额超出限制', + '66228706' => '总金额不足以按最小金额领取每个红包', + '66228707' => '商户签名校验失败', + '66228708' => '重入??', + '66228709' => 'openid转换uin失败', + '66228711' => '商户订单中的商户号有误', + '66228712' => '商户订单中的日期超过范围', + '66228713' => '余额不足', + '66228715' => '用户未关注公众号,发送AIO消息失败', + '66229716' => '用户禁用公众号,发AIO消息失败' + ]; - private $_requestParams = []; + private array $_requestParams = []; /** * @param string $value diff --git a/wchat/wx/Account.php b/wchat/wx/Account.php index 6b1f92f..cb89999 100644 --- a/wchat/wx/Account.php +++ b/wchat/wx/Account.php @@ -15,19 +15,13 @@ use wchat\common\Result; class Account extends SmallProgram { - private $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='; - private $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='; - private $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='; + private string $wxaqr = 'https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token='; + private string $getwxacode = 'https://api.weixin.qq.com/wxa/getwxacode?access_token='; + private string $getwxacodeunlimit = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='; - private $savePath = __DIR__ . '/../../Users/'; + private string $savePath = __DIR__ . '/../../Users/'; - private $OK = 0; - private $IllegalAesKey = -41001; - private $IllegalIv = -41002; - private $IllegalBuffer = -41003; - private $DecodeBase64Error = -41004; - /** * @param $path */ diff --git a/wchat/wx/Message.php b/wchat/wx/Message.php index 861a0ba..8beaeb2 100644 --- a/wchat/wx/Message.php +++ b/wchat/wx/Message.php @@ -3,21 +3,18 @@ namespace wchat\wx; +use Exception; use wchat\common\HttpClient; use wchat\common\Result; + +/** + * Class Message + * @package wchat\wx + */ class Message extends SmallProgram { - const TEXT = 0; - const IMAGE = 1; - const VOICE = 2; - const NEWS = 3; - const VIDEO = 4; - const MUSIC = 5; - const MINIPROGRAMPAGE = 6; - const WXCARD = 7; - private string $openid = ''; private array $msgData = []; @@ -26,14 +23,13 @@ class Message extends SmallProgram */ public function setOpenid(string $openid) { - $this->openid = $openid; $this->msgData['touser'] = $openid; } /** * @param string $content * @return Result - * @throws \Exception + * @throws Exception */ public function sendTextNews(string $content) { @@ -46,7 +42,7 @@ class Message extends SmallProgram /** * @param $media_id * @return Result - * @throws \Exception + * @throws Exception */ public function sendImageNews(string $media_id) { @@ -60,7 +56,7 @@ class Message extends SmallProgram /** * @param $media_id * @return Result - * @throws \Exception + * @throws Exception */ public function sendVoiceNews(string $media_id) { @@ -73,7 +69,7 @@ class Message extends SmallProgram /** * @param $media_id * @return Result - * @throws \Exception + * @throws Exception */ public function sendMpNewsNews(string $media_id) { @@ -90,7 +86,7 @@ class Message extends SmallProgram * @param string $url * @param string $picurl * @return Result - * @throws \Exception + * @throws Exception */ public function sendNewsNews(string $title, string $description, string $url, string $picurl) { @@ -108,7 +104,7 @@ class Message extends SmallProgram /** * @param string $title * @return Result - * @throws \Exception + * @throws Exception */ public function sendCardNews(string $title) { @@ -125,7 +121,7 @@ class Message extends SmallProgram * @param string $title * @param string $description * @return Result - * @throws \Exception + * @throws Exception */ public function sendVideoNews(string $media_id, string $thumb_media_id, string $title, string $description) { @@ -147,7 +143,7 @@ class Message extends SmallProgram * @param string $title * @param string $description * @return Result - * @throws \Exception + * @throws Exception */ public function sendMusicNews(string $musicurl, string $hqmusicurl, string $thumb_media_id, string $title, string $description) { @@ -168,7 +164,7 @@ class Message extends SmallProgram * @param string $tail_content * @param array $menus * @return Result - * @throws \Exception + * @throws Exception */ public function sendMenuNews(string $head_content, string $tail_content, array $menus = []) { @@ -178,7 +174,7 @@ class Message extends SmallProgram 'tail_content' => $tail_content, ]; if (empty($menus) || !is_array($menus) || count($menus) < 2) { - throw new \Exception('菜单选项必须有2个'); + throw new Exception('菜单选项必须有2个'); } foreach ($menus as $key => $val) { $this->addNewsMenu($val['id'], $val['name']); @@ -208,7 +204,7 @@ class Message extends SmallProgram * @param $pagepath * @param $thumb_media_id * @return Result - * @throws \Exception + * @throws Exception */ public function sendMiniprogrampageNews(string $title, string $appid, string $pagepath, string $thumb_media_id) { @@ -229,16 +225,16 @@ class Message extends SmallProgram * @param string $title * @param string $introduction * @return mixed - * @throws \Exception + * @throws Exception */ public function upload(string $filePath, string $type, $isPermanent = false, string $title = '', string $introduction = '') { if (!file_exists($filePath)) { - throw new \Exception('文件不存在'); + throw new Exception('文件不存在'); } if (!in_array($type, ['image', 'voice', 'video', 'thumb'])) { - throw new \Exception('暂不支持的文件类型'); + throw new Exception('暂不支持的文件类型'); } $token = $this->getAccessToken(); @@ -262,7 +258,7 @@ class Message extends SmallProgram /** @var Result $body */ $data = $this->request->post($url, $data); if (!$data->isResultsOK()) { - throw new \Exception($data->getMessage()); + throw new Exception($data->getMessage()); } return $data->getData(); @@ -279,9 +275,8 @@ class Message extends SmallProgram /** - * @param $data * @return Result - * @throws \Exception + * @throws Exception */ private function sendKefuMsg() { @@ -299,7 +294,7 @@ class Message extends SmallProgram $body = $this->request->post($url, $data); if (!$body->isResultsOK()) { - throw new \Exception($body->getMessage()); + throw new Exception($body->getMessage()); } return $body; } diff --git a/wchat/wx/Recharge.php b/wchat/wx/Recharge.php index c2d8645..46c3128 100644 --- a/wchat/wx/Recharge.php +++ b/wchat/wx/Recharge.php @@ -18,14 +18,14 @@ use wchat\common\Help; */ class Recharge extends SmallProgram { - private $money = 0; + private int $money = 0; private $orderNo; - private $data = []; + private array $data = []; - private $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; - private $uniformed = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; + private string $transfers = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; + private string $uniformed = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; /** * @param int $money diff --git a/wchat/wx/SecCheck.php b/wchat/wx/SecCheck.php index 2dd17fe..50f1d57 100644 --- a/wchat/wx/SecCheck.php +++ b/wchat/wx/SecCheck.php @@ -13,11 +13,11 @@ use wchat\common\Result; class SecCheck extends SmallProgram { - private $_url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token='; + private string $_url = '/wxa/img_sec_check?access_token='; - private $_msgUrl = 'https://api.weixin.qq.com/wxa/msg_sec_check?access_token='; + private string $_msgUrl = '/wxa/msg_sec_check?access_token='; - private $_mediaCheckAsync = 'https://api.weixin.qq.com/wxa/media_check_async?access_token='; + private string $_mediaCheckAsync = '/wxa/media_check_async?access_token='; const MEDIA_VIDEO = 1; const MEDIA_IMAGE = 1; @@ -32,6 +32,8 @@ class SecCheck extends SmallProgram return $this->sendError('文件不存在', 404); } $this->request->setUseSwoole(false); + $this->request->setIsSSL(true); + $this->request->setHost('api.weixin.qq.com'); $this->request->setHeader('Content-Type', 'multipart/form-data'); $this->request->setErrorField('errcode'); $this->request->setErrorMsgField('errmsg'); @@ -50,10 +52,14 @@ class SecCheck extends SmallProgram if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) { throw new \Exception('暂不支持的文件类型'); } - $this->request->setHeader('Content-Type', 'application/json'); + $this->request->setIsSSL(true); + $this->request->setHost('api.weixin.qq.com'); + $this->request->setHeader('Content-Type', 'application/json'); $this->request->setErrorField('errcode'); $this->request->setErrorMsgField('errmsg'); - $response = $this->request->post($this->_mediaCheckAsync . $this->config->getAccessToken(), [ + + $requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken(); + $response = $this->request->post($requestUrl, [ 'media_url' => $url, 'media_type' => $type ]); @@ -83,10 +89,15 @@ class SecCheck extends SmallProgram if (empty($content)) { return $this->sendError('文件不存在', 404); } - $this->request->setHeader('Content-Type', 'application/json'); + $this->request->setIsSSL(true); + $this->request->setHost('api.weixin.qq.com'); + $this->request->setHeader('Content-Type', 'application/json'); $this->request->setErrorField('errcode'); $this->request->setErrorMsgField('errmsg'); - return $this->request->post($this->_msgUrl . $this->config->getAccessToken(), ['content' => $content]); + + $requestUrl = $this->_msgUrl . $this->config->getAccessToken(); + + return $this->request->post($requestUrl, ['content' => $content]); } } diff --git a/wchat/wx/SmallProgram.php b/wchat/wx/SmallProgram.php index 46d231c..ad4d85f 100644 --- a/wchat/wx/SmallProgram.php +++ b/wchat/wx/SmallProgram.php @@ -14,10 +14,11 @@ class SmallProgram extends Miniprogarampage private $url = 'https://api.weixin.qq.com/cgi-bin/token'; - /** - * @return mixed - * @throws \Exception - */ + /** + * @param bool $get_token + * @return mixed + * @throws \Exception + */ public function generateAccess_token($get_token = false) { if (!empty($this->config->getToken())) { diff --git a/wchat/wx/Subject.php b/wchat/wx/Subject.php index 17316fb..7b8534f 100644 --- a/wchat/wx/Subject.php +++ b/wchat/wx/Subject.php @@ -6,10 +6,14 @@ namespace wchat\wx; use wchat\common\Result; +/** + * Class Subject + * @package wchat\wx + */ class Subject extends \wchat\base\Subject { - private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send'; + private string $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send'; /** * @return string diff --git a/wchat/wx/Template.php b/wchat/wx/Template.php index 0f0e4f2..d3acf34 100644 --- a/wchat/wx/Template.php +++ b/wchat/wx/Template.php @@ -14,7 +14,7 @@ namespace wchat\wx; */ class Template extends \wchat\base\Template { - private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send'; + private string $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send'; public function getUrl(): string { From 8f8161a78022356a65eec4aa88c59d21c6c9f07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:05:25 +0800 Subject: [PATCH 11/16] 3 --- wchat/common/HttpClient.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 33fe097..740fb63 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -652,6 +652,9 @@ class HttpClient private function curlParse($url, $data) { $ch = curl_init(); + + var_dump($this->createRequestUrl($url, $data)); + curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data)); if ($this->timeout > 0) { curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); // 超时设置 From 6e488f87b11d80e48dc0faa9eb07a194dff1b542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:06:42 +0800 Subject: [PATCH 12/16] 3 --- wchat/common/HttpClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 740fb63..cce326f 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -402,9 +402,9 @@ class HttpClient $this->host .= ':' . $this->port; } if ($this->isSSL) { - return $this->curl('https://' . $this->host . '/' . $url, $data); + return $this->curl($this->host . '/' . $url, $data); } else { - return $this->curl('http://' . $this->host . '/' . $url, $data); + return $this->curl($this->host . '/' . $url, $data); } } From 63d0639b8501437e766549709f3d88a485186159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:07:25 +0800 Subject: [PATCH 13/16] 3 --- wchat/common/HttpClient.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index cce326f..3fbce0e 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -662,6 +662,8 @@ class HttpClient } curl_setopt($ch, CURLOPT_HEADER, true); + var_dump($this->parseHeaderMat()); + if ($headers = $this->parseHeaderMat()) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } From 411bd5f0175d190d66da3614a4b1059369c331dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:08:43 +0800 Subject: [PATCH 14/16] 3 --- wchat/common/HttpClient.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 3fbce0e..6f3131d 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -401,11 +401,7 @@ class HttpClient if (!empty($this->port)) { $this->host .= ':' . $this->port; } - if ($this->isSSL) { - return $this->curl($this->host . '/' . $url, $data); - } else { - return $this->curl($this->host . '/' . $url, $data); - } + return $this->curl('http://' . $this->host . '/' . $url, $data); } /** From 016885ba166ea4d3b584ba8d2f19f9b900526aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:11:17 +0800 Subject: [PATCH 15/16] 3 --- wchat/common/HttpClient.php | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 6f3131d..2fca7b9 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -648,30 +648,18 @@ class HttpClient private function curlParse($url, $data) { $ch = curl_init(); - - var_dump($this->createRequestUrl($url, $data)); - curl_setopt($ch, CURLOPT_URL, $this->createRequestUrl($url, $data)); if ($this->timeout > 0) { curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); // 超时设置 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); // 超时设置 } curl_setopt($ch, CURLOPT_HEADER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $this->parseHeaderMat()); - var_dump($this->parseHeaderMat()); - - if ($headers = $this->parseHeaderMat()) { - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } + $this->setSslFile($ch); if (!empty($this->agent)) { curl_setopt($ch, CURLOPT_USERAGENT, $this->agent); } - if (file_exists($cert = $this->getSslCertFile())) { - curl_setopt($ch, CURLOPT_SSLCERT, $cert); - } - if (file_exists($key = $this->getSslKeyFile())) { - curl_setopt($ch, CURLOPT_SSLKEY, $key); - } curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//返回内容 @@ -681,7 +669,6 @@ class HttpClient if ($this->method == self::POST) { curl_setopt($ch, CURLOPT_POST, 1); } - if ($this->method != self::GET) { curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } @@ -696,6 +683,20 @@ class HttpClient } + /** + * @param resource $ch + */ + private function setSslFile($ch) + { + if (file_exists($cert = $this->getSslCertFile())) { + curl_setopt($ch, CURLOPT_SSLCERT, $cert); + } + if (file_exists($key = $this->getSslKeyFile())) { + curl_setopt($ch, CURLOPT_SSLKEY, $key); + } + } + + /** * @param $url * @param $params @@ -986,7 +987,7 @@ class HttpClient } $headers = []; foreach ($this->header as $key => $val) { - $header = $key . ':' . $val; + $header = $key . ': ' . $val; if (in_array($header, $headers)) { continue; } From 837fc72a10d74e3ccff494f474f549df6e9bc682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ms=C2=B7xl?= Date: Sat, 14 Nov 2020 03:14:44 +0800 Subject: [PATCH 16/16] 3 --- wchat/common/HttpClient.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/wchat/common/HttpClient.php b/wchat/common/HttpClient.php index 2fca7b9..26f3c9d 100644 --- a/wchat/common/HttpClient.php +++ b/wchat/common/HttpClient.php @@ -337,7 +337,9 @@ class HttpClient if (empty($string)) { return false; } - + if (!$this->use_swoole) { + return $this->host . '/' . ltrim($string, '/'); + } if ($this->isHttp($string)) { $string = str_replace('http://', '', $string); $hostAndUrls = explode('/', $string); @@ -398,10 +400,14 @@ class HttpClient return $this->curl($url, $data); } $url = $this->matchHost(ltrim($url, '/')); - if (!empty($this->port)) { - $this->host .= ':' . $this->port; +// if (!empty($this->port)) { +// $this->host .= ':' . $this->port; +// } + if ($this->isSSL) { + return $this->curl('https://' . $url, $data); + } else { + return $this->curl('http://' . $url, $data); } - return $this->curl('http://' . $this->host . '/' . $url, $data); } /**