wppconnect-team/wppconnect-laravel-client

一个带有 Guzzle 封装的简单 API,提供对 wppconnect 端点的便捷访问。

1.0.1 2021-04-19 15:15 UTC

This package is not auto-updated.

Last update: 2024-10-01 16:05:39 UTC


README

Wppconnect Laravel 客户端

一个简单的 API,带有 Guzzle 封装器,提供对 WPPConnect 服务器端点的便捷访问。

我们的在线频道

Discord Telegram Group WhatsApp Group YouTube

要求

  • PHP 7.4 或更高版本。
  • Laravel 8.x 或更高版本。

安装 - Laravel

使用以下命令使用 Composer(Packagist)下载包

$ composer require wppconnect-team/wppconnect-laravel-client

config/app.php 的 providers 中注册 WppconnectServiceProvider

 WPPConnectTeam\Wppconnect\WppconnectServiceProvider::class

发布 vendor 文件(配置文件)

$ php artisan vendor:publish

可选config/app.php 中注册 facade

'Wppconnect' => WPPConnectTeam\Wppconnect\Facades\Wppconnect::class

配置

应用于由 API 创建的所有请求的配置。

示例

'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'
 ]

使用

您可以使用此包,无需配置,在控制器中使用 Wppconnect facade,或者将其注入到需要客户端的类中

/**
 * @var RequestInterface
 */
protected $client;

/**
 * @param Wppconnect $client
 */
public function __construct(Wppconnect $client)
{
    $this->client = $client;
}

使用 Facade 的示例

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;
	
    }
 }
   #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;
   #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;
   #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;
   #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;

调试

在发送请求之前使用 debug(bool|resource) 来激活 Guzzle 调试器。更多信息请参阅 文档

每次请求后调试器都会关闭,如果您需要调试连续发送的多个请求,则需要为所有请求启用调试。

示例

$logFile = './client_debug_test.log';
$logFileResource = fopen($logFile, 'w+');

$this->client->debug($logFileResource)->to('post')->withBody([
	'foo' => 'bar'
])->asJson()->post();

fclose($logFileResource);

日志将保存在 client_debug_test.log 文件中。

Postman

访问包含所有端点的 WPPConnect Postman 集合