{"_id":"56b33ab2af176a0d00964ce1","category":{"_id":"56b22a9665ddf50d0076ba40","version":"568fce2b04440a1700e4cb4a","pages":[],"project":"568fce2a04440a1700e4cb47","__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"},"version":{"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","__v":20,"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"},"__v":5,"user":"55116f88e2990b0d00fb0552","parentDoc":null,"project":"568fce2a04440a1700e4cb47","updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-04T11:49:06.899Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Now let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the (ValidateNumber)[doc:rest-pnv-validatenumber] method on the Phone Number Validation API to do this.\n\nFirst, we'll make a call to Validate Number\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$validate = new stdClass();\\n$validate->Number = \\\"447700900000\\\";\\n$validate->IsInternational = true;\\n$validate->RequiredCountryCode = null;\\n\\n$result = CallJsonService(\\\"phonenumbervalidation/validatenumber\\\", \\\":::at:::Username@\\\", \\\"@Password@\\\", $validate);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nNow we need to do something with the response from the call. Let's output to the caller\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if ($result != null)\\n{\\n    switch ($result->NumberStatus)\\n    {\\n        case 2:\\n            echo \\\"<div>You phone is on, we''re sending you a message.</div>\\\";\\n            break;\\n        case 3:\\n            echo \\\"<div>You phone is off, please switch it on and try again in a few minutes.</div>\\\";\\n            break;\\n        case 4:\\n            echo \\\"<div>You phone number appears to be dead.</div>\\\";\\n            break;\\n        case 5:\\n            echo \\\"<div>You phone number isn''t on a mobile network. Have you entered a landline number.</div>\\\";\\n            break;\\n        case 6:\\n            echo \\\"<div>It looks like you entered something that isn''t a phone number.</div>\\\";\\n            break;\\n    }\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nNext we need to insert our call Send Message when we've found a phone number that's switched on:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    if ($result->NumberStatus == 2)\\n    {\\n        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        $result = CallJsonService(\\\"message/send\\\", \\\"@Username@\\\", \\\"@Password@\\\", $request);\\n\\n        if ($result != null)\\n        {\\n            if ($result[0]->successful == TRUE)\\n            {\\n                echo \\\"<div>We sent you a message.</div>\\\";\\n            }\\n            else\\n            {\\n                echo \\\"<div>Sorry, we couldn't sent you a message.</div>\\\";\\n                \\n                foreach ($result[0]->validationFailures as $failure)\\n                {\\n                    if ($failure->failureCode == \\\"ToInvalid\\\")\\n                    {\\n                        echo \\\"<div>Your phone number doesn't look like a valid number.</div>\\\";\\n                        break;\\n                    }\\n                }\\n            }\\n        }\\n    }\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"qs-php-validate-phone-numbers","type":"basic","title":"Validate Phone Numbers"}

Validate Phone Numbers


Now let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the (ValidateNumber)[doc:rest-pnv-validatenumber] method on the Phone Number Validation API to do this. First, we'll make a call to Validate Number [block:code] { "codes": [ { "code": "$validate = new stdClass();\n$validate->Number = \"447700900000\";\n$validate->IsInternational = true;\n$validate->RequiredCountryCode = null;\n\n$result = CallJsonService(\"phonenumbervalidation/validatenumber\", \"@Username@\", \"@Password@\", $validate);", "language": "php" } ] } [/block] Now we need to do something with the response from the call. Let's output to the caller [block:code] { "codes": [ { "code": "if ($result != null)\n{\n switch ($result->NumberStatus)\n {\n case 2:\n echo \"<div>You phone is on, we''re sending you a message.</div>\";\n break;\n case 3:\n echo \"<div>You phone is off, please switch it on and try again in a few minutes.</div>\";\n break;\n case 4:\n echo \"<div>You phone number appears to be dead.</div>\";\n break;\n case 5:\n echo \"<div>You phone number isn''t on a mobile network. Have you entered a landline number.</div>\";\n break;\n case 6:\n echo \"<div>It looks like you entered something that isn''t a phone number.</div>\";\n break;\n }", "language": "php" } ] } [/block] Next we need to insert our call Send Message when we've found a phone number that's switched on: [block:code] { "codes": [ { "code": " if ($result->NumberStatus == 2)\n {\n 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 $result = CallJsonService(\"message/send\", \"@Username@\", \"@Password@\", $request);\n\n if ($result != null)\n {\n if ($result[0]->successful == TRUE)\n {\n echo \"<div>We sent you a message.</div>\";\n }\n else\n {\n echo \"<div>Sorry, we couldn't sent you a message.</div>\";\n \n foreach ($result[0]->validationFailures as $failure)\n {\n if ($failure->failureCode == \"ToInvalid\")\n {\n echo \"<div>Your phone number doesn't look like a valid number.</div>\";\n break;\n }\n }\n }\n }\n }", "language": "php" } ] } [/block]