├── composer.json ├── LICENSE ├── ultramsg.class.php └── README.md /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 | "type": "library", 5 | "repositories": [ 6 | { 7 | "type": "vcs", 8 | "url": "https://github.com/ultramsg/whatsapp-php-sdk" 9 | } 10 | ], 11 | "version": "2.0.1", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "ultramsg.com", 16 | "email": "info@ultramsg.com" 17 | } 18 | ], 19 | "autoload": { 20 | "classmap": ["ultramsg.class.php"] 21 | }, 22 | "require": {} 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /ultramsg.class.php: -------------------------------------------------------------------------------- 1 | token = $token; 20 | $this->instance_id = $instance_id; 21 | if(ctype_digit($instance_id)){ 22 | $this->instance_id = "instance".$instance_id; 23 | } 24 | } 25 | 26 | // messages 27 | public function getMessages($page=1,$limit=100,$status="all",$sort="asc"){ 28 | $params =array("page"=>$page,"limit"=>$limit,"status"=>$status,"sort"=>$sort); 29 | return $this->sendRequest("GET","messages",$params ); 30 | } 31 | 32 | public function getMessageStatistics(){ 33 | return $this->sendRequest("GET","messages/statistics"); 34 | } 35 | 36 | public function sendChatMessage($to,$body,$priority=10){ 37 | $params =array("to"=>$to,"body"=>$body,"priority"=>$priority); 38 | return $this->sendRequest("POST","messages/chat",$params ); 39 | } 40 | 41 | public function sendImageMessage($to,$caption="",$image,$priority=10){ 42 | $params =array("to"=>$to,"caption"=>$caption,"image"=>$image,"priority"=>$priority); 43 | return $this->sendRequest("POST","messages/image",$params ); 44 | } 45 | 46 | public function sendDocumentMessage($to,$filename,$document,$priority=10){ 47 | $params =array("to"=>$to,"filename"=>$filename,"document"=>$document,"priority"=>$priority); 48 | return $this->sendRequest("POST","messages/document",$params ); 49 | } 50 | 51 | public function sendAudioMessage($to,$audio,$priority=10){ 52 | $params =array("to"=>$to,"audio"=>$audio,"priority"=>$priority); 53 | return $this->sendRequest("POST","messages/audio",$params ); 54 | } 55 | 56 | public function sendVoiceMessage($to,$audio,$priority=10){ 57 | $params =array("to"=>$to,"audio"=>$audio,"priority"=>$priority); 58 | return $this->sendRequest("POST","messages/voice",$params ); 59 | } 60 | 61 | public function sendVideoMessage($to,$video,$priority=10){ 62 | $params =array("to"=>$to,"video"=>$video,"priority"=>$priority); 63 | return $this->sendRequest("POST","messages/video",$params ); 64 | } 65 | 66 | public function sendLinkMessage($to,$link,$priority=10){ 67 | $params =array("to"=>$to,"link"=>$link,"priority"=>$priority); 68 | return $this->sendRequest("POST","messages/link",$params ); 69 | } 70 | 71 | public function sendContactMessage($to,$contact,$priority=10){ 72 | $params =array("to"=>$to,"contact"=>$contact,"priority"=>$priority); 73 | return $this->sendRequest("POST","messages/contact",$params ); 74 | } 75 | public function sendLocationMessage($to,$address,$lat,$lng,$priority=10){ 76 | $params =array("to"=>$to,"address"=>$address,"lat"=>$lat,"lng"=>$lng,"priority"=>$priority); 77 | return $this->sendRequest("POST","messages/location",$params ); 78 | } 79 | public function sendVcardMessage($to,$vcard,$priority=10){ 80 | $params =array("to"=>$to,"vcard"=>$vcard,"priority"=>$priority); 81 | return $this->sendRequest("POST","messages/vcard",$params ); 82 | } 83 | public function sendClearMessage($status){ 84 | $params =array("status"=>$status); 85 | return $this->sendRequest("POST","messages/clear",$params ); 86 | } 87 | 88 | // instance 89 | 90 | public function getInstanceStatus(){ 91 | return $this->sendRequest("GET","instance/status"); 92 | } 93 | 94 | public function getInstanceQr(){ 95 | return $this->sendRequest("GET","instance/qr"); 96 | } 97 | 98 | public function getInstanceQrCode(){ 99 | return $this->sendRequest("GET","instance/qrCode"); 100 | } 101 | 102 | public function getInstanceScreenshot($encoding=""){ 103 | return $this->sendRequest("GET","instance/screenshot",array("encoding"=>$encoding)); 104 | } 105 | 106 | public function getInstanceMe(){ 107 | return $this->sendRequest("GET","instance/me"); 108 | } 109 | 110 | public function getInstanceSettings(){ 111 | return $this->sendRequest("GET","instance/settings"); 112 | } 113 | 114 | 115 | public function sendInstanceTakeover(){ 116 | return $this->sendRequest("POST","instance/takeover" ); 117 | } 118 | 119 | public function sendInstanceLogout(){ 120 | return $this->sendRequest("POST","instance/logout" ); 121 | } 122 | 123 | public function sendInstanceRestart(){ 124 | return $this->sendRequest("POST","instance/restart" ); 125 | } 126 | 127 | public function sendInstanceSettings($sendDelay,$webhook_url,$webhook_message_received,$webhook_message_create,$webhook_message_ack){ 128 | 129 | $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)); 130 | return $this->sendRequest("POST","instance/settings",$params); 131 | } 132 | 133 | public function sendInstanceClear(){ 134 | return $this->sendRequest("POST","instance/clear" ); 135 | } 136 | 137 | 138 | 139 | public function sendRequest($method,$path,$params=array()){ 140 | 141 | if(!is_callable('curl_init')){ 142 | return array("Error"=>"cURL extension is disabled on your server"); 143 | } 144 | $url="https://api.ultramsg.com/".$this->instance_id."/".$path; 145 | $params['token'] = $this->token; 146 | $data=http_build_query($params); 147 | if(strtolower($method)=="get")$url = $url . '?' . $data; 148 | $curl = curl_init($url); 149 | if(strtolower($method)=="post"){ 150 | curl_setopt($curl, CURLOPT_POST, true); 151 | curl_setopt($curl, CURLOPT_POSTFIELDS,$data); 152 | } 153 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 154 | curl_setopt($curl, CURLOPT_HEADER, 1); 155 | $response = curl_exec($curl); 156 | $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); 157 | if($httpCode == 404) { 158 | return array("Error"=>"instance not found or pending please check you instance id"); 159 | } 160 | $contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE); 161 | $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE); 162 | $header = substr($response, 0, $header_size); 163 | $body = substr($response, $header_size); 164 | curl_close($curl); 165 | 166 | if (strpos($contentType,'application/json') !== false) { 167 | return json_decode($body,true); 168 | } 169 | return $body; 170 | } 171 | 172 | 173 | 174 | 175 | } -------------------------------------------------------------------------------- /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 | 54 | ```php 55 | $to="put_your_mobile_number_here"; 56 | $body="Hello world"; 57 | $priority=10; 58 | $api=$client->sendChatMessage($to,$body,$priority); 59 | print_r($api); 60 | ``` 61 | 62 | ## Send Image 63 | * **$caption** : image Caption, UTF-8 or UTF-16 string with emoji . 64 | * **$image** : HTTP link image or base64-encoded file with mime data 65 | 66 | Supported extensions ( jpg , jpeg , gif , png , svg , webp , bmp ) . 67 | 68 | Max file size : 64MB . 69 | 70 | **example images links** : 71 | 72 | - jpg : https://file-example.s3-accelerate.amazonaws.com/images/test.jpg 73 | 74 | - jpeg : https://file-example.s3-accelerate.amazonaws.com/images/test.jpeg 75 | 76 | - png : https://file-example.s3-accelerate.amazonaws.com/images/test.png 77 | 78 | - svg : https://file-example.s3-accelerate.amazonaws.com/images/test.svg 79 | 80 | - gif : https://file-example.s3-accelerate.amazonaws.com/images/test.gif 81 | 82 | - bmp : https://file-example.s3-accelerate.amazonaws.com/images/test.bmp 83 | 84 | - webp : https://file-example.s3-accelerate.amazonaws.com/images/test.webp 85 | 86 | ```php 87 | $to="put_your_mobile_number_here"; 88 | $caption="image Caption"; 89 | $image="https://file-example.s3-accelerate.amazonaws.com/images/test.jpg"; 90 | $api=$client->sendImageMessage($to,$caption,$image); 91 | print_r($api); 92 | ``` 93 | ## Send Document 94 | * **$filename** : File name, for example 1.jpg or Hello.pdf 95 | * **$document** : HTTP link file or base64-encoded file with mime data 96 | 97 | Supported most extensions like ( zip , xlsx , csv , txt , pptx , docx ....etc ) . 98 | 99 | Max file size : 64MB . 100 | 101 | ```php 102 | $to="put_your_mobile_number_here"; 103 | $filename="image Caption"; 104 | $document="https://file-example.s3-accelerate.amazonaws.com/documents/cv.pdf"; 105 | $api=$client->sendDocumentMessage($to,$filename,$document); 106 | print_r($api); 107 | ``` 108 | 109 | ## Send Audio 110 | * **$audio** : HTTP link audio or base64-encoded audio with mime data 111 | 112 | Supported extensions ( mp3 , aac , ogg ) . 113 | 114 | Max file size : 64MB . 115 | 116 | ```php 117 | $to="put_your_mobile_number_here"; 118 | $audio="https://file-example.s3-accelerate.amazonaws.com/audio/2.mp3"; 119 | $api=$client->sendAudioMessage($to,$audio); 120 | print_r($api); 121 | ``` 122 | ## Send Voice 123 | * **$audio** : HTTP link audio ogg-file with opus codec or base64 ogg-file in opus codec 124 | 125 | ```php 126 | $to="put_your_mobile_number_here"; 127 | $audio="https://file-example.s3-accelerate.amazonaws.com/voice/oog_example.ogg"; 128 | $api=$client->sendVoiceMessage($to,$audio); 129 | print_r($api); 130 | ``` 131 | 132 | ## Send Video 133 | * **$video** : HTTP link video or base64-encoded video with mime data 134 | 135 | Supported extensions ( mp4 , 3gp , mov ) . 136 | 137 | Max file size : 64MB . 138 | ```php 139 | $to="put_your_mobile_number_here"; 140 | $video="https://file-example.s3-accelerate.amazonaws.com/video/test.mp4"; 141 | $api=$client->sendVideoMessage($to,$video); 142 | print_r($api); 143 | ``` 144 | ## Send Link 145 | * **$link** : HTTP or HTTPS link 146 | 147 | ```php 148 | $to="put_your_mobile_number_here"; 149 | $link="https://ultramsg.com"; 150 | $api=$client->sendLinkMessage($to,$link); 151 | print_r($api); 152 | ``` 153 | ## Send Contact 154 | * **$contact** :Contact ID or Contact IDs array example : 155 | 156 | Example 157 | 158 | 14000000001@c.us 159 | 160 | or 161 | 162 | 14000000001@c.us,14000000002@c.us,14000000003@c.us 163 | 164 | Max length : 300 char, almost 15 contacts 165 | ```php 166 | $to="put_your_mobile_number_here"; 167 | $contact="14000000001@c.us"; 168 | $api=$client->sendContactMessage($to,$contact); 169 | print_r($api); 170 | ``` 171 | ## Send Location 172 | * **$address** : Text under the location. 173 | 174 | Supports two lines. To use two lines, use the \n symbol. 175 | 176 | Max length : 300 char . 177 | * **$lat** : Latitude 178 | * **$lng** : longitude 179 | ```php 180 | $to="put_your_mobile_number_here"; 181 | $address="ABC company \n Sixth floor , office 38"; 182 | $lat="25.197197"; 183 | $lng="55.2721877"; 184 | $api=$client->sendLocationMessage($to,$address,$lat,$lng); 185 | print_r($api); 186 | ``` 187 | ## Send Vcard 188 | * **$vcard** : Text value vcard 3.0 189 | 190 | Max length : 4096 char 191 | 192 | ```php 193 | $to="put_your_mobile_number_here"; 194 | $vcard="BEGIN:VCARD 195 | VERSION:3.0 196 | N:lastname;firstname 197 | FN:firstname lastname 198 | TEL;TYPE=CELL;waid=14000000001:14000000002 199 | NICKNAME:nickname 200 | BDAY:01.01.1987 201 | X-GENDER:M 202 | NOTE:note 203 | ADR;TYPE=home 204 | ADR;TYPE=work 205 | END:VCARD"; 206 | $vcard = preg_replace("/[\n\r]/", "\n", $vcard); 207 | $api=$client->sendVcardMessage($to,$vcard); 208 | print_r($api); 209 | ``` 210 | 211 | ## Get Messages 212 | get the messages that sent by api 213 | 214 | * **$page** : pagination page number 215 | * **$limit** : number of messages per request . max value : 100 . 216 | * **$status** : Messages status [sent , queue , unsent] 217 | - sent : get sent messages . 218 | - queue : get queue messages . 219 | - unsent : get unsent messages . 220 | - all : get all messages . 221 | * **$sort** : 222 | - asc : sorted messages by ID from smallest to largest . 223 | - desc : sorted messages by ID from largest to smallest . 224 | 225 | ```php 226 | $page=1; 227 | $limit=100; 228 | $status="all"; 229 | $sort="asc"; 230 | $api=$client->getMessages($page,$limit,$status,$sort); 231 | print_r($api); 232 | ``` 233 | 234 | ## Get Messages Statistics 235 | 236 | ```php 237 | $api=$client->getMessageStatistics(); 238 | print_r($api); 239 | ``` 240 | 241 | ## Get Instance Status 242 | 243 | ```php 244 | $api=$client->getInstanceStatus(); 245 | print_r($api); 246 | ``` 247 | 248 | ## Get Instance QR Image 249 | 250 | ```php 251 | header('Content-Type: image/png'); 252 | $api=$client->getInstanceQr(); 253 | print_r($api); 254 | ``` 255 | ## Get Instance QR Code 256 | 257 | ```php 258 | $api=$client->getInstanceQrCode(); 259 | print_r($api); 260 | ``` 261 | ## Get Instance Screenshot 262 | 263 | ```php 264 | header('Content-Type: image/png'); 265 | $api=$client->getInstanceScreenshot(); 266 | print_r($api); 267 | ``` 268 | or base64 269 | ```php 270 | $api=$client->getInstanceScreenshot("base64"); 271 | print_r($api); 272 | ``` 273 | ## Get Instance Info 274 | Get connected phone informations : number , name , image etc.. 275 | ```php 276 | $api=$client->getInstanceMe(); 277 | print_r($api); 278 | ``` 279 | ## Get Instance Settings 280 | sendDelay : Delay in seconds between sending message, Default 1 second 281 | 282 | webhook_url : Http or https URL for receiving notifications . 283 | 284 | webhook_message_ack : on/off ack (message delivered and message viewed) notifications in webhooks. 285 | 286 | webhook_message_received : on/off notifications in webhooks when message received . 287 | 288 | webhook_message_create : on/off notifications in webhooks when message create . 289 | ```php 290 | $api=$client->getInstanceSettings(); 291 | print_r($api); 292 | ``` 293 | 294 | ## Instance Takeover 295 | Returns the active session if the device has connected to another instance of Web WhatsApp 296 | 297 | ```php 298 | $api=$client->sendInstanceTakeover(); 299 | print_r($api); 300 | ``` 301 | ## Instance Logout 302 | Logout from WhatsApp Web to get new QR code. 303 | 304 | ```php 305 | $api=$client->sendInstanceLogout(); 306 | print_r($api); 307 | ``` 308 | ## Instance Restart 309 | Restart your instance. 310 | 311 | ```php 312 | $api=$client->sendInstanceRestart(); 313 | print_r($api); 314 | ``` 315 | ## Instance Settings Update 316 | * **sendDelay** : Delay in seconds between sending message . 317 | 318 | * **webhook_url** : Http or https URL for receiving notifications . 319 | 320 | * **webhook_message_received** : true/false notifications in webhooks when message received . 321 | 322 | * **webhook_message_create** : true/false notifications in webhooks when message create . 323 | 324 | * **webhook_message_ack** : true/false ack (message delivered and message viewed) notifications in webhooks. 325 | 326 | ```php 327 | $sendDelay=1; 328 | $webhook_url=""; 329 | $webhook_message_received=false; 330 | $webhook_message_create=false; 331 | $webhook_message_ack=false; 332 | 333 | $api=$client->sendInstanceSettings($sendDelay,$webhook_url,$webhook_message_received,$webhook_message_create,$webhook_message_ack); 334 | print_r($api); 335 | ``` 336 | 337 | # Support 338 | Use **Issues** to contact me --------------------------------------------------------------------------------