Public
Authored by Rohan Prabhu

PHP Signed API calls [client]

Example usage for the PHP client:

  1. Import the API client that is documented below in this snippet.
<?php

include 'mitter-api-client.php';
  1. Initialize your credentials. You can get this from your panel over at https://mitter.io/home. Select the application you want to create your credentials in, and go to Access Key > New Access Key.
const MITTER_ACCESS_KEY = '~~~';
const MITTER_ACCESS_SECRET = '~~~';
  1. Create a client by initializing it with the credentials:
$mitter_api_client = new MitterApiClient(MITTER_ACCESS_KEY, MITTER_ACCESS_SECRET);

As a sidenote, we implement a quick function to print out error bodies from the mitter.io service:

function print_mitter_error($response) {
   echo "Request Error\n";
   echo "----\n";
   echo "InstanceId: " . $response->instanceId . "\n";
   echo "ErrorCode : " . $response->errorCode  . "\n";
}

The examples below use this function for error handling.

  1. An example get call to get a list of users:
// Get users example
$users = $mitter_api_client->get('/v1/users');

if ($users->status_code < 300 && $users->status_code >= 200) {
    print_r($users->response);
} else {
    print_mitter_error($users->response);
}
  1. An example post call to create a new user:
// Create user example
$create_user = $mitter_api_client->post('/v1/users', json_encode(array(
    "userId" => "example-user-1",
    "screenName" => array(
        "screenName" => "example-user-1"
    )
)));

if ($create_user->status_code < 300 && $create_user->status_code >= 200) {
    print_r($create_user->response);
} else {
    if ($create_user->status_code == 409) {
        echo "User with id 'example-user-1' already exists";
    } else {
        print_mitter_error($create_user->response);
    }
}
Edited
mitter-api-client.php 4.04 KB
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment