Skip to content

Instantly share code, notes, and snippets.

@hypeJunction
Created June 9, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hypeJunction/54240bd44a87a34c5759 to your computer and use it in GitHub Desktop.
Save hypeJunction/54240bd44a87a34c5759 to your computer and use it in GitHub Desktop.
Testing Elgg Web Services Locally
<?php
define('WS_API_DOMAIN', 'http://xxxxxxxxxxxx.com/');
define('WS_TEST_API_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
define('WS_TEST_USERNAME', 'xxxxxxxxxxxxxxxxxxx');
define('WS_TEST_PASSWORD', 'xxxxxxxxxxxxxxxx');
function ws_test_build_url($method, $auth_token = null) {
$query_data = array(
'method' => $method,
'api_key' => WS_TEST_API_KEY,
'auth_token' => $auth_token,
);
$query = http_build_query(array_filter($query_data));
return WS_API_DOMAIN . 'services/api/rest/json/?' . $query;
}
function ws_test_post_api_call($url, $fields) {
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
function ws_test_get_api_call($url, $fields) {
$url = $url . "&" . http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
return json_decode($result, true);
}
function ws_test_get_token() {
$url = ws_test_build_url('auth.gettoken');
$fields = array(
'username' => WS_TEST_USERNAME,
'password' => WS_TEST_PASSWORD,
);
$result = ws_test_post_api_call($url, $fields);
return (!empty($result['result'])) ? $result['result'] : false;
}
$auth_token = ws_test_get_token();
// Get user messages
$url = ws_test_build_url('messages.inbox', $auth_token);
$response = ws_test_get_api_call($url, array(
'limit' => 20,
));
// Send a message on behalf of the user
$url = ws_test_build_url('messages.send', $auth_token);
$response = ws_test_post_api_call($url, array(
'subject' => 'Hello world',
'body' => 'Hello world',
'send_to' => 'some_username',
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment