Now Hiring: Are you a driven and motivated 1st Line IT Support Engineer?

PHP Integration

PHP Integration #

Include the following code in your PHP file to start sending messages. #

  • </pre>
    <pre class="prettyprint prettyprinted"><span class="pln">define</span><span class="pun">(</span><span class="str">"SERVER"</span><span class="pun">,</span> <span class="str">"https://androidapi.net/exe"</span><span class="pun">);</span><span class="pln">
    define</span><span class="pun">(</span><span class="str">"API_KEY"</span><span class="pun">,</span> <span class="str">"b7d52620813455b38a3a0d5bc801dfcc9946f34f"</span><span class="pun">);</span><span class="pln">
    
    define</span><span class="pun">(</span><span class="str">"USE_SPECIFIED"</span><span class="pun">,</span> <span class="lit">0</span><span class="pun">);</span><span class="pln">
    define</span><span class="pun">(</span><span class="str">"USE_ALL_DEVICES"</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">);</span><span class="pln">
    define</span><span class="pun">(</span><span class="str">"USE_ALL_SIMS"</span><span class="pun">,</span> <span class="lit">2</span><span class="pun">);</span>
    
    <span class="com">/**
     * @param string     $number      The mobile number where you want to send message.
     * @param string     $message     The message you want to send.
     * @param int|string $device      The ID of a device you want to use to send this message.
     * @param int        $schedule    Set it to timestamp when you want to send this message.
     * @param bool       $isMMS       Set it to true if you want to send MMS message instead of SMS.
     * @param string     $attachments Comma separated list of image links you want to attach to the message. Only works for MMS messages.
     * @param bool       $prioritize  Set it to true if you want to prioritize this message.
     *
     * @return array     Returns The array containing information about the message.
     * @throws Exception If there is an error while sending a message.
     */</span>
    <span class="kwd">function</span><span class="pln"> sendSingleMessage</span><span class="pun">(</span><span class="pln">$number</span><span class="pun">,</span><span class="pln"> $message</span><span class="pun">,</span><span class="pln"> $device </span><span class="pun">=</span> <span class="lit">0</span><span class="pun">,</span><span class="pln"> $schedule </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $isMMS </span><span class="pun">=</span> <span class="kwd">false</span><span class="pun">,</span><span class="pln"> $attachments </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $prioritize </span><span class="pun">=</span> <span class="kwd">false</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/send.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span><span class="pln"> array</span><span class="pun">(</span>
            <span class="str">'number'</span> <span class="pun">=></span><span class="pln"> $number</span><span class="pun">,</span>
            <span class="str">'message'</span> <span class="pun">=></span><span class="pln"> $message</span><span class="pun">,</span>
            <span class="str">'schedule'</span> <span class="pun">=></span><span class="pln"> $schedule</span><span class="pun">,</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'devices'</span> <span class="pun">=></span><span class="pln"> $device</span><span class="pun">,</span>
            <span class="str">'type'</span> <span class="pun">=></span><span class="pln"> $isMMS </span><span class="pun">?</span> <span class="str">"mms"</span> <span class="pun">:</span> <span class="str">"sms"</span><span class="pun">,</span>
            <span class="str">'attachments'</span> <span class="pun">=></span><span class="pln"> $attachments</span><span class="pun">,</span>
            <span class="str">'prioritize'</span> <span class="pun">=></span><span class="pln"> $prioritize </span><span class="pun">?</span> <span class="lit">1</span> <span class="pun">:</span> <span class="lit">0</span>
        <span class="pun">);</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param array  $messages        The array containing numbers and messages.
     * @param int    $option          Set this to USE_SPECIFIED if you want to use devices and SIMs specified in devices argument.
     *                                Set this to USE_ALL_DEVICES if you want to use all available devices and their default SIM to send messages.
     *                                Set this to USE_ALL_SIMS if you want to use all available devices and all their SIMs to send messages.
     * @param array  $devices         The array of ID of devices you want to use to send these messages.
     * @param int    $schedule        Set it to timestamp when you want to send these messages.
     * @param bool   $useRandomDevice Set it to true if you want to send messages using only one random device from selected devices.
     *
     * @return array     Returns The array containing messages.
     *                   For example :-
     *                   [
     *                      0 => [
     *                              "ID" => "1",
     *                              "number" => "+11234567890",
     *                              "message" => "This is a test message.",
     *                              "deviceID" => "1",
     *                              "simSlot" => "0",
     *                              "userID" => "1",
     *                              "status" => "Pending",
     *                              "type" => "sms",
     *                              "attachments" => null,
     *                              "sentDate" => "2018-10-20T00:00:00+02:00",
     *                              "deliveredDate" => null
     *                              "groupID" => ")V5LxqyBMEbQrl9*J$5bb4c03e8a07b7.62193871"
     *                           ]
     *                   ]
     * @throws Exception If there is an error while sending messages.
     */</span>
    <span class="kwd">function</span><span class="pln"> sendMessages</span><span class="pun">(</span><span class="pln">$messages</span><span class="pun">,</span><span class="pln"> $option </span><span class="pun">=</span><span class="pln"> USE_SPECIFIED</span><span class="pun">,</span><span class="pln"> $devices </span><span class="pun">=</span> <span class="pun">[],</span><span class="pln"> $schedule </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $useRandomDevice </span><span class="pun">=</span> <span class="kwd">false</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/send.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'messages'</span> <span class="pun">=></span><span class="pln"> json_encode</span><span class="pun">(</span><span class="pln">$messages</span><span class="pun">),</span>
            <span class="str">'schedule'</span> <span class="pun">=></span><span class="pln"> $schedule</span><span class="pun">,</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'devices'</span> <span class="pun">=></span><span class="pln"> json_encode</span><span class="pun">(</span><span class="pln">$devices</span><span class="pun">),</span>
            <span class="str">'option'</span> <span class="pun">=></span><span class="pln"> $option</span><span class="pun">,</span>
            <span class="str">'useRandomDevice'</span> <span class="pun">=></span><span class="pln"> $useRandomDevice
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param int    $listID      The ID of the contacts list where you want to send this message.
     * @param string $message     The message you want to send.
     * @param int    $option      Set this to USE_SPECIFIED if you want to use devices and SIMs specified in devices argument.
     *                            Set this to USE_ALL_DEVICES if you want to use all available devices and their default SIM to send messages.
     *                            Set this to USE_ALL_SIMS if you want to use all available devices and all their SIMs to send messages.
     * @param array  $devices     The array of ID of devices you want to use to send the message.
     * @param int    $schedule    Set it to timestamp when you want to send this message.
     * @param bool   $isMMS       Set it to true if you want to send MMS message instead of SMS.
     * @param string $attachments Comma separated list of image links you want to attach to the message. Only works for MMS messages.
     *
     * @return array     Returns The array containing messages.
     * @throws Exception If there is an error while sending messages.
     */</span>
    <span class="kwd">function</span><span class="pln"> sendMessageToContactsList</span><span class="pun">(</span><span class="pln">$listID</span><span class="pun">,</span><span class="pln"> $message</span><span class="pun">,</span><span class="pln"> $option </span><span class="pun">=</span><span class="pln"> USE_SPECIFIED</span><span class="pun">,</span><span class="pln"> $devices </span><span class="pun">=</span> <span class="pun">[],</span><span class="pln"> $schedule </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $isMMS </span><span class="pun">=</span> <span class="kwd">false</span><span class="pun">,</span><span class="pln"> $attachments </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/send.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'listID'</span> <span class="pun">=></span><span class="pln"> $listID</span><span class="pun">,</span>
            <span class="str">'message'</span> <span class="pun">=></span><span class="pln"> $message</span><span class="pun">,</span>
            <span class="str">'schedule'</span> <span class="pun">=></span><span class="pln"> $schedule</span><span class="pun">,</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'devices'</span> <span class="pun">=></span><span class="pln"> json_encode</span><span class="pun">(</span><span class="pln">$devices</span><span class="pun">),</span>
            <span class="str">'option'</span> <span class="pun">=></span><span class="pln"> $option</span><span class="pun">,</span>
            <span class="str">'type'</span> <span class="pun">=></span><span class="pln"> $isMMS </span><span class="pun">?</span> <span class="str">"mms"</span> <span class="pun">:</span> <span class="str">"sms"</span><span class="pun">,</span>
            <span class="str">'attachments'</span> <span class="pun">=></span><span class="pln"> $attachments
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param int $id The ID of a message you want to retrieve.
     *
     * @return array     The array containing a message.
     * @throws Exception If there is an error while getting a message.
     */</span>
    <span class="kwd">function</span><span class="pln"> getMessageByID</span><span class="pun">(</span><span class="pln">$id</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/read-messages.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'id'</span> <span class="pun">=></span><span class="pln"> $id
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param string $groupID The group ID of messages you want to retrieve.
     *
     * @return array     The array containing messages.
     * @throws Exception If there is an error while getting messages.
     */</span>
    <span class="kwd">function</span><span class="pln"> getMessagesByGroupID</span><span class="pun">(</span><span class="pln">$groupID</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/read-messages.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'groupId'</span> <span class="pun">=></span><span class="pln"> $groupID
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param string $status         The status of messages you want to retrieve.
     * @param int    $deviceID       The deviceID of the device which messages you want to retrieve.
     * @param int    $simSlot        Sim slot of the device which messages you want to retrieve. Similar to array index. 1st slot is 0 and 2nd is 1.
     * @param int    $startTimestamp Search for messages sent or received after this time.
     * @param int    $endTimestamp   Search for messages sent or received before this time.
     *
     * @return array     The array containing messages.
     * @throws Exception If there is an error while getting messages.
     */</span>
    <span class="kwd">function</span><span class="pln"> getMessagesByStatus</span><span class="pun">(</span><span class="pln">$status</span><span class="pun">,</span><span class="pln"> $deviceID </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $simSlot </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $startTimestamp </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $endTimestamp </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/read-messages.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'status'</span> <span class="pun">=></span><span class="pln"> $status</span><span class="pun">,</span>
            <span class="str">'deviceID'</span> <span class="pun">=></span><span class="pln"> $deviceID</span><span class="pun">,</span>
            <span class="str">'simSlot'</span> <span class="pun">=></span><span class="pln"> $simSlot</span><span class="pun">,</span>
            <span class="str">'startTimestamp'</span> <span class="pun">=></span><span class="pln"> $startTimestamp</span><span class="pun">,</span>
            <span class="str">'endTimestamp'</span> <span class="pun">=></span><span class="pln"> $endTimestamp
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param int $id The ID of a message you want to resend.
     *
     * @return array     The array containing a message.
     * @throws Exception If there is an error while resending a message.
     */</span>
    <span class="kwd">function</span><span class="pln"> resendMessageByID</span><span class="pun">(</span><span class="pln">$id</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/resend.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'id'</span> <span class="pun">=></span><span class="pln"> $id
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param string $groupID The group ID of messages you want to resend.
     * @param string $status  The status of messages you want to resend.
     *
     * @return array     The array containing messages.
     * @throws Exception If there is an error while resending messages.
     */</span>
    <span class="kwd">function</span><span class="pln"> resendMessagesByGroupID</span><span class="pun">(</span><span class="pln">$groupID</span><span class="pun">,</span><span class="pln"> $status </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/resend.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'groupId'</span> <span class="pun">=></span><span class="pln"> $groupID</span><span class="pun">,</span>
            <span class="str">'status'</span> <span class="pun">=></span><span class="pln"> $status
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param string $status         The status of messages you want to resend.
     * @param int    $deviceID       The deviceID of the device which messages you want to resend.
     * @param int    $simSlot        Sim slot of the device which messages you want to resend. Similar to array index. 1st slot is 0 and 2nd is 1.
     * @param int    $startTimestamp Resend messages sent or received after this time.
     * @param int    $endTimestamp   Resend messages sent or received before this time.
     *
     * @return array     The array containing messages.
     * @throws Exception If there is an error while resending messages.
     */</span>
    <span class="kwd">function</span><span class="pln"> resendMessagesByStatus</span><span class="pun">(</span><span class="pln">$status</span><span class="pun">,</span><span class="pln"> $deviceID </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $simSlot </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $startTimestamp </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $endTimestamp </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/resend.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'status'</span> <span class="pun">=></span><span class="pln"> $status</span><span class="pun">,</span>
            <span class="str">'deviceID'</span> <span class="pun">=></span><span class="pln"> $deviceID</span><span class="pun">,</span>
            <span class="str">'simSlot'</span> <span class="pun">=></span><span class="pln"> $simSlot</span><span class="pun">,</span>
            <span class="str">'startTimestamp'</span> <span class="pun">=></span><span class="pln"> $startTimestamp</span><span class="pun">,</span>
            <span class="str">'endTimestamp'</span> <span class="pun">=></span><span class="pln"> $endTimestamp
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"messages"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param int    $listID      The ID of the contacts list where you want to add this contact.
     * @param string $number      The mobile number of the contact.
     * @param string $name        The name of the contact.
     * @param bool   $resubscribe Set it to true if you want to resubscribe this contact if it already exists.
     *
     * @return array     The array containing a newly added contact.
     * @throws Exception If there is an error while adding a new contact.
     */</span>
    <span class="kwd">function</span><span class="pln"> addContact</span><span class="pun">(</span><span class="pln">$listID</span><span class="pun">,</span><span class="pln"> $number</span><span class="pun">,</span><span class="pln"> $name </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $resubscribe </span><span class="pun">=</span> <span class="kwd">false</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/manage-contacts.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'listID'</span> <span class="pun">=></span><span class="pln"> $listID</span><span class="pun">,</span>
            <span class="str">'number'</span> <span class="pun">=></span><span class="pln"> $number</span><span class="pun">,</span>
            <span class="str">'name'</span> <span class="pun">=></span><span class="pln"> $name</span><span class="pun">,</span>
            <span class="str">'resubscribe'</span> <span class="pun">=></span><span class="pln"> $resubscribe
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"contact"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param int    $listID The ID of the contacts list from which you want to unsubscribe this contact.
     * @param string $number The mobile number of the contact.
     *
     * @return array     The array containing the unsubscribed contact.
     * @throws Exception If there is an error while setting subscription to false.
     */</span>
    <span class="kwd">function</span><span class="pln"> unsubscribeContact</span><span class="pun">(</span><span class="pln">$listID</span><span class="pun">,</span><span class="pln"> $number</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/manage-contacts.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'listID'</span> <span class="pun">=></span><span class="pln"> $listID</span><span class="pun">,</span>
            <span class="str">'number'</span> <span class="pun">=></span><span class="pln"> $number</span><span class="pun">,</span>
            <span class="str">'unsubscribe'</span> <span class="pun">=></span> <span class="kwd">true</span>
        <span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"contact"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @return string    The amount of message credits left.
     * @throws Exception If there is an error while getting message credits.
     */</span>
    <span class="kwd">function</span><span class="pln"> getBalance</span><span class="pun">()</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/send.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY
        </span><span class="pun">];</span><span class="pln">
        $credits </span><span class="pun">=</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"credits"</span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> is_null</span><span class="pun">(</span><span class="pln">$credits</span><span class="pun">)</span> <span class="pun">?</span> <span class="str">"Unlimited"</span> <span class="pun">:</span><span class="pln"> $credits</span><span class="pun">;</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param string $request   USSD request you want to execute. e.g. *150#
     * @param int $device       The ID of a device you want to use to send this message.
     * @param int|null $simSlot Sim you want to use for this USSD request. Similar to array index. 1st slot is 0 and 2nd is 1.
     *
     * @return array     The array containing details about USSD request that was sent.
     * @throws Exception If there is an error while sending a USSD request.
     */</span>
    <span class="kwd">function</span><span class="pln"> sendUssdRequest</span><span class="pun">(</span><span class="pln">$request</span><span class="pun">,</span><span class="pln"> $device</span><span class="pun">,</span><span class="pln"> $simSlot </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/send-ussd-request.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'request'</span> <span class="pun">=></span><span class="pln"> $request</span><span class="pun">,</span>
            <span class="str">'device'</span> <span class="pun">=></span><span class="pln"> $device</span><span class="pun">,</span>
            <span class="str">'sim'</span> <span class="pun">=></span><span class="pln"> $simSlot
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"request"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param int $id The ID of a USSD request you want to retrieve.
     *
     * @return array     The array containing details about USSD request you requested.
     * @throws Exception If there is an error while getting a USSD request.
     */</span>
    <span class="kwd">function</span><span class="pln"> getUssdRequestByID</span><span class="pun">(</span><span class="pln">$id</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/read-ussd-requests.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'id'</span> <span class="pun">=></span><span class="pln"> $id
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"requests"</span><span class="pun">][</span><span class="lit">0</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="com">/**
     * @param string   $request        The request text you want to look for.
     * @param int      $deviceID       The deviceID of the device which USSD requests you want to retrieve.
     * @param int      $simSlot        Sim slot of the device which USSD requests you want to retrieve. Similar to array index. 1st slot is 0 and 2nd is 1.
     * @param int|null $startTimestamp Search for USSD requests sent after this time.
     * @param int|null $endTimestamp   Search for USSD requests sent before this time.
     *
     * @return array     The array containing USSD requests.
     * @throws Exception If there is an error while getting USSD requests.
     */</span>
    <span class="kwd">function</span><span class="pln"> getUssdRequests</span><span class="pun">(</span><span class="pln">$request</span><span class="pun">,</span><span class="pln"> $deviceID </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $simSlot </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $startTimestamp </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">,</span><span class="pln"> $endTimestamp </span><span class="pun">=</span> <span class="kwd">null</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $url </span><span class="pun">=</span><span class="pln"> SERVER </span><span class="pun">.</span> <span class="str">"/services/read-ussd-requests.php"</span><span class="pun">;</span><span class="pln">
        $postData </span><span class="pun">=</span> <span class="pun">[</span>
            <span class="str">'key'</span> <span class="pun">=></span><span class="pln"> API_KEY</span><span class="pun">,</span>
            <span class="str">'request'</span> <span class="pun">=></span><span class="pln"> $request</span><span class="pun">,</span>
            <span class="str">'deviceID'</span> <span class="pun">=></span><span class="pln"> $deviceID</span><span class="pun">,</span>
            <span class="str">'simSlot'</span> <span class="pun">=></span><span class="pln"> $simSlot</span><span class="pun">,</span>
            <span class="str">'startTimestamp'</span> <span class="pun">=></span><span class="pln"> $startTimestamp</span><span class="pun">,</span>
            <span class="str">'endTimestamp'</span> <span class="pun">=></span><span class="pln"> $endTimestamp
        </span><span class="pun">];</span>
        <span class="kwd">return</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)[</span><span class="str">"requests"</span><span class="pun">];</span>
    <span class="pun">}</span>
    
    <span class="kwd">function</span><span class="pln"> sendRequest</span><span class="pun">(</span><span class="pln">$url</span><span class="pun">,</span><span class="pln"> $postData</span><span class="pun">)</span>
    <span class="pun">{</span><span class="pln">
        $ch </span><span class="pun">=</span><span class="pln"> curl_init</span><span class="pun">();</span><span class="pln">
        curl_setopt</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLOPT_URL</span><span class="pun">,</span><span class="pln"> $url</span><span class="pun">);</span><span class="pln">
        curl_setopt</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLOPT_POST</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span><span class="pln">
        curl_setopt</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLOPT_RETURNTRANSFER</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span><span class="pln">
        curl_setopt</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLOPT_POSTFIELDS</span><span class="pun">,</span><span class="pln"> http_build_query</span><span class="pun">(</span><span class="pln">$postData</span><span class="pun">));</span><span class="pln">
        $response </span><span class="pun">=</span><span class="pln"> curl_exec</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">);</span><span class="pln">
        $httpCode </span><span class="pun">=</span><span class="pln"> curl_getinfo</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">,</span><span class="pln"> CURLINFO_HTTP_CODE</span><span class="pun">);</span>
        <span class="kwd">if</span> <span class="pun">(</span><span class="pln">curl_errno</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">))</span> <span class="pun">{</span>
            <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="pln">curl_error</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">));</span>
        <span class="pun">}</span><span class="pln">
        curl_close</span><span class="pun">(</span><span class="pln">$ch</span><span class="pun">);</span>
        <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$httpCode </span><span class="pun">==</span> <span class="lit">200</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
            $json </span><span class="pun">=</span><span class="pln"> json_decode</span><span class="pun">(</span><span class="pln">$response</span><span class="pun">,</span> <span class="kwd">true</span><span class="pun">);</span>
            <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$json </span><span class="pun">==</span> <span class="kwd">false</span><span class="pun">)</span> <span class="pun">{</span>
                <span class="kwd">if</span> <span class="pun">(</span><span class="pln">empty</span><span class="pun">(</span><span class="pln">$response</span><span class="pun">))</span> <span class="pun">{</span>
                    <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="str">"Missing data in request. Please provide all the required information to send messages."</span><span class="pun">);</span>
                <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
                    <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="pln">$response</span><span class="pun">);</span>
                <span class="pun">}</span>
            <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
                <span class="kwd">if</span> <span class="pun">(</span><span class="pln">$json</span><span class="pun">[</span><span class="str">"success"</span><span class="pun">])</span> <span class="pun">{</span>
                    <span class="kwd">return</span><span class="pln"> $json</span><span class="pun">[</span><span class="str">"data"</span><span class="pun">];</span>
                <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
                    <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="pln">$json</span><span class="pun">[</span><span class="str">"error"</span><span class="pun">][</span><span class="str">"message"</span><span class="pun">]);</span>
                <span class="pun">}</span>
            <span class="pun">}</span>
        <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
            <span class="kwd">throw</span> <span class="kwd">new</span> <span class="typ">Exception</span><span class="pun">(</span><span class="str">"HTTP Error Code : {$httpCode}"</span><span class="pun">);</span>
        <span class="pun">}</span>
    <span class="pun">}</span></pre>
    <pre>

Leave your thought here

Your email address will not be published. Required fields are marked *