{"id":858,"date":"2022-06-29T14:59:51","date_gmt":"2022-06-29T14:59:51","guid":{"rendered":"https:\/\/test.thesmsworks.co.uk\/blog\/?p=858"},"modified":"2022-09-09T15:38:11","modified_gmt":"2022-09-09T15:38:11","slug":"send-sms-message-php","status":"publish","type":"post","link":"https:\/\/thesmsworks.co.uk\/blog\/send-sms-message-php\/","title":{"rendered":"How to send an SMS in PHP"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"858\" class=\"elementor elementor-858\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-20c90981 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"20c90981\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-715f118c\" data-id=\"715f118c\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-2560b97a elementor-widget elementor-widget-text-editor\" data-id=\"2560b97a\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<h1 class=\"wp-block-heading\">How to send\u00a0an SMS\u00a0with PHP &#8211; a simple guide<\/h1>\n\n<p>More and more businesses are using SMS as a way of communicating with their customers.\u00a0<\/p>\n\n<p>Despite SMS being nearly thirty years old and incredibly basic,\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/A2P-SMS\">A2P SMS<\/a><\/strong>\u00a0volumes are predicted to rise by at least 4.4% every year until 2027, according to Juniper research.<\/p>\n\n<p>Most companies are looking for a fast and simple integration that will be hassle free, reliable and low cost.<\/p>\n\n<p>For many that will mean using PHP as the coding language for SMS integration.<\/p>\n\n<h2 class=\"wp-block-heading\"><strong>What is PHP?<\/strong><\/h2>\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><a href=\"https:\/\/www.php.net\/manual\/en\/intro-whatis.php\">PHP<\/a><\/strong>\u00a0is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.<\/p>\n<\/blockquote>\n\n<h2 class=\"wp-block-heading\"><strong>Why is PHP so popular for sending SMS?<\/strong><\/h2>\n\n<p>PHP is often the go to coding language for sending SMS. There are a few reasons why it\u2019s so popular.<\/p>\n\n<p><strong>1 PHP is easy to learn<\/strong><br \/>PHP is really easy to get to grips with. Compared to other languages there are fewer challenging concepts to grapple with.<\/p>\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em><strong>No other language is as easy for a beginner to get visible results<\/strong><\/em><br \/><strong>Bill Karwin<\/strong>&#8211; SQL developer, consultant, trainer<\/p>\n<\/blockquote>\n\n<p><strong>2.PHP is flexible and forgiving<\/strong><br \/>Minor coding mistakes don\u2019t cause the code to stop working, so it\u2019s perfect if you need to get a proof of concept up and running fast.<\/p>\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>Quick and Dirty is the default.<\/strong><br \/><strong>Bob Fanger<\/strong>, Developer<\/p>\n<\/blockquote>\n\n<p><strong>3.PHP is well documented<\/strong><br \/>There are countless tutorials, training programmes and video courses on PHP.<\/p>\n\n<p>Whatever you\u2019re trying to do, there will be a wealth of resources available.<\/p>\n\n<p><strong>4. PHP is free<\/strong><br \/>Since it began back in 1995, PHP has always been a free, collaborative project.<\/p>\n\n<h2 class=\"wp-block-heading\"><strong>3 simple steps to integrate SMS with PHP<\/strong><\/h2>\n\n<p>It&#8217;s a really simple process to send SMS with PHP.<\/p>\n\n<p>Here are the 3 simple steps you need to take.<\/p>\n\n<p><strong>1.Set up a free account<\/strong><br \/>Your\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/user\/register\">free SMS API account<\/a><\/strong>\u00a0will come with 50 free text credits, so you test us at no cost.<\/p>\n\n<p><strong>2.set up authentication<\/strong><br \/>Generate an API Key &amp; Secret and use these to generate a JSON Web Token.<\/p>\n\n<p>The JSON web token is passed in the &#8216;Authorization&#8217; header with all calls to the base URL https:\/\/api.thesmsworks.co.uk\/v1<\/p>\n\n<p><strong>3. Use our simple methods to send and receive messages<\/strong><\/p>\n\n<p>There are 3 options.<br \/>1. Choose from our\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/developers#sdks\">SDKs<\/a><\/strong><br \/>2. Simple integration with cURL using our\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/developers#sending\">PHP code snippets<\/a><\/strong><br \/>3. Make regular HTTP POST and GET calls to our\u00a0<strong><a href=\"https:\/\/api.thesmsworks.co.uk\/docs\/api-reference.html\">SMS API end-points<\/a><\/strong><\/p>\n\n<h2 class=\"wp-block-heading\"><strong>SMS PHP documentation<\/strong><\/h2>\n\n<p>All the documentation for sending SMS with PHP, along with sample code can be found on our\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/developers\">developers\u2019 page<\/a><\/strong>. Developers &#8211; SMS API method docs, Download SDKs | The SMS Works<\/p>\n\n<h2 class=\"wp-block-heading\"><strong>Sample PHP code to send an SMS in PHP<\/strong><\/h2>\n\n<p>The following PHP code can be used to send texts using The SMS Works\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/\">SMS API<\/a><\/strong>.<\/p>\n\n<p>&lt;!?php<br \/>\u00a0 \/* requires SMS Works PHP SDK *\/<br \/>\u00a0 require_once(__DIR__ . &#8216;\/vendor\/autoload.php&#8217;);<\/p>\n\n<p>\u00a0 \/\/ Configure API key authorization: JWT<br \/>\u00a0 $config = \\Swagger\\Client\\Configuration::getDefaultConfiguration()-&gt;setApiKey(&#8216;Authorization&#8217;, &#8216;Your JSON Web Token goes here&#8217;);<\/p>\n\n<p>\u00a0 $apiInstance = new \\Swagger\\Client\\Api\\MessagesApi(<br \/>\u00a0 \u00a0 \u00a0 new \\GuzzleHttp\\Client(),<br \/>\u00a0 \u00a0 \u00a0 $config<br \/>\u00a0 );<\/p>\n\n<p>\u00a0 $sender = &#8216;MyCompany&#8217;;<br \/>\u00a0 $destination = &#8216;07777777777&#8217;;<br \/>\u00a0 $content = &#8216;My super awesome message&#8217;;<br \/>\u00a0 $schedule = &#8221;;<\/p>\n\n<p>\u00a0 $data = array(&#8220;sender&#8221;=&gt;$sender,&#8221;destination&#8221;=&gt;$destination,&#8221;content&#8221;=&gt;$content,&#8221;schedule&#8221;=&gt;$schedule);<\/p>\n\n<p>\u00a0 $sms_message = new \\Swagger\\Client\\Model\\Message($data);<\/p>\n\n<p>\u00a0 try {<br \/>\u00a0 \u00a0 \u00a0 $result = $apiInstance-&gt;sendMessage($sms_message);<br \/>\u00a0 \u00a0 \u00a0 print_r($result);<br \/>\u00a0 } catch (\\Swagger\\Client\\ApiException $e) {<br \/>\u00a0 \u00a0 \u00a0 echo &#8216;Exception when calling MessagesApi-&gt;sendMessage: &#8216;, $e-&gt;getMessage(), PHP_EOL;<br \/>\u00a0 }<br \/>?&gt;<\/p>\n\n<h2 class=\"wp-block-heading\"><strong>\u00a0Parameter definitions for sending an SMS in PHP<\/strong><\/h2>\n\n<p><strong>sender<\/strong><br \/>The sender of the message. Should be no longer than 11 characters for alphanumeric or 15 characters for numeric sender ID&#8217;s. No spaces or special characters. Read more details about\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/sms-sender-id\">SMS sender ID<\/a><\/strong>. https:\/\/thesmsworks.co.uk\/sms-sender-id<\/p>\n\n<p><strong>destination<\/strong><br \/>The mobile number or\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/MSISDN\">MSISDN<\/a><\/strong>\u00a0that you&#8217;re sending to using PHP. Numbers can be either in UK format, 07900123456 or International format, 447900123456. Do not start numbers with the + sign.<\/p>\n\n<p><strong>content<\/strong><br \/>The message itself. A standard text is 160 characters, including spaces. You can send messages of up to 1280 characters in length. This article explains more about message length when sending a message in PHP https:\/\/thesmsworks.co.uk\/maximum-length-sms-text-message<\/p>\n\n<p><strong>deliveryreporturl<\/strong><br \/>The url to which we should POST <strong><a href=\"https:\/\/thesmsworks.co.uk\/blog\/sms-delivery-reports\">delivery reports<\/a><\/strong> to for this message. If none is specified, we&#8217;ll use the global delivery report URL that you&#8217;ve configured on your account page.<\/p>\n\n<p><strong>schedule<\/strong><br \/>The schedule date format should match the JavaScript date format, which is the same as the ISO 8601 syntax. E.g Sun Sep 03 2017 15:34:23 GMT+0100 (BST)<\/p>\n\n<p><strong>tag<\/strong><br \/>You can add a tag to each message which can be useful for identifying all numbers that were sent a particular message in PHP or are part of the same campaign. The tag can be alphanumeric and contain spaces. The maximum number of characters is 280, including spaces.<\/p>\n\n<p><strong>ttl<\/strong><br \/>Time To Live. The optional number of minutes before the message is deleted, defined as an integer. Optional. Omit this parameter to prevent delivery report deletion.<\/p>\n\n<p><strong>responseemail<\/strong><br \/>An optional array of email addresses to forward responses to this specific message to. An SMS Works\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/incoming-sms\">Reply Number<\/a><\/strong>\u00a0is required to use this feature.<\/p>\n\n<p><strong>metadata<\/strong><br \/>Include an array of metadata objects on messages you send with the SMS Works API. Any supplied metadata will be stored on delivery reports and included in the API response sent for calls to the \/message\/send end-point.<\/p>\n\n<p>Metadata must be provided with &#8216;key&#8217; and &#8216;value&#8217; parameters, as shown in the following example:<\/p>\n\n<p>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&#8220;metadata&#8221;: [<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0{ &#8220;key&#8221;:&#8221;branchGUID&#8221;, &#8220;value&#8221;: &#8220;e60e5f31-etc&#8221;},<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0{ &#8220;key&#8221;:&#8221;messageQueueID&#8221;, &#8220;value&#8221;: &#8220;9648523&#8221;}<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0]<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/>This will be returned in the delivery report formatted as follows:<\/p>\n\n<p>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0&#8220;metadata&#8221;: {<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8220;branchGUID&#8221;: &#8220;e60e5f31-etc&#8221;,<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8220;messageQueueID&#8221;: &#8220;9648523&#8221;<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0 \u00a0 \u00a0}<br \/>\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<br \/><strong>validity<\/strong><br \/>The optional number of minutes to attempt delivery before the message is marked as EXPIRED. The default is 2880 minutes. Integer.<\/p>\n\n<h2 class=\"wp-block-heading\"><strong>How much does it cost to send a text in PHP?<\/strong><\/h2>\n\n<p>There are probably over 100 SMS providers that allow you to send text messages in PHP.<\/p>\n\n<p>We\u2019ve created a list of over\u00a0<a href=\"https:\/\/thesmsworks.co.uk\/smsapi#list\">100\u00a0SMSAPI providers\u00a0here.<\/a><\/p>\n\n<p>Apart from offering a competitive price, you should select a company that\u2019s based in your country or the country in which you wish to send messages using PHP<\/p>\n\n<p>There\u2019s quite a wide range of SMS pricing, from about 7 pence to just under 3 pence, excluding VAT<br \/>Here\u2019s a list of some of the providers that allow you to send text messages in PHP.<\/p>\n\n<figure class=\"wp-block-table\">\n<table>\n<tbody>\n<tr>\n<td><strong>SMS API Provider<\/strong><\/td>\n<td><strong>Cost per text in pence<\/strong><\/td>\n<\/tr>\n<tr>\n<td>The SMS Works<\/td>\n<td>3.15p<\/td>\n<\/tr>\n<tr>\n<td>Esendex<\/td>\n<td>7.0p<\/td>\n<\/tr>\n<tr>\n<td>Text Local<\/td>\n<td>4.9p<\/td>\n<\/tr>\n<tr>\n<td>Clockwork<\/td>\n<td>4.0p<\/td>\n<\/tr>\n<tr>\n<td>Dragon Fly<\/td>\n<td>3.9p<\/td>\n<\/tr>\n<tr>\n<td>Bulk SMS<\/td>\n<td>4.5p<\/td>\n<\/tr>\n<tr>\n<td>Kapow<\/td>\n<td>6.25p<\/td>\n<\/tr>\n<tr>\n<td>Intellisms<\/td>\n<td>4.4p<\/td>\n<\/tr>\n<tr>\n<td>Fast SMS<\/td>\n<td>3.5p<\/td>\n<\/tr>\n<tr>\n<td>Click SMS<\/td>\n<td>3.7p<\/td>\n<\/tr>\n<tr>\n<td>SendMode<\/td>\n<td>3.4p<\/td>\n<\/tr>\n<tr>\n<td>FireText<\/td>\n<td>4.9p<\/td>\n<\/tr>\n<tr>\n<td>Text Anywhere<\/td>\n<td>6.4p<\/td>\n<\/tr>\n<tr>\n<td>Text Marketer<\/td>\n<td>3.3p<\/td>\n<\/tr>\n<tr>\n<td>Red SMS<\/td>\n<td>4.5p<\/td>\n<\/tr>\n<tr>\n<td>Text Tank<\/td>\n<td>3.5p<\/td>\n<\/tr>\n<tr>\n<td>Green Text<\/td>\n<td>3.6p<\/td>\n<\/tr>\n<tr>\n<td>2sms<\/td>\n<td>10p<\/td>\n<\/tr>\n<tr>\n<td>Think Message<\/td>\n<td>3.9p<\/td>\n<\/tr>\n<tr>\n<td>Text Magic<\/td>\n<td>4.0p<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n<p>The SMS Works operates a\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/pricing\">sliding scale of SMS message costs<\/a><\/strong>, with message rates starting at just 2.99 pence + VAT per text.<\/p>\n\n<p>We\u2019ve also created an\u00a0<strong><a href=\"https:\/\/thesmsworks.co.uk\/sms-price-comparison-calculator\">SMS price comparison tool<\/a><\/strong>\u00a0that allows you to compare SMS prices for different providers.<\/p>\n\n<h2 class=\"wp-block-heading\"><strong>Support for sending texts in PHP<\/strong><\/h2>\n\n<p>We&#8217;re here to help.<\/p>\n\n<p>So whatever your query, please get tin touch. We do support by email, so any queries, please contact us on\u00a0<strong>support@thesmsworks.co.uk<\/strong><\/p>\n\n<p>We&#8217;re really responsive and aim to get back to you within the hour, often we&#8217;re even quicker than that.<\/p>\n\n<p><strong>Related articles<\/strong><\/p>\n\n<p><strong><a href=\"https:\/\/thesmsworks.co.uk\/developers\">SMS API documentation<\/a>\u00a0\u00a0<\/strong>The home for our complete set of SMS API documentation, containing code snippets in PHP and all other popular languages.<\/p>\n\n<p><strong><a href=\"https:\/\/thesmsworks.co.uk\/virtual-mobile-number\">Virtual mobile numbers<\/a>\u00a0<\/strong>Do you need to be able to receive replies to your outbound SMS? This article explains how it works and what&#8217;s involved.<\/p>\n\n<p><strong><a href=\"https:\/\/thesmsworks.co.uk\/SMS-API-integration-checklist\">14 things to check before your SMS API integration goes live<\/a>\u00a0<\/strong>Once you&#8217;ve set up your SMS service using PHP, here are some important things that you need to check before it goes live. (Obviously this only applies if you&#8217;ve used The SMS Works for your integration.)<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>How to send\u00a0an SMS\u00a0with PHP &#8211; a simple guide More and more businesses are using SMS as a way of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":80,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-858","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/posts\/858","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=858"}],"version-history":[{"count":0,"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/posts\/858\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/media\/80"}],"wp:attachment":[{"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thesmsworks.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}