PHP code example of alexfantom / vonage-client-core
1. Go to this page and download the library: Download alexfantom/vonage-client-core 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/ */
alexfantom / vonage-client-core example snippets
$client = new Vonage\Client(new Vonage\Client\Credentials\Basic(API_KEY, API_SECRET));
$client = new Vonage\Client(
new Vonage\Client\Credentials\Basic(API_KEY, API_SECRET),
[
'base_api_url' => 'https://example.com'
]
);
$text = new \Vonage\SMS\Message\SMS(VONAGE_TO, VONAGE_FROM, 'Test message using PHP client library');
$text->setClientRef('test-message');
$response = $client->sms()->send($text);
$data = $response->current();
echo "Sent message to " . $data->getTo() . ". Balance is now " . $data->getRemainingBalance() . PHP_EOL;
$client = new Vonage\Client(new Vonage\Client\Credentials\SignatureSecret(API_KEY, SIGNATURE_SECRET, 'sha256'));
$signature = new \Vonage\Client\Signature($_GET, SIGNATURE_SECRET, 'sha256');
// is it valid? Will be true or false
$isValid = $signature->check($_GET['sig']);
$request = new \Vonage\Verify\Request('14845551212', 'My App');
$response = $client->verify()->start($request);
echo "Started verification with an id of: " . $response->getRequestId();
$request = new \Vonage\Verify\RequestPSD2('14845551212', 'My App');
$response = $client->verify()->requestPSD2($request);
echo "Started verification with an id of: " . $response['request_id'];
$basic = new \Vonage\Client\Credentials\Basic('key', 'secret');
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client(new \Vonage\Client\Credentials\Container($basic, $keypair));
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$outboundCall
->setAnswerWebhook(
new \Vonage\Voice\Webhook('https://example.com/answer')
)
->setEventWebhook(
new \Vonage\Voice\Webhook('https://example.com/event')
)
;
$response = $client->voice()->createOutboundCall($outboundCall);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$ncco = new NCCO();
//ADD ACTIONS TO THE NCCO OBJECT HERE
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$ncco = new NCCO();
$ncco->addAction(\Vonage\Voice\NCCO\Action\Record::factory([
'eventUrl' => 'https://webhook.url'
]);
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$ncco = new NCCO();
$ncco->addAction(new \Vonage\Voice\NCCO\Action\Talk('This is a text to speech call from Vonage'));
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$ncco = new NCCO();
$ncco->addAction(new \Vonage\Voice\NCCO\Action\Stream('https://my-mp3.url'));
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$ncco = new NCCO();
$ncco->addAction(\Vonage\Voice\NCCO\Action\Talk::factory('Please record your name.',[
'bargeIn' => true,
]));
$ncco->addAction(\Vonage\Voice\NCCO\Action\Input::factory([
'eventUrl' => 'https://webhook.url',
'type' => [
'speech',
],
'speech' => [
'endOnSilence' => true,
],
]));
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
$outboundCall = new \Vonage\Voice\OutboundCall(
new \Vonage\Voice\Endpoint\Phone('14843331234'),
new \Vonage\Voice\Endpoint\Phone('14843335555')
);
$ncco = new NCCO();
$ncco->addAction(new \Vonage\Voice\NCCO\Action\Talk('We are just testing the notify function, you do not need to do anything.'));
$ncco->addAction(new \Vonage\Voice\NCCO\Action\Notify([
'foo' => 'bar',
], new Vonage\Voice\Webhook('https://webhook.url')));
$outboundCall->setNCCO($ncco);
$response = $client->voice()->createOutboundCall($outboundCall);
try {
$insights = $client->insights()->basic(PHONE_NUMBER);
echo $insights->getNationalFormatNumber();
} catch (Exception $e) {
// for the Vonage-specific exceptions, try the `getEntity()` method for more diagnostic information
}
try {
$client->insights()->advancedAsync(PHONE_NUMBER, 'http://example.com/webhooks/number-insights');
} catch (Exception $e) {
// for the Vonage-specific exceptions, try the `getEntity()` method for more diagnostic information
}
$client = new Vonage\Client(
new Vonage\Client\Credentials\Basic(API_KEY, API_SECRET),
[
'show_deprecations' => true
]
);
$adapter_client = new Http\Adapter\Guzzle6\Client(new GuzzleHttp\Client(['timeout' => 5]));
$vonage_client = new Vonage\Client(new Vonage\Client\Credentials\Basic($api_key, $api_secret), [], $adapter_client);
$client = new \Vonage\Client(new \Vonage\Client\Credentials\Basic('abcd1234', 's3cr3tk3y'), ['debug' => true]);
$logger = new \Monolog\Logger('test');
$logger->pushHandler(new \Monolog\Handler\StreamHandler(__DIR__ . '/log.txt', \Monolog\Logger::DEBUG));
$client->getFactory()->set(\PSR\Log\LoggerInterface::class, $logger);