API Binding: PHP

The API binding for PHP can be used to access TestRail's API from PHP. It provides the basic functionality to authenticate API requests, provides seamless JSON encoding/decoding and has generic support for read and write requests. Please see below for several examples on how to use the API binding.

The source code for the binding can be found on GitHub:

TestRail API binding for PHP (ZIP)
The GitHub repository with the API bindings as ZIP download

Setup & installation

Please download the ZIP from the above location and extract it to a directory of your choice. The binding for PHP can be located in the “php” sub-directory and is a single file (“testrail.php”). Please copy this file to a directory of your application or project where “require/include” can find it. You can then use the API binding as follows:

require 'testrail.php';
 
$client = new TestRailAPIClient('http://<server>/testrail/');
$client->set_user('..');
$client->set_password('..');
 
..

Please note: You should specify the same TestRail address for the API client you also use to access TestRail with your web browser (e.g., https://<your-name>.testrail.com/ or http://<server>/testrail/).

Example: GET request

GET requests are used by read-only API methods and can be used to read or query data from TestRail. For example, the following code shows how to get the properties of a test case using the “get_caseAPI method:

$client = new TestRailAPIClient('http://<server>/testrail/');
$client->set_user('..');
$client->set_password('..');
$case = $client->send_get('get_case/1');
var_dump($case);

The API binding provides a single method for GET requests named “send_get” which expects the name of the TestRail API method (here “get_case”) including any possible parameters (in this case the ID of the test case). The return value is a PHP key/value array and strings use the UTF-8 encoding.

An exception of type TestRailAPIException may be thrown in case of an error (connection issues, failed authentication, missing API arguments, etc.) and your code should be prepared to handle this.

Example: POST request

POST requests are used by write-based API methods and can be used to add or modify data. For example, the following code shows how to add a test result using the “add_result_for_caseAPI method:

..
$result = $client->send_post(
	'add_result_for_case/1/1',
	array(
		'status_id' => 1,
		'comment' => 'This test worked fine!' 
	)
);
 
var_dump($result);
..

The “send_post” method has the same properties as “send_get” except that it uses POST instead of GET for the API requests. Like “send_get”, “send_post” expects the name of the TestRail API method (here “add_result_for_case”) including any possible parameters (in this case the IDs of the test run and case, respectively). The return value is a PHP key/value array. POST arguments can be added as second parameter as key/value array (here the status ID and a comment). The expected encoding for strings is UTF-8.

An exception of type TestRailAPIException may be thrown in case of an error (connection issues, failed authentication, missing API arguments, etc.) and your code should be prepared to handle this.