Whispir Forums

Getting Started

RSS Feed

Sending SMS with PHP

  1. This example code snippet should help PHP developers get started with using the Whispir API to send messages.

            Author:         Jordan Walsh
            Date:           10-11-2012
            Description:    Simple script to connect to the Whispir API using your API Key, 
                            Username and Password and send an SMS message.
        //Whispir API URL - replace the query parameter with your API key
        $WHISPIR_API = 'http://api.whispir.com/messages?apikey=<your API Key>';
        $WHISPIR_USERNAME = '<your Whispir Username';
        $WHISPIR_PASSWORD = '<your Whispir Password>';
            This variable contains the message recipients and content.
            Recipient information example: 
            "to" => "0408099521" 
        $data = array("to" => "<recipient phone number>", 
                      "subject" => "First Test Message.", 
                      "body" => "Hello World!");    
        //Encode the data into JSON
        $data_string = json_encode($data);                                                                                   
        //Initialise the WHISPIR_API object
        $curl = curl_init($WHISPIR_API); 
        //This is the authorization header 
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        //Other Headers to support the message
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);     
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(                                                                          
            'Content-Type: application/vnd.whispir.message-v1+json',   //This says we are sending JSON      
            'Accept: application/vnd.whispir.message-v1+json')         //This says we want JSON returned to us                                                                             
        //You need this to ensure that the response is written to a variable.
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);                    
        //Execute the request
        //Pull the response from the $curl request
        $headers = curl_getinfo($curl);
        //This will print the whole headers array
        //This will print specific items from the headers array
        echo "Response Code: ".$headers['http_code'];
        //Close the HTTP request.

    Message edited by Jordan Walsh 5 years ago

[ Page 1 of 1 ]