Last active
January 12, 2022 02:14
-
-
Save jhowbhz/bc9116ef7c232f3e71e915f15a3436d3 to your computer and use it in GitHub Desktop.
Class PHP IntegracaoWhatsApp para MyZap 2.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/****************************************************************************************************************/ | |
/* Antes de utilizar esses codigos, inicie a sessão em https://https://whatsapp-v2.apibrasil.com.br/start */ | |
/****************************************************************************************************************/ | |
class IntegracaoWhatsApp { | |
public static function start() { | |
try { | |
$data = [ | |
"server_host" => "https://whatsapp2.contrateumdev.com.br", //required | |
"method" => "POST", //optional | |
"apitoken" => "YOUR_TOKEN_API", //required | |
"session_name" => "YOUR_SESSION_NAME", //required | |
"session_key" => "YOUR_SESSION_KEY", //required | |
"wh_status" => "", //optional | |
"wh_message" => "", //optional | |
"wh_connect" => "", //optional | |
"wh_qrcode" => "", //optional | |
]; | |
self::requestIntegracao("start", $data); | |
} catch (\Throwable $th) { | |
var_dump($th->getMessage()); | |
exit(); | |
} | |
} | |
public static function sendText() { | |
try { | |
$data = [ | |
"server_host" => "https://whatsapp2.contrateumdev.com.br", //required | |
"method" => "POST", //optional | |
"session" => "YOUR_SESSION_NAME", //required | |
"session_key" => "YOUR_SESSION_KEY", //required | |
"number" => "+55995360492", //required | |
"text" => "IS MY FIRST TEXT SEND FROM APIBRASIL.COM.BR" //required | |
]; | |
self::requestIntegracao("sendText", $data); | |
} catch (\Throwable $th) { | |
var_dump($th->getMessage()); | |
exit(); | |
} | |
} | |
public static function requestIntegracao(string $action = '', array $data = []) { | |
$curl = curl_init(); | |
//validate inforequest | |
$method = $data['method'] ?? 'POST'; | |
$server_host = $data['server_host'] ?? ''; | |
$sessionkey = $data['sessionkey'] ?? ''; | |
$server_host = $server_host."/".$action; | |
//clear data after send body | |
$clear = ['server_host', 'apitoken']; | |
$body = array_diff_key($data, array_flip($clear)); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => $server_host, | |
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 => json_encode($body), | |
CURLOPT_HTTPHEADER => [ | |
'sessionkey' => $sessionkey, | |
'Content-Type' => 'application/json' | |
], | |
)); | |
$callback = curl_exec($curl); | |
curl_close($curl); | |
return json_decode($callback); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment