@@ -18,23 +18,31 @@ To use the client library you'll need to have [created a Nexmo account][signup].
18
18
19
19
To install the PHP client library using Composer:
20
20
21
- composer require nexmo/client
21
+ ``` bash
22
+ composer require nexmo/client
23
+ ```
22
24
23
25
Alternatively you can clone the repository, however, you'll need to ensure the library is autoloaded by a PSR-0 or PSR-4
24
26
compatible autoloader.
25
27
26
- git clone [email protected] :Nexmo/nexmo-php.git
28
+ ``` bash
29
+ git clone
[email protected] :Nexmo/nexmo-php.git
30
+ ```
27
31
28
32
Usage
29
33
-----
30
34
31
35
If you're using composer, make sure the autoloader is included in your project's bootstrap file:
32
36
33
- require_once "vendor/autoload.php";
37
+ ``` php
38
+ require_once "vendor/autoload.php";
39
+ ```
34
40
35
41
Create a client with your API key and secret:
36
42
37
- $client = new Nexmo\Client(new Nexmo\Credentials\Basic(API_KEY, API_SECRET));
43
+ ``` php
44
+ $client = new Nexmo\Client(new Nexmo\Credentials\Basic(API_KEY, API_SECRET));
45
+ ```
38
46
39
47
Examples
40
48
--------
@@ -45,43 +53,57 @@ To use [Nexmo's SMS API][doc_sms] to send an SMS message, call the `$client->mes
45
53
46
54
The API can be called directly, using a simple array of parameters, the keys match the [ parameters of the API] [ doc_sms ] .
47
55
48
- $message = $client->message()->send([
49
- 'to' => NEXMO_TO,
50
- 'from' => NEXMO_FROM,
51
- 'text' => 'Test message from the Nexmo PHP Client'
52
- ]);
56
+ ``` php
57
+ $message = $client->message()->send([
58
+ 'to' => NEXMO_TO,
59
+ 'from' => NEXMO_FROM,
60
+ 'text' => 'Test message from the Nexmo PHP Client'
61
+ ]);
62
+ ```
53
63
54
64
The API response data can be accessed as array properties of the message.
55
65
56
- echo "Sent message to " . $message['to'] . ". Balance is now " . $message['remaining-balance'] . PHP_EOL;
66
+ ``` php
67
+ echo "Sent message to " . $message['to'] . ". Balance is now " . $message['remaining-balance'] . PHP_EOL;
68
+ ```
57
69
58
70
** A message object** is a more expressive way to create and send messages. Each message type can be constructed with the
59
71
required parameters, and a fluent interface provides access to optional parameters.
60
72
61
- $text = new \Nexmo\Message\Text(NEXMO_TO, NEXMO_FROM, 'Test message using PHP client library');
62
- $text->setClientRef('test-message')
63
- ->setClass(\Nexmo\Message\Text::CLASS_FLASH);
73
+ ``` php
74
+ $text = new \Nexmo\Message\Text(NEXMO_TO, NEXMO_FROM, 'Test message using PHP client library');
75
+ $text->setClientRef('test-message')
76
+ ->setClass(\Nexmo\Message\Text::CLASS_FLASH);
77
+ ```
64
78
65
79
The message object is passed to the same ` send ` method:
66
80
67
- $client->message()->send($text);
81
+ ``` php
82
+ $client->message()->send($text);
83
+ ```
68
84
69
85
Once sent, the message object can be used to access the response data.
70
86
71
- echo "Sent message to " . $text->getTo() . ". Balance is now " . $text->getRemainingBalance() . PHP_EOL;
87
+ ``` php
88
+ echo "Sent message to " . $text->getTo() . ". Balance is now " . $text->getRemainingBalance() . PHP_EOL;
89
+ ```
72
90
73
91
Array access can still be used:
74
92
75
- echo "Sent message to " . $text['to'] . ". Balance is now " . $text['remaining-balance'] . PHP_EOL;
93
+ ``` php
94
+ echo "Sent message to " . $text['to'] . ". Balance is now " . $text['remaining-balance'] . PHP_EOL;
95
+ ```
76
96
77
97
If the message text had to be sent as multiple messages, by default, the data of the last message is returned. However,
78
98
specific message data can be accessed using array notation, passing an index to a getter, or iterating over the object.
79
99
80
- $text[0]['remaining-balance']
81
- $text->getRemainingBalance(0);
82
- foreach($text as $index => $data){
83
- $data['remaining-balance'];
84
- }
100
+ ``` php
101
+ $text[0]['remaining-balance']
102
+ $text->getRemainingBalance(0);
103
+ foreach($text as $index => $data){
104
+ $data['remaining-balance'];
105
+ }
106
+ ```
85
107
86
108
The [ send example] [ send_example ] also has full working examples.
87
109
@@ -90,32 +112,40 @@ The [send example][send_example] also has full working examples.
90
112
Inbound messages are [ sent to your application as a webhook] [ doc_inbound ] , and the client library provides a way to
91
113
create an inbound message object from a webhook:
92
114
93
- $inbound = \Nexmo\Message\InboundMessage::createFromGlobals();
94
- if($inbound->isValid()){
95
- error_log($inbound->getBody());
96
- } else {
97
- error_log('invalid message');
98
- }
115
+ ``` php
116
+ $inbound = \Nexmo\Message\InboundMessage::createFromGlobals();
117
+ if($inbound->isValid()){
118
+ error_log($inbound->getBody());
119
+ } else {
120
+ error_log('invalid message');
121
+ }
122
+ ```
99
123
100
124
You can also access the webhook data as an arry:
101
125
102
- $inbound = \Nexmo\Message\InboundMessage::createFromGlobals();
103
- error_log($inbound['to']);
126
+ ``` php
127
+ $inbound = \Nexmo\Message\InboundMessage::createFromGlobals();
128
+ error_log($inbound['to']);
129
+ ```
104
130
105
131
### Fetching A Message
106
132
107
133
You can retrieve a message log from the API using the ID of the message:
108
134
109
- $message = $client->message()->search('02000000DA7C52E7');
110
- echo "The body of the message was: " . $message->getBody();
135
+ ``` php
136
+ $message = $client->message()->search('02000000DA7C52E7');
137
+ echo "The body of the message was: " . $message->getBody();
138
+ ```
111
139
112
140
If the message was sent to a Nexmo virtual number, the object will be an instance of ` Nexmo\Message\InboundMessage ` , if
113
141
the message was sent from your account, it will be an instance of ` Nexmo\Message\Message ` . You can also pass a message
114
142
object to the client:
115
143
116
- $message = new \Nexmo\Message\InboundMessage('02000000DA7C52E7');
117
- $client->message()->search($message);
118
- echo "The body of the message was: " . $message->getBody();
144
+ ``` php
145
+ $message = new \Nexmo\Message\InboundMessage('02000000DA7C52E7');
146
+ $client->message()->search($message);
147
+ echo "The body of the message was: " . $message->getBody();
148
+ ```
119
149
120
150
### Starting a Verification
121
151
@@ -124,57 +154,71 @@ or implement second factor authentication during signin.
124
154
125
155
You can start a verification process using a simple array:
126
156
127
- $verification = $client->verify()->start([
128
- 'number' => '14845551212',
129
- 'brand' => 'My App'
130
- ]);
131
- echo "Started verification with an id of: " . $verification->getRequestId();
157
+ ``` php
158
+ $verification = $client->verify()->start([
159
+ 'number' => '14845551212',
160
+ 'brand' => 'My App'
161
+ ]);
162
+ echo "Started verification with an id of: " . $verification->getRequestId();
163
+ ```
132
164
133
165
Or you can pass the client a verification object:
134
166
135
- $verification = new \Nexmo\Verify\Verification('14845551212', 'My App');
136
- $client->verify()->start($verification);
137
- echo "Started verification with an id of: " . $verification->getRequestId();
167
+ ``` php
168
+ $verification = new \Nexmo\Verify\Verification('14845551212', 'My App');
169
+ $client->verify()->start($verification);
170
+ echo "Started verification with an id of: " . $verification->getRequestId();
171
+ ```
138
172
139
173
### Controlling a Verification
140
174
141
175
To cancel an in-progress verification, or to trigger the next attempt to send the confirmation code, you can pass
142
176
either an exsisting verification object to the client library, or simply use a request ID:
143
177
144
- $client->verify()->trigger('00e6c3377e5348cdaf567e1417c707a5');
145
-
146
- $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
147
- $client->verify()->cancel($verification);
178
+ ``` php
179
+ $client->verify()->trigger('00e6c3377e5348cdaf567e1417c707a5');
180
+
181
+ $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
182
+ $client->verify()->cancel($verification);
183
+ ```
148
184
149
185
### Checking A Verification
150
186
151
187
In the same way, checking a verification requires the code the user provided, and an exiting verification object:
152
188
153
- $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
154
- $client->verify()->check($verification, '1234');
189
+ ``` php
190
+ $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
191
+ $client->verify()->check($verification, '1234');
192
+ ```
155
193
156
194
Or a request ID:
157
195
158
- $client->verify()->check('00e6c3377e5348cdaf567e1417c707a5', '1234');
196
+ ``` php
197
+ $client->verify()->check('00e6c3377e5348cdaf567e1417c707a5', '1234');
198
+ ```
159
199
160
200
### Searching For A Verification
161
201
162
202
You can check the status of a verification, or access the results of past verifications using either an exsisting
163
203
verification object, or a request ID. The verification object will then provide a rich interface:
164
204
165
- $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
166
- $client->verify()->search($verification);
167
-
168
- echo "Codes checked for verification: " . $verification->getRequestId() . PHP_EOL;
169
- foreach($verification->getChecks() as $check){
170
- echo $check->getDate()->format('d-m-y') . ' ' . $check->getStatus() . PHP_EOL;
171
- }
205
+ ``` php
206
+ $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
207
+ $client->verify()->search($verification);
208
+
209
+ echo "Codes checked for verification: " . $verification->getRequestId() . PHP_EOL;
210
+ foreach($verification->getChecks() as $check){
211
+ echo $check->getDate()->format('d-m-y') . ' ' . $check->getStatus() . PHP_EOL;
212
+ }
213
+ ```
172
214
173
215
You can also access the raw API response here using array access:
174
216
175
- $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
176
- $client->verify()->search($verification);
177
- echo "Verification cost was: " . $verification['price'] . PHP_EOL;
217
+ ``` php
218
+ $verification = new \Nexmo\Verify\Verification('00e6c3377e5348cdaf567e1417c707a5');
219
+ $client->verify()->search($verification);
220
+ echo "Verification cost was: " . $verification['price'] . PHP_EOL;
221
+ ```
178
222
179
223
API Coverage
180
224
------------
0 commit comments