PHP code example of wppconnect-team / wppconnect-laravel-client
1. Go to this page and download the library: Download wppconnect-team/wppconnect-laravel-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wppconnect-team / wppconnect-laravel-client example snippets
/**
* @var RequestInterface
*/
protected $client;
/**
* @param Wppconnect $client
*/
public function __construct(Wppconnect $client)
{
$this->client = $client;
}
$logFile = './client_debug_test.log';
$logFileResource = fopen($logFile, 'w+');
$this->client->debug($logFileResource)->to('post')->withBody([
'foo' => 'bar'
])->asJson()->post();
fclose($logFileResource);
php
WPPConnectTeam\Wppconnect\WppconnectServiceProvider::class
bash
$ php artisan vendor:publish
php
'Wppconnect' => WPPConnectTeam\Wppconnect\Facades\Wppconnect::class
php
'defaults' => [
/**
* URL do WPPConnect Server
*/
'base_uri' => 'http://192.168.0.39:21465',
/**
* Secret Key
* Veja: https://github.com/wppconnect-team/wppconnect-server#secret-key
*/
'secret_key' => 'MYKeYPHP'
]
php
class WppconnectController extends Controller
{
protected $url;
protected $key;
protected $session;
/**
* __construct function
*/
public function __construct()
{
$this->url = config('wppconnect.defaults.base_uri');
$this->key = config('wppconnect.defaults.secret_key');
$this->session = "mySession";
}
public function index(){
#Function: Generated Token
# /api/:session/generate-token
//Session::flush();
if(!Session::get('token') and !Session::get('session')):
Wppconnect::make($this->url);
$response = Wppconnect::to('/api/'.$this->session.'/'.$this->key.'/generate-token')->asJson()->post();
$response = json_decode($response->getBody()->getContents(),true);
if($response['status'] == 'success'):
Session::put('token', $response['token']);
Session::put('session', $response['session']);
endif;
endif;
#Function: Start Session
# /api/:session/start-session
if(Session::get('token') and Session::get('session') and !Session::get('init')):
Wppconnect::make($this->url);
$response = Wppconnect::to('/api/'.Session::get('session').'/start-session')->withHeaders([
'Authorization' => 'Bearer '.Session::get('token')
])->asJson()->post();
$response = json_decode($response->getBody()->getContents(),true);
Session::put('init', true);
endif;
}
}
php
#Function: Check Connection Session
# /api/:session/check-connection-session
if(Session::get('token') and Session::get('session') and Session::get('init')):
Wppconnect::make($this->url);
$response = Wppconnect::to('/api/'. Session::get('session').'/check-connection-session')->withHeaders([
'Authorization' => 'Bearer '.Session::get('token')
])->asJson()->get();
$response = json_decode($response->getBody()->getContents(),true);
dd($response);
endif;
php
#Function: Close Session
# /api/:session/close-session
if(Session::get('token') and Session::get('session') and Session::get('init')):
Wppconnect::make($this->url);
$response = Wppconnect::to('/api/'. Session::get('session').'/close-session')->withHeaders([
'Authorization' => 'Bearer '.Session::get('token')
])->asJson()->post();
$response = json_decode($response->getBody()->getContents(),true);
dd($response);
endif;
php
#Function: Send Message
# /api/:session/send-message
if(Session::get('token') and Session::get('session') and Session::get('init')):
Wppconnect::make($this->url);
$response = Wppconnect::to('/api/'. Session::get('session').'/send-message')->withBody([
'phone' => '5500000000000',
'message' => 'Opa, funciona mesmo!'
])->withHeaders([
'Authorization' => 'Bearer '.Session::get('token')
])->asJson()->post();
$response = json_decode($response->getBody()->getContents(),true);
dd($response);
endif;
php
#Function: Send File Base64
# /api/:session/send-file-base64
if(Session::get('token') and Session::get('session') and Session::get('init')):
Wppconnect::make($this->url);
$response = Wppconnect::to('/api/'. Session::get('session').'/send-file-base64')->withBody([
'phone' => '5500000000000',
'base64' => 'data:image/jpg;base64,' . base64_encode(file_get_contents(resource_path('/img/xpto.jpg')))
])->withHeaders([
'Authorization' => 'Bearer '.Session::get('token')
])->asJson()->post();
$response = json_decode($response->getBody()->getContents(),true);
dd($response);
endif;