{"__v":17,"_id":"56b22b962d96461700599228","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":["56f924f16dd61e2900ea2d20"],"next":{"pages":[],"description":""},"createdAt":"2016-02-03T16:32:22.683Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"\",\n  \"body\": \"Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/PHP.zip\"\n}\n[/block]\n##Prerequisites\nYou'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing.\n\nBefore you start, you *must* have an up to date CA root bundle for cURL. Download the [CA root bundle for cURL](https://curl.haxx.se/ca/cacert.pem), copy it into your PHP directory and update the *curl.cainfo* PHP setting to the path to the new file.\n\n##Hello World via SMS\nWe're going to create a simple PHP based web application that sends a hello message when the user lands on a page.\n\nWe'll start by creating JsonHelper.php which conveniently wraps the calling of RESTful APIs.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\nfunction CallJsonService($servicePath, $username, $password, $requestObject)\\n{\\n\\t$jsonData = json_encode($requestObject);\\n\\t$ch = curl_init();\\n\\t$options = array(\\n\\t\\tCURLOPT_POST\\t\\t\\t=> true,\\n\\t\\tCURLOPT_RETURNTRANSFER \\t=> true,     // return web page\\n\\t\\tCURLOPT_HEADER         \\t=> false,    // don't return headers\\n\\t\\tCURLOPT_ENCODING       \\t=> \\\"\\\",       // handle all encodings\\n\\t\\tCURLOPT_CONNECTTIMEOUT \\t=> 10,       // timeout on connect\\n\\t\\tCURLOPT_TIMEOUT        \\t=> 130,      // timeout on response\\n\\t\\tCURLOPT_HTTPHEADER\\t\\t=> array(\\n\\t\\t\\t\\\"Content-type: application/json\\\",\\n\\t\\t\\t\\\"Connection: close\\\",\\n\\t\\t\\t\\\"Content-length: \\\" . strlen($jsonData),\\n\\t\\t\\t\\\"Accept: application/json\\\",\\n\\t\\t\\t\\\"Authorization: Basic \\\" . base64_encode($username . \\\":\\\" . $password)),\\n\\t\\tCURLOPT_URL\\t\\t\\t\\t=> \\\"https://services.dynmark.com/WebApi/\\\" . $servicePath,\\n\\t\\tCURLOPT_POSTFIELDS\\t\\t=> $jsonData\\n\\t);\\n\\tcurl_setopt_array($ch, $options);\\n\\n\\t$response = curl_exec($ch);\\n\\t$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);\\n\\tcurl_close($ch);\\n\\t\\n\\tif ($status > 200 && status < 300)\\n\\t{\\n\\t\\treturn json_decode($response);\\n\\t}\\n\\telse if ($status == 401)\\n\\t{\\n\\t\\techo \\\"<div>Invalid credentials</div>\\\"\\n\\t}\\n\\telse if ($status == 400)\\n\\t{\\n\\t\\techo \\\"<div>Bad request format</div>\\\"\\n\\t}\\n\\telse\\n\\t{\\n\\t\\techo \\\"<div>Non success response</div>\\\"\\n\\t}\\n}\\n?>\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nLets look at the details:\n1. You'll need your Dynmark username and password to authenticate calls.\n2. We use curl to make the actual HTTP request.\n3. If the response is 2xx, we can parse the JSON response to an object and return it. \n4. If the response is *400 Bad Request * or *401 Unauthorised* we write an error message to the output.\n \nNow we'll create the actual send message page\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<?php\\n\\nrequire_once(\\\"JsonHelper.php\\\");\\n\\nini_set('display_errors', 'On');\\n\\nfunction SendMessage()\\n{\\n    $request[0] = new stdClass();\\n\\t  $request[0]->to = \\\"447700900000\\\";\\n\\t  $request[0]->from = \\\"Example\\\";\\n\\t  $request[0]->body = \\\"Hello, this is a test message\\\",\\n\\t  $result = CallJsonService(\\\"message/send\\\", \\\":::at:::Username@\\\", \\\"@Password@\\\", $request);\\n\\n    echo $result;\\n}\\n\\n?>\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nHere we simply define an SMS message request containing the bare minimum details to send an SMS and output the results to the page\n\nUsers often make mistakes entering their details, so we check the HTTP status for common failures and for  Let's add some code to check the response from the API for validation failures:\n\n1. Remove the echo and add a check on the $result object to and add code to check the *successful* value of the result object. This shows that the API call was in a valid format.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"...\\n\\nif ($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\\n?>\\n\",\n      \"language\": \"php\",\n      \"name\": \"\"\n    }\n  ]\n}\n[/block]","excerpt":"Sending text messages via the REST API","slug":"qs-php-send-sms-messages","type":"basic","title":"Send SMS Messages"}

Send SMS Messages

Sending text messages via the REST API

[block:callout] { "type": "info", "title": "", "body": "Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/PHP.zip" } [/block] ##Prerequisites You'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing. Before you start, you *must* have an up to date CA root bundle for cURL. Download the [CA root bundle for cURL](https://curl.haxx.se/ca/cacert.pem), copy it into your PHP directory and update the *curl.cainfo* PHP setting to the path to the new file. ##Hello World via SMS We're going to create a simple PHP based web application that sends a hello message when the user lands on a page. We'll start by creating JsonHelper.php which conveniently wraps the calling of RESTful APIs. [block:code] { "codes": [ { "code": "<?php\nfunction CallJsonService($servicePath, $username, $password, $requestObject)\n{\n\t$jsonData = json_encode($requestObject);\n\t$ch = curl_init();\n\t$options = array(\n\t\tCURLOPT_POST\t\t\t=> true,\n\t\tCURLOPT_RETURNTRANSFER \t=> true, // return web page\n\t\tCURLOPT_HEADER \t=> false, // don't return headers\n\t\tCURLOPT_ENCODING \t=> \"\", // handle all encodings\n\t\tCURLOPT_CONNECTTIMEOUT \t=> 10, // timeout on connect\n\t\tCURLOPT_TIMEOUT \t=> 130, // timeout on response\n\t\tCURLOPT_HTTPHEADER\t\t=> array(\n\t\t\t\"Content-type: application/json\",\n\t\t\t\"Connection: close\",\n\t\t\t\"Content-length: \" . strlen($jsonData),\n\t\t\t\"Accept: application/json\",\n\t\t\t\"Authorization: Basic \" . base64_encode($username . \":\" . $password)),\n\t\tCURLOPT_URL\t\t\t\t=> \"https://services.dynmark.com/WebApi/\" . $servicePath,\n\t\tCURLOPT_POSTFIELDS\t\t=> $jsonData\n\t);\n\tcurl_setopt_array($ch, $options);\n\n\t$response = curl_exec($ch);\n\t$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\tcurl_close($ch);\n\t\n\tif ($status > 200 && status < 300)\n\t{\n\t\treturn json_decode($response);\n\t}\n\telse if ($status == 401)\n\t{\n\t\techo \"<div>Invalid credentials</div>\"\n\t}\n\telse if ($status == 400)\n\t{\n\t\techo \"<div>Bad request format</div>\"\n\t}\n\telse\n\t{\n\t\techo \"<div>Non success response</div>\"\n\t}\n}\n?>", "language": "php" } ] } [/block] Lets look at the details: 1. You'll need your Dynmark username and password to authenticate calls. 2. We use curl to make the actual HTTP request. 3. If the response is 2xx, we can parse the JSON response to an object and return it. 4. If the response is *400 Bad Request * or *401 Unauthorised* we write an error message to the output. Now we'll create the actual send message page [block:code] { "codes": [ { "code": "<?php\n\nrequire_once(\"JsonHelper.php\");\n\nini_set('display_errors', 'On');\n\nfunction SendMessage()\n{\n $request[0] = new stdClass();\n\t $request[0]->to = \"447700900000\";\n\t $request[0]->from = \"Example\";\n\t $request[0]->body = \"Hello, this is a test message\",\n\t $result = CallJsonService(\"message/send\", \"@Username@\", \"@Password@\", $request);\n\n echo $result;\n}\n\n?>", "language": "php" } ] } [/block] Here we simply define an SMS message request containing the bare minimum details to send an SMS and output the results to the page Users often make mistakes entering their details, so we check the HTTP status for common failures and for Let's add some code to check the response from the API for validation failures: 1. Remove the echo and add a check on the $result object to and add code to check the *successful* value of the result object. This shows that the API call was in a valid format. [block:code] { "codes": [ { "code": "...\n\nif ($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\n?>\n", "language": "php", "name": "" } ] } [/block]