├── LICENSE ├── README.md ├── composer.json └── ultramsg.class.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 ultramsg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [Ultramsg.com](https://ultramsg.com/?utm_source=github&utm_medium=php&utm_campaign=api) WhatsApp API PHP SDK 2 | 3 | Lightweight PHP library for WhatsApp API to send the whatsappp messages in PHP provided by [Ultramsg.com](https://ultramsg.com/?utm_source=github&utm_medium=php&utm_campaign=api) 4 | 5 | # Installation 6 | 7 | Just download ultramsg.class.php or use Composer: 8 | 9 | ``` 10 | composer require ultramsg/whatsapp-php-sdk 11 | ``` 12 | 13 | 14 | # Example usage 15 | 16 | ```php 17 | sendChatMessage($to,$body); 28 | print_r($api); 29 | ``` 30 | > **NOTE:** you need replace instance_id and token with yours in [ultramsg.com](https://ultramsg.com/?utm_source=github&utm_medium=php&utm_campaign=api) account if you don't have account create one from [here](https://ultramsg.com/?utm_source=github&utm_medium=php&utm_campaign=api) 31 | 32 | # Youtube 33 | [![Send Message by WhatsApp api using PHP SDK | Ultramsg PHP SDK 34 | ](https://img.youtube.com/vi/OqDOKyMIp20/0.jpg)](https://www.youtube.com/watch?v=OqDOKyMIp20) 35 | 36 | 37 | ## Send Message 38 | * **$to** : your number for testing with international format e.g. +14155552671 or chatID for contact or group e.g 14155552671@c.us or 14155552671-441234567890@g.us 39 | * **$body** : Message text, UTF-8 or UTF-16 string with emoji . 40 | * **$priority** : This parameter is optional, 41 | 42 | You can use it to create a professional queue for messages, The Messages with less priority value are sent first. 43 | 44 | example of usage : 45 | 46 | priority = 0: for High priority like OTP messages. 47 | 48 | priority = 5: used with general messages. 49 | 50 | priority =10: Non-urgent promotional offers and notifications to your customers. 51 | 52 | **Default value** : 10 53 | * **$referenceId** : Your custom reference ID for this message. 54 | ```php 55 | $to="put_your_mobile_number_here"; 56 | $body="Hello world"; 57 | $priority=10; 58 | $referenceId="SDK"; 59 | $api=$client->sendChatMessage($to,$body,$priority,$referenceId); 60 | print_r($api); 61 | ``` 62 | 63 | ## Send Image 64 | * **$caption** : image Caption, UTF-8 or UTF-16 string with emoji . 65 | * **$image** : HTTP link image or base64-encoded file 66 | 67 | Supported extensions ( jpg , jpeg , gif , png , svg , webp , bmp ) . 68 | 69 | Max file size : 16MB . 70 | 71 | Max Base64 length : 10,000,000 72 | 73 | * **$nocache** : default false 74 | 75 | false : use a previously uploaded file instead of uploading it with each request 76 | 77 | true : uploading it each request 78 | 79 | ```php 80 | $to="put_your_mobile_number_here"; 81 | $image="https://file-example.s3-accelerate.amazonaws.com/images/test.jpg"; 82 | $caption="image Caption"; 83 | $priority=10; 84 | $referenceId="SDK"; 85 | $nocache=false; 86 | $api=$client->sendImageMessage($to,$image,$caption,$priority,$referenceId,$nocache); 87 | print_r($api); 88 | 89 | ``` 90 | ## Send Sticker 91 | * **$sticker** : HTTP link image or base64-encoded file 92 | 93 | Supported extensions ( jpg , jpeg , gif , png , svg , webp , bmp ) . 94 | 95 | Max file size : 16MB . 96 | 97 | Max Base64 length : 10,000,000 98 | 99 | * **$nocache** : default false 100 | 101 | false : use a previously uploaded file instead of uploading it with each request 102 | 103 | true : uploading it each request 104 | 105 | ```php 106 | $to="put_your_mobile_number_here"; 107 | $sticker="https://file-example.s3.us-west-2.amazonaws.com/sticker/1.webp"; 108 | $priority=10; 109 | $referenceId="SDK"; 110 | $nocache=false; 111 | $api=$client->sendStickerMessage($to,$sticker,$priority,$referenceId,$nocache); 112 | print_r($api); 113 | ``` 114 | 115 | ## Send Document 116 | * **$filename** : File name, for example 1.jpg or Hello.pdf 117 | * **$caption** : Document Caption, UTF-8 or UTF-16 string with emoji . 118 | * **$document** : HTTP link file or base64-encoded file 119 | 120 | Supported most extensions like ( zip , xlsx , csv , txt , pptx , docx ....etc ) . 121 | 122 | Max file size : 100MB . 123 | 124 | Max Base64 length : 10,000,000 125 | 126 | ```php 127 | $to="put_your_mobile_number_here"; 128 | $filename="File name"; 129 | $caption="Document Caption"; 130 | $document="https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf"; 131 | $api=$client->sendDocumentMessage($to,$filename,$document,$caption); 132 | print_r($api); 133 | ``` 134 | 135 | ## Send Audio 136 | * **$audio** : HTTP link audio or base64-encoded audio 137 | 138 | Supported extensions ( mp3 , aac , ogg ) . 139 | 140 | Max file size : 16MB . 141 | 142 | Max Base64 length : 10,000,000 143 | 144 | ```php 145 | $to="put_your_mobile_number_here"; 146 | $audio="https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"; 147 | $api=$client->sendAudioMessage($to,$audio); 148 | print_r($api); 149 | ``` 150 | ## Send Voice 151 | * **$audio** : HTTP link audio ogg-file with opus codec or base64 ogg-file in opus codec 152 | 153 | Max file size : 16MB . 154 | 155 | Max Base64 length : 10,000,000 156 | 157 | ```php 158 | $to="put_your_mobile_number_here"; 159 | $audio="https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg"; 160 | $api=$client->sendVoiceMessage($to,$audio); 161 | print_r($api); 162 | ``` 163 | 164 | ## Send Video 165 | * **$video** : HTTP link video or base64-encoded video 166 | 167 | Supported extensions ( mp4 , 3gp , mov ) . 168 | 169 | Max file size : 16MB . 170 | 171 | Max Base64 length : 10,000,000 172 | 173 | ```php 174 | $to="put_your_mobile_number_here"; 175 | $video="https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"; 176 | $caption="video Caption"; 177 | $api=$client->sendVideoMessage($to,$video,$caption); 178 | print_r($api); 179 | ``` 180 | ## Send Link 181 | * **$link** : HTTP or HTTPS link 182 | 183 | ```php 184 | $to="put_your_mobile_number_here"; 185 | $link="https://ultramsg.com"; 186 | $api=$client->sendLinkMessage($to,$link); 187 | print_r($api); 188 | ``` 189 | ## Send Contact 190 | * **$contact** :Contact ID or Contact IDs array example : 191 | 192 | Example 193 | 194 | 14000000001@c.us 195 | 196 | or 197 | 198 | 14000000001@c.us,14000000002@c.us,14000000003@c.us 199 | 200 | Max length : 300 char, almost 15 contacts 201 | ```php 202 | $to="put_your_mobile_number_here"; 203 | $contact="14000000001@c.us"; 204 | $api=$client->sendContactMessage($to,$contact); 205 | print_r($api); 206 | ``` 207 | ## Send Location 208 | * **$address** : Text under the location. 209 | 210 | Supports two lines. To use two lines, use the \n symbol. 211 | 212 | Max length : 300 char . 213 | * **$lat** : Latitude 214 | * **$lng** : longitude 215 | ```php 216 | $to="put_your_mobile_number_here"; 217 | $address="ABC company \n Sixth floor , office 38"; 218 | $lat="25.197197"; 219 | $lng="55.2721877"; 220 | $api=$client->sendLocationMessage($to,$address,$lat,$lng); 221 | print_r($api); 222 | ``` 223 | ## Send Vcard 224 | * **$vcard** : Text value vcard 3.0 225 | 226 | Max length : 4096 char 227 | 228 | ```php 229 | $to="put_your_mobile_number_here"; 230 | $vcard="BEGIN:VCARD 231 | VERSION:3.0 232 | N:lastname;firstname 233 | FN:firstname lastname 234 | TEL;TYPE=CELL;waid=14000000001:14000000002 235 | NICKNAME:nickname 236 | BDAY:01.01.1987 237 | X-GENDER:M 238 | NOTE:note 239 | ADR;TYPE=home 240 | ADR;TYPE=work 241 | END:VCARD"; 242 | $vcard = preg_replace("/[\n\r]/", "\n", $vcard); 243 | $api=$client->sendVcardMessage($to,$vcard); 244 | print_r($api); 245 | ``` 246 | 247 | ## Resend messages by status 248 | * **$status** : unsent or expired 249 | 250 | ```php 251 | $status="expired"; 252 | $api=$client->resendByStatus($status); 253 | print_r($api); 254 | ``` 255 | 256 | ## Resend message by ID 257 | * **$id** : message id 258 | 259 | ```php 260 | $id=123; 261 | $api=$client->resendById($id); 262 | print_r($api); 263 | ``` 264 | 265 | ## Get Messages 266 | get the messages that sent by api 267 | 268 | * **$page** : pagination page number 269 | * **$limit** : number of messages per request . max value : 100 . 270 | * **$status** : Messages status [sent , queue , unsent] 271 | - sent : get sent messages . 272 | - queue : get queue messages . 273 | - unsent : get unsent messages . 274 | - invalid : get invalid messages . 275 | - expired : get expired messages . 276 | - all : get all messages . 277 | * **$sort** : 278 | - asc : sorted messages by ID from smallest to largest . 279 | - desc : sorted messages by ID from largest to smallest . 280 | * **$id** : filter messages by message ID . 281 | * **$referenceId** : filter messages by your custom reference ID . 282 | * **$from** : filter messages by WhatsApp sender number e.g 14155552671@c.us . 283 | * **$to** : filter messages by recipient number e.g 14155552671@c.us or 14155552671-441234567890@g.us . 284 | * **$ack** : filter messages by message ack status [ pending , server , device , read , played ] . 285 | 286 | 287 | ```php 288 | $page=1; 289 | $limit=100; 290 | $status="all"; 291 | $sort="asc"; 292 | $id=""; 293 | $referenceId=""; 294 | $from=""; 295 | $to=""; 296 | $ack=""; 297 | $api=$client->getMessages($page,$limit,$status,$sort,$id,$referenceId,$from,$to,$ack); 298 | print_r($api); 299 | ``` 300 | 301 | ## Get Messages Statistics 302 | 303 | ```php 304 | $api=$client->getMessageStatistics(); 305 | print_r($api); 306 | ``` 307 | 308 | ## Get Instance Status 309 | 310 | ```php 311 | $api=$client->getInstanceStatus(); 312 | print_r($api); 313 | ``` 314 | 315 | ## Get Instance QR Image 316 | 317 | ```php 318 | header('Content-Type: image/png'); 319 | $api=$client->getInstanceQr(); 320 | print_r($api); 321 | ``` 322 | ## Get Instance QR Code 323 | 324 | ```php 325 | $api=$client->getInstanceQrCode(); 326 | print_r($api); 327 | ``` 328 | 329 | ## Get Instance Info 330 | Get connected phone informations : number , name , image etc.. 331 | ```php 332 | $api=$client->getInstanceMe(); 333 | print_r($api); 334 | ``` 335 | ## Get Instance Settings 336 | sendDelay : Delay in seconds between sending message, Default 1 second 337 | 338 | webhook_url : Http or https URL for receiving notifications . 339 | 340 | webhook_message_ack : on/off ack (message delivered and message viewed) notifications in webhooks. 341 | 342 | webhook_message_received : on/off notifications in webhooks when message received . 343 | 344 | webhook_message_create : on/off notifications in webhooks when message create . 345 | 346 | webhook_message_download_media : on/off to get received document / media files. 347 | ```php 348 | $api=$client->getInstanceSettings(); 349 | print_r($api); 350 | ``` 351 | 352 | ## Instance Takeover 353 | Returns the active session if the device has connected to another instance of Web WhatsApp 354 | 355 | ```php 356 | $api=$client->sendInstanceTakeover(); 357 | print_r($api); 358 | ``` 359 | ## Instance Logout 360 | Logout from WhatsApp Web to get new QR code. 361 | 362 | ```php 363 | $api=$client->sendInstanceLogout(); 364 | print_r($api); 365 | ``` 366 | ## Instance Restart 367 | Restart your instance. 368 | 369 | ```php 370 | $api=$client->sendInstanceRestart(); 371 | print_r($api); 372 | ``` 373 | ## Instance Settings Update 374 | * **sendDelay** : Delay in seconds between sending message . 375 | 376 | * **webhook_url** : Http or https URL for receiving notifications . 377 | 378 | * **webhook_message_received** : true/false notifications in webhooks when message received . 379 | 380 | * **webhook_message_create** : true/false notifications in webhooks when message create . 381 | 382 | * **webhook_message_ack** : true/false ack (message delivered and message viewed) notifications in webhooks. 383 | 384 | ```php 385 | $sendDelay=1; 386 | $webhook_url=""; 387 | $webhook_message_received=false; 388 | $webhook_message_create=false; 389 | $webhook_message_ack=false; 390 | $webhook_message_download_media=false; 391 | 392 | $api=$client->sendInstanceSettings($sendDelay,$webhook_url,$webhook_message_received,$webhook_message_create,$webhook_message_ack,$webhook_message_download_media); 393 | print_r($api); 394 | ``` 395 | 396 | ## Get the chats list 397 | 398 | ```php 399 | $api=$client->getChats(); 400 | print_r($api); 401 | ``` 402 | 403 | ## get last message from chat conversation 404 | 405 | * **$chatId** : chatID for contact or group e.g 14155552671@c.us or 14155552671-441234567890@g.us 406 | * **$limit** : number of messages per request . 407 | 408 | max value : 1000 . 409 | 410 | ```php 411 | $chatId="14155552671@c.us"; 412 | $limit=100; 413 | $api=$client->getChatsMessages($chatId,$limit); 414 | print_r($api); 415 | ``` 416 | 417 | 418 | ## Get the contacts list 419 | 420 | ```php 421 | $api=$client->getContacts(); 422 | print_r($api); 423 | ``` 424 | 425 | ## Get contact info 426 | 427 | * **$chatId** : chatID for contact e.g 14155552671@c.us 428 | 429 | ```php 430 | $chatId="14155552671@c.us"; 431 | $api=$client->getContact($chatId); 432 | print_r($api); 433 | ``` 434 | 435 | 436 | ## Gets all blocked contacts 437 | 438 | ```php 439 | $api=$client->getBlockedContacts(); 440 | print_r($api); 441 | ``` 442 | 443 | ## block contact from WhatsApp 444 | 445 | * **$chatId** : chatID for contact e.g 14155552671@c.us 446 | 447 | ```php 448 | $chatId="14155552671@c.us"; 449 | $api=$client->blockContact($chatId); 450 | print_r($api); 451 | ``` 452 | 453 | ## Unblock contact from WhatsApp 454 | 455 | * **$chatId** : chatID for contact e.g 14155552671@c.us 456 | 457 | ```php 458 | $chatId="14155552671@c.us"; 459 | $api=$client->unblockContact($chatId); 460 | print_r($api); 461 | ``` 462 | 463 | ## Check if number is WhatsApp user 464 | 465 | * **$chatId** : chatID for contact e.g 14155552671@c.us 466 | 467 | ```php 468 | $chatId="14155552671@c.us"; 469 | $api=$client->checkContact($chatId); 470 | print_r($api); 471 | ``` 472 | 473 | 474 | 475 | # Support 476 | Use **Issues** to contact me -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ultramsg/whatsapp-php-sdk", 3 | "description": "Lightweight PHP library for WhatsApp API to send the whatsappp messages in PHP provided by ultramsg.com", 4 | "keywords": [ 5 | "whatsapp-api", 6 | "whatsapp-bot", 7 | "whatsapp-chat", 8 | "whatsapp-api-php" 9 | ], 10 | "homepage": "https://ultramsg.com", 11 | "type": "library", 12 | "repositories": [ 13 | { 14 | "type": "vcs", 15 | "url": "https://github.com/ultramsg/whatsapp-php-sdk" 16 | } 17 | ], 18 | "version": "2.0.8", 19 | "license": "MIT", 20 | "authors": [ 21 | { 22 | "name": "ultramsg.com", 23 | "email": "info@ultramsg.com" 24 | } 25 | ], 26 | "autoload": { 27 | "classmap": [ 28 | "ultramsg.class.php" 29 | ] 30 | }, 31 | "require": {} 32 | } -------------------------------------------------------------------------------- /ultramsg.class.php: -------------------------------------------------------------------------------- 1 | token = $token; 20 | $this->instance_id = "instance".preg_replace('/[^0-9]/', '',$instance_id); 21 | 22 | } 23 | 24 | // messages 25 | public function getMessages($page=1,$limit=100,$status="all",$sort="asc",$id="",$referenceId="",$from="",$to="",$ack=""){ 26 | $params =array("page"=>$page,"limit"=>$limit,"status"=>$status,"sort"=>$sort,"id"=>$id,"referenceId"=>$referenceId,"from"=>$from,"to"=>$to,"ack"=>$ack); 27 | return $this->sendRequest("GET","messages",$params ); 28 | } 29 | 30 | public function getMessageStatistics(){ 31 | return $this->sendRequest("GET","messages/statistics"); 32 | } 33 | 34 | public function sendChatMessage($to,$body,$priority=10,$referenceId=""){ 35 | $params =array("to"=>$to,"body"=>$body,"priority"=>$priority,"referenceId"=>$referenceId); 36 | return $this->sendRequest("POST","messages/chat",$params ); 37 | } 38 | 39 | public function sendImageMessage($to,$image,$caption="",$priority=10,$referenceId="",$nocache=false){ 40 | $params =array("to"=>$to,"caption"=>$caption,"image"=>$image,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache); 41 | return $this->sendRequest("POST","messages/image",$params ); 42 | } 43 | public function sendStickerMessage($to,$sticker,$priority=10,$referenceId="",$nocache=false){ 44 | $params =array("to"=>$to,"sticker"=>$sticker,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache); 45 | return $this->sendRequest("POST","messages/sticker",$params ); 46 | } 47 | 48 | public function sendDocumentMessage($to,$filename,$document,$caption="",$priority=10,$referenceId="",$nocache=false){ 49 | $params =array("to"=>$to,"filename"=>$filename,"document"=>$document,"caption"=>$caption,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache); 50 | return $this->sendRequest("POST","messages/document",$params ); 51 | } 52 | 53 | public function sendAudioMessage($to,$audio,$priority=10,$referenceId="",$nocache=false){ 54 | $params =array("to"=>$to,"audio"=>$audio,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache); 55 | return $this->sendRequest("POST","messages/audio",$params ); 56 | } 57 | 58 | public function sendVoiceMessage($to,$audio,$priority=10,$referenceId="",$nocache=false){ 59 | $params =array("to"=>$to,"audio"=>$audio,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache); 60 | return $this->sendRequest("POST","messages/voice",$params ); 61 | } 62 | 63 | public function sendVideoMessage($to,$video,$caption="",$priority=10,$referenceId="",$nocache=false){ 64 | $params =array("to"=>$to,"caption"=>$caption,"video"=>$video,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache); 65 | return $this->sendRequest("POST","messages/video",$params ); 66 | } 67 | 68 | public function sendLinkMessage($to,$link,$priority=10,$referenceId=""){ 69 | $params =array("to"=>$to,"link"=>$link,"priority"=>$priority,"referenceId"=>$referenceId); 70 | return $this->sendRequest("POST","messages/link",$params ); 71 | } 72 | 73 | public function sendContactMessage($to,$contact,$priority=10,$referenceId=""){ 74 | $params =array("to"=>$to,"contact"=>$contact,"priority"=>$priority,"referenceId"=>$referenceId); 75 | return $this->sendRequest("POST","messages/contact",$params ); 76 | } 77 | public function sendLocationMessage($to,$address,$lat,$lng,$priority=10,$referenceId=""){ 78 | $params =array("to"=>$to,"address"=>$address,"lat"=>$lat,"lng"=>$lng,"priority"=>$priority,"referenceId"=>$referenceId); 79 | return $this->sendRequest("POST","messages/location",$params ); 80 | } 81 | public function sendVcardMessage($to,$vcard,$priority=10,$referenceId=""){ 82 | $params =array("to"=>$to,"vcard"=>$vcard,"priority"=>$priority,"referenceId"=>$referenceId); 83 | return $this->sendRequest("POST","messages/vcard",$params ); 84 | } 85 | public function sendClearMessage($status){ 86 | $params =array("status"=>$status); 87 | return $this->sendRequest("POST","messages/clear",$params ); 88 | } 89 | public function resendByStatus($status){ 90 | $params =array("status"=>$status); 91 | return $this->sendRequest("POST","messages/resendByStatus",$params ); 92 | } 93 | 94 | public function resendById($id){ 95 | $params =array("id"=>$id); 96 | return $this->sendRequest("POST","messages/resendById",$params ); 97 | } 98 | 99 | // instance 100 | 101 | public function getInstanceStatus(){ 102 | return $this->sendRequest("GET","instance/status"); 103 | } 104 | 105 | public function getInstanceQr(){ 106 | return $this->sendRequest("GET","instance/qr"); 107 | } 108 | 109 | public function getInstanceQrCode(){ 110 | return $this->sendRequest("GET","instance/qrCode"); 111 | } 112 | 113 | public function getInstanceScreenshot($encoding=""){ 114 | return $this->sendRequest("GET","instance/screenshot",array("encoding"=>$encoding)); 115 | } 116 | 117 | public function getInstanceMe(){ 118 | return $this->sendRequest("GET","instance/me"); 119 | } 120 | 121 | public function getInstanceSettings(){ 122 | return $this->sendRequest("GET","instance/settings"); 123 | } 124 | 125 | public function sendInstanceTakeover(){ 126 | return $this->sendRequest("POST","instance/takeover" ); 127 | } 128 | 129 | public function sendInstanceLogout(){ 130 | return $this->sendRequest("POST","instance/logout" ); 131 | } 132 | 133 | public function sendInstanceRestart(){ 134 | return $this->sendRequest("POST","instance/restart" ); 135 | } 136 | 137 | public function sendInstanceSettings($sendDelay="1",$webhook_url="",$webhook_message_received=false,$webhook_message_create=false,$webhook_message_ack=false,$webhook_message_download_media=false){ 138 | $params =array("sendDelay"=>$sendDelay,"webhook_url"=>$webhook_url,"webhook_message_received"=>json_encode($webhook_message_received),"webhook_message_create"=>json_encode($webhook_message_create),"webhook_message_ack"=>json_encode($webhook_message_ack),"webhook_message_download_media"=>json_encode($webhook_message_download_media)); 139 | return $this->sendRequest("POST","instance/settings",$params); 140 | } 141 | 142 | public function sendInstanceClear(){ 143 | return $this->sendRequest("POST","instance/clear" ); 144 | } 145 | 146 | // Chats 147 | 148 | public function getChats(){ 149 | return $this->sendRequest("GET","chats"); 150 | } 151 | 152 | public function getChatsMessages($chatId,$limit=100){ 153 | $params =array("chatId"=>$chatId,"limit"=>$limit); 154 | return $this->sendRequest("GET","chats/messages",$params); 155 | } 156 | 157 | // Contacts 158 | 159 | public function getContacts(){ 160 | return $this->sendRequest("GET","contacts"); 161 | } 162 | 163 | public function getContact($chatId){ 164 | $params =array("chatId"=>$chatId); 165 | return $this->sendRequest("GET","contacts/contact",$params); 166 | } 167 | 168 | public function getBlockedContacts(){ 169 | return $this->sendRequest("GET","contacts/blocked"); 170 | } 171 | 172 | public function checkContact($chatId){ 173 | $params =array("chatId"=>$chatId); 174 | return $this->sendRequest("GET","contacts/check",$params); 175 | } 176 | 177 | public function blockContact($chatId){ 178 | $params =array("chatId"=>$chatId); 179 | return $this->sendRequest("POST","contacts/block",$params); 180 | } 181 | 182 | public function unblockContact($chatId){ 183 | $params =array("chatId"=>$chatId); 184 | return $this->sendRequest("POST","contacts/unblock",$params); 185 | } 186 | 187 | public function sendRequest($method,$path,$params=array()){ 188 | 189 | if(!is_callable('curl_init')){ 190 | return array("Error"=>"cURL extension is disabled on your server"); 191 | } 192 | $url="https://api.ultramsg.com/".$this->instance_id."/".$path; 193 | $params['token'] = $this->token; 194 | $data=http_build_query($params); 195 | if(strtolower($method)=="get")$url = $url . '?' . $data; 196 | $curl = curl_init($url); 197 | if(strtolower($method)=="post"){ 198 | curl_setopt($curl, CURLOPT_POST, true); 199 | curl_setopt($curl, CURLOPT_POSTFIELDS,$data); 200 | } 201 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 202 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 203 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 204 | curl_setopt($curl, CURLOPT_HEADER, 1); 205 | $response = curl_exec($curl); 206 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 207 | if($httpCode == 404) { 208 | return array("Error"=>"instance not found or pending please check you instance id"); 209 | } 210 | $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); 211 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 212 | $header = substr($response, 0, $header_size); 213 | $body = substr($response, $header_size); 214 | curl_close($curl); 215 | 216 | if (strpos($contentType,'application/json') !== false) { 217 | return json_decode($body,true); 218 | } 219 | return $body; 220 | } 221 | 222 | 223 | 224 | 225 | } --------------------------------------------------------------------------------