0
Answered

API & PHP help

Erik Madison 9 years ago updated 9 years ago 5
Reading the API, I am embarrassingly lost. I could really use some stub code to get me started...
Currently, from within my mobile app, I allow my users to jot down noticed bugs and I send it off to a php page that just makes a list for me. I'd like to have my php instead send these to Lean Testing and add them to my project. I know how/where to get the various info I need, but I'm confused as to what format exactly I'm using to do the final send.
Under review
Hi Eric is this for an iOS or Android app?
Android, specifically a Unity app. However, I'm not concerned with sending from my app. Sending to my PHP is working great, but now, rather than dumping that to a flat file, I'd like to forward the info to my bug project.
Hi Erik,

Any POST and PUT request to our API must be JSON formatted.

Here's an example of how to create a bug with PHP and the popular lib Guzzle:

Note: remember to replace the <PROJECT ID> and <OAUTH TOKEN> placeholders.

<?php
// Include Guzzle. If using Composer:
// require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Post\PostBody;
use GuzzleHttp\Stream\StreamInterface;
use GuzzleHttp\Exception\RequestException;
function send_request() {
    // Create (POST https://api.leantesting.com/v1/projects/<PROJECT ID>/bugs)
    $client = new Client();
    $request = $client->createRequest('POST', 'https://api.leantesting.com/v1/projects/<PROJECT ID>/bugs');
    $postBody = $request->getBody();
    try {
        $request->addHeaders([
            'Authorization'=>'Bearer <OAUTH TOKEN>',
        ]);
        $body = [
            "description" => "Hello from the API :smile:",
            "severity_id" => 3,
            "steps" => [
                "Step 1",
                "Step 2",
                "Step 3"
            ],
            "project_version" => "1.0.1",
            "title" => "A bug reported from the SDK",
            "platform" => [
                "os" => "iOs",
                "os_version" => "8.1",
            ],
            "type_id" => "1",
            "reproducibility_id" => 1,
            "status_id" => 1,
            "expected_results" => "Should work smoothly"
        ];
        $request->setBody(GuzzleHttp\Stream\Stream::factory(json_encode($body)));
        $response = $client->send($request);
        echo "Response HTTP : " . $response->getStatusCode();
    }
    catch (RequestException $e) {
        echo "HTTP Request failed\n";
        echo $e->getRequest();
        if ($e->hasResponse()) {
            echo $e->getResponse();
        }
    }
}
Beautiful. I truly appreciate this!