✏️ 正在编辑: Whatsapp.php
路径:
/srv/systems_dir/koruspay/library/Core/Whatsapp.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_Whatsapp { private $_token = '6cc81c9936ceb71ee3787d2440f51f66e2a01da1';//válido private $_endpoint = ''; private $_service = null; public function getToken() { return $this->_token; } public function getEndpoint() { return $this->_endpoint; } public function getService() { return $this->_service; } public function setToken($token) { $this->_token = $token; } public function setEndpoint($endpoint) { $this->_endpoint = $endpoint; } public function setService($service) { $this->_service = $service; } public function __construct($endpoint) { $this->_endpoint = $endpoint; $this->_service = $this->listServices(); } public function sendMessage($phoneNumber, $text) { $url = "https://{$this->_endpoint}-api.digisac.app/v1/messages"; $params = http_build_query([ 'number' => '55' . $phoneNumber, 'serviceId' => $this->getService()->id, 'text' => $text, 'dontOpenTicket' => true ]); $response = $this->sendRequest($url, 'POST', $params); if( !empty($response->error) ){ throw new Core_Exception($response->message); } return $response; } protected function listServices() { $url = "https://{$this->_endpoint}-api.digisac.app/v1/services"; $services = $this->sendRequest($url, 'GET'); if( empty($services) ){ throw new Core_Exception("Endpoint da API não localizado!"); } if( !empty($services->error) ){ throw new Core_Exception($services->message); } return $services->data[0]; } protected function sendRequest($url, $method, $params = '') { $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POSTFIELDS => $params, CURLOPT_HTTPHEADER => [ "Authorization: Bearer " . $this->getToken(), ], ]); $response = curl_exec($curl); curl_close($curl); return json_decode($response); } }
💾 保存文件
← 返回文件管理器