From 9b8ed284f61ac8f7da94688c95ce5719b22fe685 Mon Sep 17 00:00:00 2001 From: Tony Bedford Date: Thu, 25 Jun 2020 11:07:55 +0100 Subject: [PATCH 1/5] PHP examples for Reports API --- .env-example | 7 ++++++- reports/create-async-report.php | 20 ++++++++++++++++++++ reports/get-report-status.php | 13 +++++++++++++ reports/get-report.php | 18 ++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 reports/create-async-report.php create mode 100644 reports/get-report-status.php create mode 100644 reports/get-report.php diff --git a/.env-example b/.env-example index 9743233..071d9d8 100644 --- a/.env-example +++ b/.env-example @@ -27,4 +27,9 @@ BRAND_NAME=Acme, Inc. CODE= NUMBER= REQUEST_ID= -WORKFLOW_ID=4 \ No newline at end of file +WORKFLOW_ID=4 + +# Reports API +ACCOUNT_ID= +FILE_ID= +REQUEST_ID= diff --git a/reports/create-async-report.php b/reports/create-async-report.php new file mode 100644 index 0000000..a41dbc3 --- /dev/null +++ b/reports/create-async-report.php @@ -0,0 +1,20 @@ + "SMS", + "account_id" => getenv('ACCOUNT_ID'), + "direction" => "outbound" +]; +$body = json_encode($data); +$options = ["http" => [ + "method" => "POST", + "header" => ["Authorization: Basic " . $auth, + "Content-Type: application/json"], + "ignore_errors" => true, + "content" => $body + ]]; +$context = stream_context_create($options); +// make the request +$response = file_get_contents($url, false, $context); +var_dump($response); diff --git a/reports/get-report-status.php b/reports/get-report-status.php new file mode 100644 index 0000000..1f3766b --- /dev/null +++ b/reports/get-report-status.php @@ -0,0 +1,13 @@ + [ + "method" => "GET", + "header" => ["Authorization: Basic " . $auth], + "ignore_errors" => true + ]]; +$context = stream_context_create($options); +// make the request +$response = file_get_contents($url, false, $context); +var_dump($response); diff --git a/reports/get-report.php b/reports/get-report.php new file mode 100644 index 0000000..a83d2a6 --- /dev/null +++ b/reports/get-report.php @@ -0,0 +1,18 @@ + [ + "method" => "GET", + "header" => ["Authorization: Basic " . $auth], + "ignore_errors" => true + ]]; +$context = stream_context_create($options); +// stream to download +$stream = fopen($url, 'rb', false, $context); +// stream to local file +$fp = fopen("report.zip", 'wb'); +// connect the hoses together +stream_copy_to_stream($stream, $fp); +fclose($stream); +fclose($fp); From 38857891a319c3449aedc5ee3a4eb3d02bcb8587 Mon Sep 17 00:00:00 2001 From: Tony Bedford Date: Thu, 25 Jun 2020 11:49:43 +0100 Subject: [PATCH 2/5] Modified to use repo convention for variable load. --- reports/create-async-report.php | 7 +++++-- reports/get-report-status.php | 9 +++++++-- reports/get-report.php | 10 ++++++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/reports/create-async-report.php b/reports/create-async-report.php index a41dbc3..916262c 100644 --- a/reports/create-async-report.php +++ b/reports/create-async-report.php @@ -1,9 +1,12 @@ "SMS", - "account_id" => getenv('ACCOUNT_ID'), + "account_id" => ACCOUNT_ID, "direction" => "outbound" ]; $body = json_encode($data); diff --git a/reports/get-report-status.php b/reports/get-report-status.php index 1f3766b..324e2fa 100644 --- a/reports/get-report-status.php +++ b/reports/get-report-status.php @@ -1,7 +1,10 @@ [ "method" => "GET", "header" => ["Authorization: Basic " . $auth], @@ -11,3 +14,5 @@ // make the request $response = file_get_contents($url, false, $context); var_dump($response); + +echo(REQUEST_ID); \ No newline at end of file diff --git a/reports/get-report.php b/reports/get-report.php index a83d2a6..61b8d54 100644 --- a/reports/get-report.php +++ b/reports/get-report.php @@ -1,7 +1,13 @@ [ "method" => "GET", "header" => ["Authorization: Basic " . $auth], From 337ea71abb564a8e6baf571dc5bf06a066563617 Mon Sep 17 00:00:00 2001 From: Tony Bedford Date: Thu, 25 Jun 2020 11:50:02 +0100 Subject: [PATCH 3/5] Add files to ignore when testing locally --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 1209ce6..8192521 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ vendor private.key .env .DS_Store +composer.phar +report.zip +composer-setup.php From 55d5a8b61e08f6e12258cc22f1d42cfe77d4e034 Mon Sep 17 00:00:00 2001 From: Tony Bedford Date: Thu, 25 Jun 2020 11:52:50 +0100 Subject: [PATCH 4/5] Remove debugging code inadvertently left in. --- reports/get-report.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/reports/get-report.php b/reports/get-report.php index 61b8d54..f412eeb 100644 --- a/reports/get-report.php +++ b/reports/get-report.php @@ -2,9 +2,6 @@ require_once __DIR__ . '/../config.php'; require_once __DIR__ . '/../vendor/autoload.php'; -echo(FILE_ID . "\n\n"); -echo("-------------------------------"); - $auth = base64_encode(NEXMO_API_KEY . ":" . NEXMO_API_SECRET); $url = 'https://api.nexmo.com/v3/media/' . FILE_ID; From 8b0fe5690c7fad0dc93289cc5543180806f1da9d Mon Sep 17 00:00:00 2001 From: Tony Bedford Date: Thu, 25 Jun 2020 11:57:06 +0100 Subject: [PATCH 5/5] Removed another missed debug message. --- reports/get-report-status.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/reports/get-report-status.php b/reports/get-report-status.php index 324e2fa..126d1ed 100644 --- a/reports/get-report-status.php +++ b/reports/get-report-status.php @@ -14,5 +14,3 @@ // make the request $response = file_get_contents($url, false, $context); var_dump($response); - -echo(REQUEST_ID); \ No newline at end of file