connect-corp / nexmo-client
非官方Nexmo Rest客户端
0.1.0
2015-05-19 19:34 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~5.0
Requires (Dev)
- phpunit/phpunit: ~4.5
This package is not auto-updated.
Last update: 2024-09-25 14:37:20 UTC
README
非官方 Nexmo Rest客户端
文档
安装方法
$ composer require connect-corp/nexmo-client
使用示例
设置客户端对象
$apiKey = 'api_key_from_nexmo_account'; $apiSecret = 'api_secret_from_nexmo_account'; $nexmo = new \Nexmo\Client($apiKey, $apiSecret);
发送消息
$from = '1234567890'; $to = '15551232020'; $text = 'hello world'; try { $response = $nexmo->message->invoke($from, $to, 'text', $text); } catch (Exception $e) { die($e->getMessage()); } foreach ($response['messages'] as $i => $m) { switch ($m['status']) { case '0': echo 'Message sent successfully:'; print_r($m); break; default: echo 'Message sending failed:' print_r($m); break; } }
获取账户余额
try { $response = $nexmo->account->balance(); } catch (Exception $e) { die($e->getMessage()); } echo "Account balance is $response";
按目的地国家获取定价
$country = 'US'; try { $response = $nexmo->account->pricing->country($country); } catch (Exception $e) { die($e->getMessage()); } echo 'Price is ' . $response->price();
按收件人号码获取定价
$number = '15551232020'; try { // SMS pricing. $response = $nexmo->account->pricing->sms($number); // Voice pricing. $response = $nexmo->account->pricing->voice($number); } catch (Exception $e) { die($e->getMessage()); } echo 'Price is ' . $response->price();
按国家搜索长虚拟号码
$country = 'US'; try { $response = $nexmo->number->search($country); } catch (Exception $e) { die($e->getMessage()); } $all = $response->all(); if (isset($all['numbers'])) { foreach ($all['numbers'] as $n) { printf("%d \$%01.2f %-10s %-15s\n", $n['msisdn'], $n['cost'], $n['type'], join(',', $n['features'])); } }
购买长虚拟号码
$country = 'US'; $msisdn = '1234567890'; // Number found using $nexmo->number->search() try { $response = $nexmo->number->buy($country, $msisdn); } catch (Exception $e) { die($e->getMessage()); } if (200 == $response['error-code']) { echo 'Number purchase success'; }
列出账户中的长虚拟号码
$country = 'US'; try { $response = $nexmo->account->numbers(); } catch (Exception $e) { die($e->getMessage()); } $all = $response->all(); if (isset($all['numbers'])) { foreach ($all['numbers'] as $n) { printf("%d %-2s %-10s %-15s\n", $n['msisdn'], $n['country'], $n['type'], join(',', $n['features'])); } }
取消长虚拟号码
$country = 'US'; $msisdn = '1234567890'; // Number found using $nexmo->account->numbers() try { $response = $nexmo->number->cancel($country, $msisdn); } catch (Exception $e) { die($e->getMessage()); } if (200 == $response['error-code']) { echo 'Number cancel success'; }