Quote:
Originally Posted by Greg Marra
TBA currently offers an API client in PHP that can be found here.
...
Has anyone else written a port of the Client Library? What issues have you had porting it?
|
I noticed other threads with people having problems. PHP5 has this great new thing called
Exceptions. If there is an error, it will kill the execution of the script, unless the programmer has a method of catching and handling the exception.
The official PHP client is serverely lacking in error checking, for instance, if the API server returns a blank XML (XHTML, say) document, then no error will pop up.
I'll keep it simple, my edits are public domain. I have not tested this, but it should work.
Code:
function tba_send_request($method, $arguments=array()){
$api_key = ""; //replace with your API key
$api_url = "http://thebluealliance.net/tbatv/api.php"; //this should not change
// prepend the default array elements
$arguments = array('version'=>1, 'api_key'=>$api_key,'method'=>$method) + $arguments;
$file = $api_url.'?'.http_build_query($arguments);
$xml = simplexml_load_file($file);
// check for a broken connection, bad XML, etc
if($xml===false) throw new Exception("Bad XML returned getting file $file");
// check for errors thrown by the server
if (count($xml->error)){
throw new Exception("Error generated by API server: ".(string)$xml->error->text, (int)$xml->error->code);
}else{
return $xml;
}
}
While you could put these functions into a class, and have the API key be a property of the object, I would not recommend it (at least for PHP), there is no benefit to using object-oriented programming here (since the most objects you will ever need is one).