Home

You are here

Code Samples

The following samples will help move through the process of requesting numbers and retrieving stats.

This code uses the python oauth library that can be found at:
https://github.com/simplegeo/python-oauth2
import oauth2 as oauth
import time, json

# Create your consumer with the proper key/secret.
consumer = oauth.Consumer(key="MY_CONSUMER_KEY",
    secret="MY_CONSUMER_SECRET")
   
client = oauth.Client(consumer)

headers = {};
headers['Accept'] = 'application/json';

url = "http://api.mob4hire.com/v1/sms/devices"
params = 'cc=CA&mnc=220&mcc=302&count=2'
resp, content = client.request(url, "GET", params, headers)
content = json.loads(content)
device = content['devices'][0]
print device

url = "http://api.mob4hire.com/v1/sms/stats"
params = 'min=%s' % device['min']
resp, content = client.request(url, "GET", params, headers)
stats = json.loads(content)['stat']
print stats

url = "http://api.mob4hire.com/v1/sms/user"
resp, content = client.request(url, "GET", '', headers)
user = json.loads(content)
print user

url = "http://api.mob4hire.com/v1/sms/message";
params = 'min=%s&message= %s Test Message Here&destination=DESTINATION' % (device['min'], user['smsCode'])
resp, content = client.request(url, "POST", params, headers)
message = json.loads(content)
print message
This sample uses the oauth ruby gem.
gem install oauth
require 'rubygems'
require 'oauth'

consumer_key = "MY_CONSUMER_KEY"
consumer_secret = "MY_CONSUMER_SECRET"
consumer = OAuth::Consumer.new(consumer_key, consumer_secret,
                               :site => "http://api.mob4hire.com",
                               :http_method => :get)

access_token = OAuth::AccessToken.new consumer

url = "/v1/sms/devices"
result = access_token.get(url, { 'Accept' => 'application/json' })
json = JSON.parse(result.body)
device = json['devices'][1]
p device

url = "/v1/sms/stats?min=%s" % device["min"]
result = access_token.get(url, { 'Accept' => 'application/json' })
stats = JSON.parse(result.body)
p stats

url = "/v1/sms/user"
result = access_token.get(url, { 'Accept' => 'application/json' })
user = JSON.parse(result.body)
p user

url = "/v1/sms/message";
result = access_token.post(url, { :min => device["min"], :message => " %s Test Message Here" % user["smsCode"], :destination => 'DESTINATION'}, { 'Accept' => 'application/json' } )
message = JSON.parse(result.body)
p message
This sample uses the java oauth-signpost library at
https://github.com/kaeppler/signpost
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;

import org.json.JSONObject;

public class mob {

   public static void main(String[] args) throws Exception {

    String url = "http://api.mob4hire.com/v1/sms/devices?cc=CA&mnc=220&mcc=302&count=2";
        JSONObject json = doRequest(url, "GET");
        JSONObject device = (JSONObject) json.getJSONArray("devices").get(0);
        System.out.println(device.toString());

        url = "http://api.mob4hire.com/v1/sms/stats?min="+device.getString("min");
        json = doRequest(url, "GET");
        System.out.println(json.toString());
       
        url = "http://api.mob4hire.com/v1/sms/user";
        JSONObject user = doRequest(url,"GET");
        System.out.println(user.toString());
       
        url = "http://api.mob4hire.com/v1/sms/message";
        String message = URLEncoder.encode(" "+user.getString("smsCode")+" Test Message Here", "UTF-8");
        String query = "?min="+device.getString("min")+"&message="+message+"&destination=DESTINATION";
        json = doRequest(url+query, "POST");
        System.out.println(json.toString());
    }
   
    public static JSONObject doRequest(String sUrl, String method) throws Exception
    {
        OAuthConsumer consumer = new DefaultOAuthConsumer(
        "MY_CONSUMER_KEY",
                "MY_CONSUMER_SECRET"
        );
       
    URL url = new URL(sUrl);
        HttpURLConnection request = (HttpURLConnection) url.openConnection();
        request.addRequestProperty("Accept", "application/json");
       request.setRequestMethod(method);
        consumer.sign(request);
        request.connect();
        JSONObject o = new JSONObject(getResponse(request));
        return o;
    }
   
    public static String getResponse(HttpURLConnection request) throws Exception
    {
   
    BufferedReader rd  = null;
        StringBuilder sb = null;
        String line = null;
       
        rd  = new BufferedReader(new InputStreamReader(request.getInputStream()));
        sb = new StringBuilder();
     
        while ((line = rd.readLine()) != null)
        {
            sb.append(line + '\n');
        }
     
        return sb.toString();

    }
}
This sample uses the php oauth library at
https://github.com/jrconlin/oauthsimple.git
<?php

include_once 'oauthsimple/php/OAuthSimple.php';

function doOauthRequest( $url, $params = array(), $action = 'GET' ) {
$key = 'MY_CONSUMER_KEY'; // this is your consumer key
$secret = 'MY_CONSUMER_SECRET'; // this is your secret key

$oauth = new OAuthSimple($key,$secret);

$oauth->setPath($url);
$oauth->setParameters($params);
$oauth->setAction($action);
$result = $oauth->sign();
var_dump( '-----', $url, $result['signed_url']);

$ch = curl_init();
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $result['signed_url']);
if( $action == 'POST' )
{
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
$r = curl_exec($ch);
curl_close($ch);
return json_decode( $r );
}


// make a request to get testable devices

$url = "http://api.mob4hire.com/v1/sms/devices";
$params = array(
'cc' => 'CA',
'mnc' => 220,
'mcc' => 302,
'count' => 1
);

$json = doOauthRequest( $url, $params );
$device = $json->devices[0];
print_r( $device );

// get the stats for a device

$url = "http://api.mob4hire.com/v1/sms/stats";
$params = array(
'min' => $device->min
);
$json = doOauthRequest( $url, $params );

print_r( $json );

// get the code we need to use to connect our test to our account

$url = "http://api.mob4hire.com/v1/sms/user";
$user = doOauthRequest( $url );

print_r( $user );

// send a test message

$url = "http://api.mob4hire.com/v1/sms/message";
$params = array(
'min' => $device->min,
'message' => ' '.$user->smsCode.' Test Message Here',
'destination' => 'DESTINATION'
);
$json = doOauthRequest( $url, $params, 'POST' );

print_r( $json );