Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ultramsg authored Aug 24, 2022
1 parent 4c18e0b commit a4287d1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 130 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ true : uploading it each request

```php
$to="put_your_mobile_number_here";
$caption="image Caption";
$image="https://file-example.s3-accelerate.amazonaws.com/images/test.jpg";
$caption="image Caption";
$priority=10;
$referenceId="SDK"
$nocache=false;
$api=$client->sendImageMessage($to,$caption,$image,$priority,$referenceId,$nocache);
$api=$client->sendImageMessage($to,$image,$caption,$priority,$referenceId,$nocache);
print_r($api);
```
## Send Document
Expand Down Expand Up @@ -144,9 +144,9 @@ Max Base64 length : 2,000,000

```php
$to="put_your_mobile_number_here";
$video="https://file-example.s3-accelerate.amazonaws.com/video/test.mp4";
$caption="video Caption";
$video="https://file-example.s3-accelerate.amazonaws.com/video/test.mp4";
$api=$client->sendVideoMessage($to,$caption,$video);
$api=$client->sendVideoMessage($to,$video,$caption);
print_r($api);
```
## Send Link
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"url": "https://github.com/ultramsg/whatsapp-php-sdk"
}
],
"version": "2.0.6",
"version": "2.0.7",
"license": "MIT",
"authors": [
{
Expand Down
248 changes: 123 additions & 125 deletions ultramsg.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ class WhatsAppApi {
* @param $instance_id
*/
public function __construct($token, $instance_id){
$this->token = $token;
$this->instance_id = $instance_id;
if(ctype_digit($instance_id)){
$this->instance_id = "instance".$instance_id;
}
$this->token = $token;
$this->instance_id = "instance".preg_replace('/[^0-9]/', '',$instance_id);

}

// messages
Expand All @@ -38,7 +36,7 @@ public function sendChatMessage($to,$body,$priority=10,$referenceId=""){
return $this->sendRequest("POST","messages/chat",$params );
}

public function sendImageMessage($to,$caption="",$image,$priority=10,$referenceId="",$nocache=false){
public function sendImageMessage($to,$image,$caption="",$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"caption"=>$caption,"image"=>$image,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/image",$params );
}
Expand All @@ -58,7 +56,7 @@ public function sendVoiceMessage($to,$audio,$priority=10,$referenceId="",$nocach
return $this->sendRequest("POST","messages/voice",$params );
}

public function sendVideoMessage($to,$caption="",$video,$priority=10,$referenceId="",$nocache=false){
public function sendVideoMessage($to,$video,$caption="",$priority=10,$referenceId="",$nocache=false){
$params =array("to"=>$to,"caption"=>$caption,"video"=>$video,"priority"=>$priority,"referenceId"=>$referenceId,"nocache"=>$nocache);
return $this->sendRequest("POST","messages/video",$params );
}
Expand All @@ -73,151 +71,151 @@ public function sendContactMessage($to,$contact,$priority=10,$referenceId=""){
return $this->sendRequest("POST","messages/contact",$params );
}
public function sendLocationMessage($to,$address,$lat,$lng,$priority=10,$referenceId=""){
$params =array("to"=>$to,"address"=>$address,"lat"=>$lat,"lng"=>$lng,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/location",$params );
$params =array("to"=>$to,"address"=>$address,"lat"=>$lat,"lng"=>$lng,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/location",$params );
}
public function sendVcardMessage($to,$vcard,$priority=10,$referenceId=""){
$params =array("to"=>$to,"vcard"=>$vcard,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/vcard",$params );
$params =array("to"=>$to,"vcard"=>$vcard,"priority"=>$priority,"referenceId"=>$referenceId);
return $this->sendRequest("POST","messages/vcard",$params );
}
public function sendClearMessage($status){
$params =array("status"=>$status);
return $this->sendRequest("POST","messages/clear",$params );
$params =array("status"=>$status);
return $this->sendRequest("POST","messages/clear",$params );
}
public function resendByStatus($status){
$params =array("status"=>$status);
return $this->sendRequest("POST","messages/resendByStatus",$params );
$params =array("status"=>$status);
return $this->sendRequest("POST","messages/resendByStatus",$params );
}

public function resendById($id){
$params =array("id"=>$id);
return $this->sendRequest("POST","messages/resendById",$params );
$params =array("id"=>$id);
return $this->sendRequest("POST","messages/resendById",$params );
}

// instance

public function getInstanceStatus(){
return $this->sendRequest("GET","instance/status");
return $this->sendRequest("GET","instance/status");
}

public function getInstanceQr(){
return $this->sendRequest("GET","instance/qr");
return $this->sendRequest("GET","instance/qr");
}

public function getInstanceQrCode(){
return $this->sendRequest("GET","instance/qrCode");
return $this->sendRequest("GET","instance/qrCode");
}

public function getInstanceScreenshot($encoding=""){
return $this->sendRequest("GET","instance/screenshot",array("encoding"=>$encoding));
return $this->sendRequest("GET","instance/screenshot",array("encoding"=>$encoding));
}

public function getInstanceMe(){
return $this->sendRequest("GET","instance/me");
return $this->sendRequest("GET","instance/me");
}

public function getInstanceSettings(){
return $this->sendRequest("GET","instance/settings");
return $this->sendRequest("GET","instance/settings");
}

public function sendInstanceTakeover(){
return $this->sendRequest("POST","instance/takeover" );
}

public function sendInstanceLogout(){
return $this->sendRequest("POST","instance/logout" );
}

public function sendInstanceRestart(){
return $this->sendRequest("POST","instance/restart" );
}

public function sendInstanceSettings($sendDelay="1",$webhook_url="",$webhook_message_received=false,$webhook_message_create=false,$webhook_message_ack=false,$webhook_message_download_media=false){
$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));
return $this->sendRequest("POST","instance/settings",$params);
}

public function sendInstanceClear(){
return $this->sendRequest("POST","instance/clear" );
}

// Chats

public function getChats(){
return $this->sendRequest("GET","chats");
}

public function getChatsMessages($chatId,$limit=100){
$params =array("chatId"=>$chatId,"limit"=>$limit);
return $this->sendRequest("GET","chats/messages",$params);
}

// Contacts

public function getContacts(){
return $this->sendRequest("GET","contacts");
}

public function getContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("GET","contacts/contact",$params);
}

public function getBlockedContacts(){
return $this->sendRequest("GET","contacts/blocked");
}

public function checkContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("GET","contacts/check",$params);
}

public function blockContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("POST","contacts/block",$params);
}

public function unblockContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("POST","contacts/unblock",$params);
}

public function sendRequest($method,$path,$params=array()){

if(!is_callable('curl_init')){
return array("Error"=>"cURL extension is disabled on your server");
}
$url="https://api.ultramsg.com/".$this->instance_id."/".$path;
$params['token'] = $this->token;
$data=http_build_query($params);
if(strtolower($method)=="get")$url = $url . '?' . $data;
$curl = curl_init($url);
if(strtolower($method)=="post"){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
return array("Error"=>"instance not found or pending please check you instance id");
}
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);

if (strpos($contentType,'application/json') !== false) {
return json_decode($body,true);
}
return $body;
}




}
public function sendInstanceTakeover(){
return $this->sendRequest("POST","instance/takeover" );
}
public function sendInstanceLogout(){
return $this->sendRequest("POST","instance/logout" );
}
public function sendInstanceRestart(){
return $this->sendRequest("POST","instance/restart" );
}
public function sendInstanceSettings($sendDelay="1",$webhook_url="",$webhook_message_received=false,$webhook_message_create=false,$webhook_message_ack=false,$webhook_message_download_media=false){
$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));
return $this->sendRequest("POST","instance/settings",$params);
}
public function sendInstanceClear(){
return $this->sendRequest("POST","instance/clear" );
}
// Chats
public function getChats(){
return $this->sendRequest("GET","chats");
}
public function getChatsMessages($chatId,$limit=100){
$params =array("chatId"=>$chatId,"limit"=>$limit);
return $this->sendRequest("GET","chats/messages",$params);
}
// Contacts
public function getContacts(){
return $this->sendRequest("GET","contacts");
}
public function getContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("GET","contacts/contact",$params);
}
public function getBlockedContacts(){
return $this->sendRequest("GET","contacts/blocked");
}
public function checkContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("GET","contacts/check",$params);
}
public function blockContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("POST","contacts/block",$params);
}
public function unblockContact($chatId){
$params =array("chatId"=>$chatId);
return $this->sendRequest("POST","contacts/unblock",$params);
}
public function sendRequest($method,$path,$params=array()){
if(!is_callable('curl_init')){
return array("Error"=>"cURL extension is disabled on your server");
}
$url="https://api.ultramsg.com/".$this->instance_id."/".$path;
$params['token'] = $this->token;
$data=http_build_query($params);
if(strtolower($method)=="get")$url = $url . '?' . $data;
$curl = curl_init($url);
if(strtolower($method)=="post"){
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
return array("Error"=>"instance not found or pending please check you instance id");
}
$contentType = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
if (strpos($contentType,'application/json') !== false) {
return json_decode($body,true);
}
return $body;
}
}

0 comments on commit a4287d1

Please sign in to comment.