Scoreoid’s Open API methods are RESTful (HTTP/HTTPS) requests which return XML or JSON responses. The Scoreoid Open API works with every coding language making it truly cross platform. We recommend using the built in Scoreoid console via the Scoreoid dashboard to learn and test our Open API methods.
API URL: https://api.scoreoid.com/v1/createScore
POST Parameters
api_key=> Your API Key [Required] game_id => Your Game ID [Required] response => String Value, "XML" or "JSON" [Required] username => String Value, if user does not exist it well be created [Required] score => Integer Value, [Required] platform => String Value, [Optional] unique_id => Integer Value, [Optional] difficulty => Integer Value (don't use 0 as it's the default value) [Optional] data => Custom Data Value, [Optional]
API Response Returns
success => success message, "The score has been saved" [String] upon failure you well receive an error message, example - "Please enter player username"
Example Response
Example response in JSON or XML format
JSON
XML
<success>The score has been created</success>
Code Snippets
The following are simple code snippets designed for basic examples, they do not follow best practice nor recommend guidelines. We have listed a number of the popular languages however Scoreoid supports every language that is able to use RESTful HTTP/HTTPS.
AS3
{
var url:String = "https://www.scoreoid.com/api/createScore";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
request.data = requestVars;
requestVars.api_key = "YOUR API KEY";
requestVars.game_id = "YOUR GAME ID";
requestVars.response ="XML"
requestVars.username ="Players Username"
requestVars.score ="Players score"
request.method = URLRequestMethod.POST;
var urlLoader:URLLoader = new URLLoader();
urlLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
urlLoader.load(request);
}
function loaderCompleteHandler(event:Event):void
{
trace("responseVars: " + event.target.data);
}
JavaScript/HTML5
For JavaScript and HTML5 web based games please use Scoroeid’s auto generated proxy check out the full wiki article for instructions.
For mobile or desktop based HTML5/JavaScript games use the standard XMLHttpRequest object (or any library that implements it). We recommend using jQuery Post or a normal Ajax call using pure Javascript does the job very quickly and avoids the overhead for loading the jQuery file. Do note we recommend that you test this thru the targeted platform debugger, if you run into any issues please fill out a support ticket.
jQuery post code snippet
function(data) {
alert("Data Loaded: " + data);
console.log("Data Loaded: " + data);
});
Make sure to inculde the Javascript library in your files.
C#
string post_data = "api_key=Your API Key&game_id=Your Game ID&response=XML&username= Players username&score= Players score";
// URL
string uri = "https://www.scoreoid.com/api/createScore";
// Create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(uri); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
// Turn our request string into a byte stream
byte[] postBytes = Encoding.ASCII.GetBytes(str);
// This is important - make sure you specify type this way
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
// Now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
// Grab te response and print it out to the console along with the status code
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine(new StreamReader(response.GetResponseStream()).ReadToEnd());
Console.WriteLine(response.StatusCode);
Lua
The following example uses LuaSocket libraries, taking from Corona’s network API’s
if ( event.isError ) then
print( "Network error!")
else
print ( "RESPONSE: " .. event.response )
end
end
postData = "api_key=Your API Key&game_id=Your Game ID&username=Player Username&score=Player Score&response=xml"
local params = {}
params.body = postData
network.request("https://www.scoreoid.com/api/createScore", "POST", networkListener, params)
Objective C
The following example is using Cocoa
[self urlEncodeValue:Your API Key]
[self urlEncodeValue:Your game ID]
[self urlEncodeValue:XML]
[self urlEncodeValue:The Player Username]
[self urlEncodeValue:The Player Score]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"https://www.scoreoid.com/api/createScore"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
- (NSString *)urlEncodeValue:(NSString *)str
{
NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
return [result autorelease];
}
Java
The following example is for Android, tested on Android 2.1
HttpClient httpclient;
// List with parameters and their values
List<NameValuePair> nameValuePairs;
String serverResponsePhrase;
int serverStatusCode;
String bytesSent;
httppost = new HttpPost(https://www.scoreoid.com/api/createScore);
httpclient = new DefaultHttpClient();
nameValuePairs = new ArrayList<NameValuePair>(5);
// Adding parameters to send to the HTTP server.
nameValuePairs.add(new BasicNameValuePair(api_key, Your API Key));
nameValuePairs.add(new BasicNameValuePair(game_id, Your game));
nameValuePairs.add(new BasicNameValuePair(response, XML)
nameValuePairs.add(new BasicNameValuePair(username, Players username)
nameValuePairs.add(new BasicNameValuePair(score, Players score));
// Send POST message with given parameters to the HTTP server.
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1)
{
baf.append((byte)current);
}
bytesSent = new String(baf.toByteArray());
// Response from the server
serverResponsePhrase = response.getStatusLine().getReasonPhrase();
serverStatusCode = response.getStatusLine().getStatusCode();
}
catch (Exception e) {
// Exception handling
}
C++
The following example uses libcurl C++ library
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.scoreoid.com/api/createScore");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
Unity
JavaScript example using Unity WWW class.
/* API method */
var url = "https://www.scoreoid.com/api/createScore";
/* Unity WWW Class used for HTTP Request */
var form = new WWWForm();
form.AddField( "api_key", "YOUR API KEY" );
form.AddField( "game_id", "YOUR GAME ID");
form.AddField( "response", "xml");
form.AddField( "username", "Player username");
form.AddField( "score", "Player score");
var www = new WWW( url, form );
/* Wait for request to complete */
yield www;
/* Check for errors */
if (www.error == null)
{
/* Request completed! */
Debug.Log("request completed!: " + www.data);
} else {
/* Something wrong! */
Debug.Log("WWW Error: "+ www.error);
}
}
C# example using Unity WWW class.
void Start () {
/* API method */
string url = "https://www.scoreoid.com/api/createScore";
/* Unity WWW Class used for HTTP Request */
WWWForm form = new WWWForm();
form.AddField( "api_key", "YOUR API KEY" );
form.AddField( "game_id", "YOUR GAME ID");
form.AddField( "response", "xml");
form.AddField( "username", "Player username");
form.AddField( "score", "Player score");
WWW www = new WWW(url, form);
StartCoroutine(WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www)
{
yield return www;
/* Check for errors */
if (www.error == null)
{
Debug.Log("WWW Ok!: " + www.text);
} else {
Debug.Log("WWW Error: "+ www.error);
}
}
MORE
The following example uses Python 3.0 http.client library (low-level library that implements RFC 2616).
>>> params = urllib.parse.urlencode({'api_key': Your API Key, 'game_id': Your game, 'response': XML, 'username': Players username, 'score' : Players score})
>>> conn = http.client.HTTPConnection("https://www.scoreoid.com/api/createScore:80")
>>> conn.request("POST", "/cgi-bin/query", params)
>>> response = conn.getresponse()
>>> print(response.status, response.reason)
200 OK
>>> data = response.read()
>>> conn.close()
The following example uses PHP.
$post_data = array(
'api_key' => 'Your API Key',
'game_id' => 'Your Game ID',
'response' => 'XML'
'username' => 'Player username'
'score' => 'Player score'
);
// Send a request to www.scoreoid.com/api/createScore
$result = post_request('https://www.scoreoid.com/api/createScore', $post_data);
if ($result['status'] == 'ok'){
// Print headers
echo $result['header'];
echo '<hr />';
// print the result of the whole request:
echo $result['content'];
}
else {
echo 'A error occured: ' . $result['error'];
}