From 08643336d996d84bb24381e63b274a227c469a7a Mon Sep 17 00:00:00 2001 From: Christian Giupponi Date: Sat, 1 Nov 2014 19:39:13 +0100 Subject: [PATCH] First upload --- .gitignore | 4 + .travis.yml | 13 ++ composer.json | 25 +++ phpunit.xml | 18 ++ public/.gitkeep | 0 src/ChristianGiupponi/Nexmo/Facades/Nexmo.php | 14 ++ src/ChristianGiupponi/Nexmo/Nexmo.php | 164 ++++++++++++++++++ .../Nexmo/NexmoServiceProvider.php | 47 +++++ src/config/.gitkeep | 0 src/config/config.php | 6 + src/controllers/.gitkeep | 0 src/lang/.gitkeep | 0 src/migrations/.gitkeep | 0 src/views/.gitkeep | 0 tests/.gitkeep | 0 15 files changed, 291 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 public/.gitkeep create mode 100644 src/ChristianGiupponi/Nexmo/Facades/Nexmo.php create mode 100644 src/ChristianGiupponi/Nexmo/Nexmo.php create mode 100644 src/ChristianGiupponi/Nexmo/NexmoServiceProvider.php create mode 100644 src/config/.gitkeep create mode 100644 src/config/config.php create mode 100644 src/controllers/.gitkeep create mode 100644 src/lang/.gitkeep create mode 100644 src/migrations/.gitkeep create mode 100644 src/views/.gitkeep create mode 100644 tests/.gitkeep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5826402 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/vendor +composer.phar +composer.lock +.DS_Store diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..f60bbe0 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,13 @@ +language: php + +php: + - 5.4 + - 5.5 + - 5.6 + - hhvm + +before_script: + - travis_retry composer self-update + - travis_retry composer install --prefer-source --no-interaction --dev + +script: phpunit diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..29540c3 --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "christian-giupponi/nexmo", + "description": "This is a Laravel4 package that helps you integrate Nexmo sms services into your site.", + "License": "MIT", + "authors": [ + { + "name": "Christian Giupponi", + "email": "christian.giupponi@stilogo.it" + } + ], + "require": { + "php": ">=5.4.0", + "illuminate/support": "4.2.*", + "guzzlehttp/guzzle": "~5.0" + }, + "autoload": { + "classmap": [ + "src/migrations" + ], + "psr-0": { + "ChristianGiupponi\\Nexmo\\": "src/" + } + }, + "minimum-stability": "stable" +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..3347b75 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,18 @@ + + + + + ./tests/ + + + diff --git a/public/.gitkeep b/public/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/ChristianGiupponi/Nexmo/Facades/Nexmo.php b/src/ChristianGiupponi/Nexmo/Facades/Nexmo.php new file mode 100644 index 0000000..a972e3f --- /dev/null +++ b/src/ChristianGiupponi/Nexmo/Facades/Nexmo.php @@ -0,0 +1,14 @@ +get_request( $base_url ); + + } + + /** + * pricing + * Get the price of a given country. + * It returns also the available carrier name with price. + * + * @param string $countryOrPrefix + * @return json + */ + public function pricing( $countryOrPrefix = '' ) + { + //check if the request is fo a specific country or prefix + if( trim( $countryOrPrefix ) == '' ) + { + return json_encode( [ + 'code' => '400', + 'reason' => 'Code or Prefix required', + 'body' => '' + ] ); + } + + $base_url = 'get-pricing/outbound/'; + $params = '/' . $countryOrPrefix; + + return $this->get_request( $base_url, $params ); + + } + + /** + * Send SMS + * + * This function will send a post request to Nexmo website to send a new sms + * To see all the options available please visite nexmo website: + * https://docs.nexmo.com/index.php/sms-api/send-message + * The option must be formed like: array( 'foo' => 'bar' ) + * + * @param string $from + * @param string $to + * @param string $text + * @param array $options + * @return json + */ + public function sendSMS( $from = '', $to = '', $text = '', $options = [ ] ) + { + if( trim( $from ) == '' || trim( $to ) == '' || trim( $text ) == '' ) + { + return json_encode( [ + 'code' => '400', + 'reason' => 'Missing parameter, Form - To and Text are required.', + 'body' => '' + ] ); + } + + $required = [ + 'from' => $from, + 'to' => $to, + 'text' => urlencode( $text ) + ]; + + return $this->post_request( $required, $options ); + + } + + /** + * api_request + * This function will make the api call using GuzzleHttp + * + * @param $method + * @param $base_url + * @param string $params + * @return json + */ + public function get_request( $base_url, $params = '' ) + { + $this->apiKey = \Config::get( 'nexmo::nexmo_api_key' ); + $this->apiSecret = \Config::get( 'nexmo::nexmo_api_secret' ); + + //Create the url + $url = $this->nexmoUrl . $base_url . $this->apiKey . '/' . $this->apiSecret . $params; + + //Initialize a new client for api request + $client = new Client(); + + //Send the request + $response = $client->get( $url, [ + 'headers' => [ 'Accept' => 'application/json' ] + ] ); + + //Get the result + $code = $response->getStatusCode(); + $reason = $response->getReasonPhrase(); + $body = $response->json(); + + //Format the result + return json_encode( [ + 'code' => $code, + 'reason' => $reason, + 'body' => $body + ] ); + } + + public function post_request( $requried = [ ], $optional = [ ] ) + { + //Get api from config file + $this->apiKey = \Config::get( 'nexmo::nexmo_api_key' ); + $this->apiSecret = \Config::get( 'nexmo::nexmo_api_secret' ); + + //Create uri params with the required file + $required_url = http_build_query( $requried ); + + //If there are any optional fields let's add them + $optional_url = ( count( $optional ) > 0 ) ? '&' . http_build_query( $optional ) : ''; + + //This is the final url + $url = $this->nexmoPostUrl . "?api_key=" . $this->apiKey . '&api_secret=' . $this->apiSecret . '&' . $required_url . $optional_url; + + //Initialize a new client for api request + $client = new Client(); + + //Make POST request + $response = $client->post( $url ); + + //Get the result + $code = $response->getStatusCode(); + $reason = $response->getReasonPhrase(); + $body = $response->json(); + + //Format the result + return json_encode( [ + 'code' => $code, + 'reason' => $reason, + 'body' => $body + ] ); + + } +} \ No newline at end of file diff --git a/src/ChristianGiupponi/Nexmo/NexmoServiceProvider.php b/src/ChristianGiupponi/Nexmo/NexmoServiceProvider.php new file mode 100644 index 0000000..06e827d --- /dev/null +++ b/src/ChristianGiupponi/Nexmo/NexmoServiceProvider.php @@ -0,0 +1,47 @@ +package('christian-giupponi/nexmo'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + $this->app['nexmo'] = $this->app->share(function($app) + { + return new Nexmo; + }); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return array(); + } + +} diff --git a/src/config/.gitkeep b/src/config/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/config/config.php b/src/config/config.php new file mode 100644 index 0000000..49f0ad3 --- /dev/null +++ b/src/config/config.php @@ -0,0 +1,6 @@ + '', + 'nexmo_api_secret' => '' +]; \ No newline at end of file diff --git a/src/controllers/.gitkeep b/src/controllers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/lang/.gitkeep b/src/lang/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/migrations/.gitkeep b/src/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/views/.gitkeep b/src/views/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/tests/.gitkeep b/tests/.gitkeep new file mode 100644 index 0000000..e69de29