Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

pasos_server.php 1.5KB

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