Difference between revisions of "API:Core"

From TheHostingTool Wiki
Jump to: navigation, search
(Created page with '==Setting Up an Authentication Key== ==Calling a Function== ===Method One=== ===Method Two===')
 
m (Added the ApiNotReady template.)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Template:ApiNotReady}}
 +
 +
 
==Setting Up an Authentication Key==
 
==Setting Up an Authentication Key==
 +
Use the Gallery for information:
 +
<gallery>
 +
Image:Admin1.png|Step 1: Log into the admin panel
 +
Image:API1.png|Step 2: Go to the ''XML-API'' page and click on ''Generate New Key''
 +
Image:API2.png|Step 3: Click ''Generate''
 +
Image:API3.png|Step 4: The key is generated. Go back to the ''XML-API'' page.
 +
Image:API1.png|Step 5: Click ''Get API Key''
 +
Image:API4.png|Step 6: Get your key.
 +
</gallery>
 +
 +
 
==Calling a Function==
 
==Calling a Function==
===Method One===
+
The API uses a REQUEST system -- this means you can submit your variables via POST or GET. Requests are made to xml-api/index.php. The xml-api directory will be found in the THT directory. For these examples, we will be using '''PHP''' as our language for coding a script to get a list of accounts. For simplicity, we will be using a GET request.
===Method Two===
+
 
 +
You will need to create a hash of the function.
 +
===If using a parameter===
 +
To call a function with a parameter using the XML-API, you would make a request to:
 +
<pre>http://mydomain.tld/THT_DIR/xml-api/function/params/auth_hash</pre>
 +
To create your auth hash, you need to generate a sha512 hash and two sha1 hashes. The two sha1 hashes are generated, joined together, and are then used to create a sha512 hash. Your hashing code should look like this:
 +
<pre>$hash = hash('sha512', sha1('function_name|parameters') . sha1($my_tht_api_key));</pre>
 +
 
 +
===If not using a parameter===
 +
So now let's get a list of users. We'll assume you already have your API key. Now, the hashing and access method change. To generate a hash:
 +
<pre>$hash = hash('sha512', sha1('listaccts') . sha1($my_tht_api_key));</pre>
 +
And the request URL would be:
 +
<pre>http://mydomain.tld/THT_DIR/xml-api/listaccts/auth_hash</pre>
 +
 
 +
Now, the function to list accounts would be [[API:listaccts]], which we'll get into on that function's wiki page. There are no parameters. We'll make a cURL GET request to process a list of users, which will be returned in [[KB:Dictionary#X|'''XML.''']]
 +
 
 +
<pre>
 +
<?php
 +
$ch = curl_init();
 +
curl_setopt($ch, CURLOPT_URL, 'http://mydomain.tld/THT_DIR/xml-api/listaccts/auth_hash');
 +
$val = curl_exec($ch);
 +
curl_close($ch);
 +
$xml = new SimpleXMLElement($val);
 +
unset($val);
 +
echo $xml->acct[0]->domain[0];
 +
?>
 +
</pre>
 +
 
 +
If you have at least one client, this will output the domain of the first account in the array.

Latest revision as of 21:32, 29 March 2010



Warning.png

Warning!
TheHostingTool's Application Programming Interface (API) is outdated and no longer included as of THT 1.2.2. It is considered a security warning, and is not supported, so use at your own risk.


Setting Up an Authentication Key

Use the Gallery for information:


Calling a Function

The API uses a REQUEST system -- this means you can submit your variables via POST or GET. Requests are made to xml-api/index.php. The xml-api directory will be found in the THT directory. For these examples, we will be using PHP as our language for coding a script to get a list of accounts. For simplicity, we will be using a GET request.

You will need to create a hash of the function.

If using a parameter

To call a function with a parameter using the XML-API, you would make a request to:

http://mydomain.tld/THT_DIR/xml-api/function/params/auth_hash

To create your auth hash, you need to generate a sha512 hash and two sha1 hashes. The two sha1 hashes are generated, joined together, and are then used to create a sha512 hash. Your hashing code should look like this:

$hash = hash('sha512', sha1('function_name|parameters') . sha1($my_tht_api_key));

If not using a parameter

So now let's get a list of users. We'll assume you already have your API key. Now, the hashing and access method change. To generate a hash:

$hash = hash('sha512', sha1('listaccts') . sha1($my_tht_api_key));

And the request URL would be:

http://mydomain.tld/THT_DIR/xml-api/listaccts/auth_hash

Now, the function to list accounts would be API:listaccts, which we'll get into on that function's wiki page. There are no parameters. We'll make a cURL GET request to process a list of users, which will be returned in XML.

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://mydomain.tld/THT_DIR/xml-api/listaccts/auth_hash');
$val = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($val);
unset($val);
echo $xml->acct[0]->domain[0];
?>

If you have at least one client, this will output the domain of the first account in the array.