Browse Source

agregar servidor que recibe y envia los mensajes como SMS

Luis 5 years ago
parent
commit
06359ced24
1 changed files with 63 additions and 0 deletions
  1. 63
    0
      pasos_server.php

+ 63
- 0
pasos_server.php View File

@@ -0,0 +1,63 @@
1
+<?php
2
+
3
+if ($_SERVER['REQUEST_METHOD'] != 'POST') {
4
+	exit;
5
+}
6
+
7
+require 'vendor/autoload.php';
8
+
9
+use libphonenumber\NumberParseException;
10
+use libphonenumber\PhoneNumber;
11
+use libphonenumber\PhoneNumberFormat;
12
+use libphonenumber\PhoneNumberUtil;
13
+
14
+use Aws\Sns\SnsClient; 
15
+use Aws\Exception\AwsException;
16
+use Aws\Credentials\Credentials;
17
+
18
+
19
+$credentials = new Credentials('AKIAZM7JX5FWJCK2HMNI', 'loeDIJqhn7WeYMR6hanqnaUEpR8KOg52C7lDv/08');
20
+
21
+$SnSclient = new SnsClient([
22
+    'region' => 'us-east-1',
23
+    'version' => '2010-03-31',
24
+    'credentials' => $credentials
25
+]);
26
+
27
+$phoneUtil = PhoneNumberUtil::getInstance();
28
+
29
+if(isset($_POST["msg"])){
30
+	$message = $_POST["msg"];
31
+}
32
+
33
+if(isset($_POST["contacts"])){
34
+	$contacts = json_decode($_POST["contacts"]);
35
+}
36
+ 
37
+
38
+for ($i = 0; $i < count($contacts); $i++) {
39
+	try {
40
+		$numberString = $contacts[$i];
41
+	    $numberPrototype = $phoneUtil->parse($numberString, "US");
42
+	    $phone_e164 = $phoneUtil->format($numberPrototype, PhoneNumberFormat::E164);
43
+	    try {
44
+    		$result = $SnSclient->publish([
45
+	        	'Message' => $message,
46
+	        	'PhoneNumber' => $phone_e164,
47
+    		]);
48
+		    // var_dump($result);
49
+		} catch (AwsException $e) {
50
+		    // output error message if fails
51
+		    error_log($e->getMessage());
52
+		}
53
+	} catch (NumberParseException $e) {
54
+    	// number is not US
55
+    	error_log($e,3,"debug/postdata.txt");
56
+	}
57
+}
58
+
59
+ob_start();
60
+var_dump($_POST);
61
+$output = ob_get_clean();
62
+error_log(date('c')." PHP: ".$_SERVER["PHP_SELF"].": ".$output."\n", 3, "debug/postdata.txt");
63
+?>