|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Andreshg112\HablameSms; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
6
|
|
|
|
|
7
|
|
|
class Client |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var string $api Clave API suministrada por Háblame SMS. */ |
|
10
|
|
|
protected $api = null; |
|
11
|
|
|
|
|
12
|
|
|
/** @var string $client Número del cliente en Háblame SMS. */ |
|
13
|
|
|
protected $client = null; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \GuzzleHttp\Client $http Cliente de Guzzle. */ |
|
16
|
|
|
protected $http = null; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Crea una instancia recibiendo el número del cliente y la clave. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $client |
|
22
|
|
|
* @param string $api |
|
23
|
|
|
* @param \GuzzleHttp\Client $http |
|
24
|
|
|
*/ |
|
25
|
6 |
|
public function __construct(string $client, string $api, GuzzleClient $http = null) |
|
26
|
|
|
{ |
|
27
|
6 |
|
$this->client = $client; |
|
28
|
|
|
|
|
29
|
6 |
|
$this->api = $api; |
|
30
|
|
|
|
|
31
|
6 |
|
$this->http = $http ?? new GuzzleClient(); |
|
32
|
6 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Consulta el saldo. |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function checkBalance(): array |
|
40
|
|
|
{ |
|
41
|
1 |
|
$url = 'https://api.hablame.co/saldo/consulta/index.php'; |
|
42
|
|
|
|
|
43
|
1 |
|
$params = ['cliente' => $this->client, 'api' => $this->api]; |
|
44
|
|
|
|
|
45
|
1 |
|
$response = $this->http->get($url, ['query' => $params]); |
|
46
|
|
|
|
|
47
|
1 |
|
return json_decode((string)$response->getBody(), true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Envía un mensaje de texto (SMS) al destinatario o destinatarios indicados. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $phoneNumbers Número(s) telefonico(s) a enviar SMS (separados por coma). |
|
54
|
|
|
* @param string $sms Mensaje de texto a enviar. |
|
55
|
|
|
* @param string|null $datetime [optional] Fecha de envío. Si está vacío, se envía inmediatamente. |
|
56
|
|
|
* @param string|null $reference [optional] Número de referencia o nombre de campaña. |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function sendMessage( |
|
60
|
|
|
string $phoneNumbers, |
|
61
|
|
|
string $sms, |
|
62
|
|
|
string $datetime = null, |
|
63
|
|
|
string $reference = null |
|
64
|
|
|
): array { |
|
65
|
1 |
|
$url = 'https://api.hablame.co/sms/envio'; |
|
66
|
|
|
|
|
67
|
|
|
$params = [ |
|
68
|
1 |
|
'cliente' => $this->client, |
|
69
|
1 |
|
'api' => $this->api, |
|
70
|
1 |
|
'numero' => $phoneNumbers, |
|
71
|
1 |
|
'sms' => $sms, |
|
72
|
1 |
|
'fecha' => $datetime, |
|
73
|
1 |
|
'referencia' => $reference, |
|
74
|
|
|
]; |
|
75
|
|
|
|
|
76
|
1 |
|
$response = $this->http->post($url, ['query' => array_filter($params)]); |
|
77
|
|
|
|
|
78
|
1 |
|
return json_decode((string)$response->getBody(), true); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|