{"__v":9,"_id":"56b33a9978a12121009006a1","category":{"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","_id":"56b22a9665ddf50d0076ba40","pages":[],"__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-03T16:28:06.358Z","from_sync":false,"order":4,"slug":"php-quickstarts","title":"PHP Quickstarts"},"parentDoc":null,"project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","version":{"__v":20,"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-04T11:48:41.341Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"In the Send SMS Messages quickstart, we sent a message, but we don't know whether it's been delivered to the recipient yet; they may have their phone switched off, be in an area with no mobile coverage or the number may be out of service, so it'd be good to get some more information. To do this we'll use [Receipt Forwarding](doc:receipt-forwarding-overview).\n\nBuilding on the last sample, we're going to create a receipt handler that logs the status of each message to an in memory data store.\n\n1.Create ReceiptHandler.php which will receive and process receipts\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nrequire_once(\\\"JsonHelper.php\\\");\\n\\nini_set('display_errors', 'On');\\nsession_start();\\n\\nparse_str($_SERVER[\\\"QUERY_STRING\\\"], $request);\\n\\nif (!isset($_SESSION['ReceiptStore'])) {\\n  $_SESSION['ReceiptStore'] = array();\\n}\\n\\n$clientRef = $request[\\\"clientref\\\"];\\n\\n$_SESSION['ReceiptStore'][$clientRef] =\\n[\\n    \\\"ClientRef\\\" => $clientRef,\\n    \\\"Date\\\" => DateTime::createFromFormat('Y-m-d\\\\\\\\TH:i:s.u', $request[\\\"datetime\\\"]),\\n    \\\"Recipient\\\" => $request[\\\"recipient\\\"],\\n    \\\"StatusId\\\" => $request[\\\"statusid\\\"],\\n    \\\"StatusDescription\\\" => $request[\\\"statusdescription\\\"]\\n];\\n\\n?>\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nWe log the date the message status changed, numeric status id, the status description, the phone number that the receipt relates to and the clientRef that was specified with the message at send time.\nWe also return a 200 OK status to indicate to the sender that we've processed the receipt and don't need it to be sent again later\n\n2. We now need to enable the sending of receipts to our new handler by adding *deliverStatusUrl* to our SendMessage function from the [Send SMS Messages](doc:qs-send-sms-messages) quickstart. We'll also add a clientRef to help us match the receipt to the message that caused it to be sent:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if (!isset($_SESSION['messageId'])) {\\n    $_SESSION['messageId'] = 0;\\n} else {\\n    $_SESSION['messageId']++;\\n}\\n\\n$request[0] = new stdClass();\\n$request[0]->to = \\\"447700900000\\\";\\n$request[0]->from = \\\"Example\\\";\\n$request[0]->body = \\\"Hello, this is a test message\\\";\\n$request[0]->deliveryStatusUrl = \\\"https://inbound.example.com/SmsReceipts\\\";\\n$request[0]->clientRef = \\\"msg-\\\" . $_SESSION['messageId'];\\n\\n$result = CallJsonService(\\\"message/send\\\", \\\":::at:::Username@\\\", \\\"@Password@\\\", $request);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Note\",\n  \"body\": \"The receipt handler endpoint specified in *deliveryStatusUrl* must be publicly accessible\"\n}\n[/block]\n3. Add a view page called ReceiptViewer.php which will allow us to view the receipts we've processed\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<table>\\n    <tr>\\n        <th>ClientRef</th>\\n        <th>Received At</th>\\n        <th>Recipient</th>\\n        <th>StatusId</th>\\n        <th>Status Description</th>\\n    </tr>\\n<?php    \\n    session_start();\\n\\n    parse_str($_SERVER[\\\"QUERY_STRING\\\"], $request);\\n    \\n    foreach ($_SESSION[\\\"ReceiptStore\\\"] as $receipt)\\n    {\\n        echo \\\"<tr>\\\";\\n            echo \\\"<td>\\\" . htmlentities($receipt[\\\"ClientRef\\\"]) . \\\"</td>\\\";\\n            echo \\\"<td>\\\" . date_format($receipt[\\\"Date\\\"], 'Y-m-d H:i:s') . \\\"</td>\\\";\\n            echo \\\"<td>\\\" . $receipt[\\\"Recipient\\\"] . \\\"</td>\\\";\\n            echo \\\"<td>\\\" . $receipt[\\\"StatusId\\\"] . \\\"</td>\\\";\\n            echo \\\"<td>\\\" . htmlentities($receipt[\\\"StatusDescription\\\"]) . \\\"</td>\\\";\\n        echo \\\"</tr>\\\";        \\n    }\\n?>\\n</table>\",\n      \"language\": \"php\",\n      \"name\": \"jade\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"qs-php-receive-sms-receipts","type":"basic","title":"Receive SMS Receipts"}

Receive SMS Receipts


In the Send SMS Messages quickstart, we sent a message, but we don't know whether it's been delivered to the recipient yet; they may have their phone switched off, be in an area with no mobile coverage or the number may be out of service, so it'd be good to get some more information. To do this we'll use [Receipt Forwarding](doc:receipt-forwarding-overview). Building on the last sample, we're going to create a receipt handler that logs the status of each message to an in memory data store. 1.Create ReceiptHandler.php which will receive and process receipts [block:code] { "codes": [ { "code": "<?php\n\nrequire_once(\"JsonHelper.php\");\n\nini_set('display_errors', 'On');\nsession_start();\n\nparse_str($_SERVER[\"QUERY_STRING\"], $request);\n\nif (!isset($_SESSION['ReceiptStore'])) {\n $_SESSION['ReceiptStore'] = array();\n}\n\n$clientRef = $request[\"clientref\"];\n\n$_SESSION['ReceiptStore'][$clientRef] =\n[\n \"ClientRef\" => $clientRef,\n \"Date\" => DateTime::createFromFormat('Y-m-d\\\\TH:i:s.u', $request[\"datetime\"]),\n \"Recipient\" => $request[\"recipient\"],\n \"StatusId\" => $request[\"statusid\"],\n \"StatusDescription\" => $request[\"statusdescription\"]\n];\n\n?>", "language": "php" } ] } [/block] We log the date the message status changed, numeric status id, the status description, the phone number that the receipt relates to and the clientRef that was specified with the message at send time. We also return a 200 OK status to indicate to the sender that we've processed the receipt and don't need it to be sent again later 2. We now need to enable the sending of receipts to our new handler by adding *deliverStatusUrl* to our SendMessage function from the [Send SMS Messages](doc:qs-send-sms-messages) quickstart. We'll also add a clientRef to help us match the receipt to the message that caused it to be sent: [block:code] { "codes": [ { "code": "if (!isset($_SESSION['messageId'])) {\n $_SESSION['messageId'] = 0;\n} else {\n $_SESSION['messageId']++;\n}\n\n$request[0] = new stdClass();\n$request[0]->to = \"447700900000\";\n$request[0]->from = \"Example\";\n$request[0]->body = \"Hello, this is a test message\";\n$request[0]->deliveryStatusUrl = \"https://inbound.example.com/SmsReceipts\";\n$request[0]->clientRef = \"msg-\" . $_SESSION['messageId'];\n\n$result = CallJsonService(\"message/send\", \"@Username@\", \"@Password@\", $request);", "language": "javascript" } ] } [/block] [block:callout] { "type": "info", "title": "Note", "body": "The receipt handler endpoint specified in *deliveryStatusUrl* must be publicly accessible" } [/block] 3. Add a view page called ReceiptViewer.php which will allow us to view the receipts we've processed [block:code] { "codes": [ { "code": "<table>\n <tr>\n <th>ClientRef</th>\n <th>Received At</th>\n <th>Recipient</th>\n <th>StatusId</th>\n <th>Status Description</th>\n </tr>\n<?php \n session_start();\n\n parse_str($_SERVER[\"QUERY_STRING\"], $request);\n \n foreach ($_SESSION[\"ReceiptStore\"] as $receipt)\n {\n echo \"<tr>\";\n echo \"<td>\" . htmlentities($receipt[\"ClientRef\"]) . \"</td>\";\n echo \"<td>\" . date_format($receipt[\"Date\"], 'Y-m-d H:i:s') . \"</td>\";\n echo \"<td>\" . $receipt[\"Recipient\"] . \"</td>\";\n echo \"<td>\" . $receipt[\"StatusId\"] . \"</td>\";\n echo \"<td>\" . htmlentities($receipt[\"StatusDescription\"]) . \"</td>\";\n echo \"</tr>\"; \n }\n?>\n</table>", "language": "php", "name": "jade" } ] } [/block]