API

File: 

Proofread Bot provides a rest server that can return the report in several formats from json to xmlrpc. For a sample live client implementation take a look at the Wordpress plugin.

Here is a sample php, json implementation:

You will need this php snippet

<?php

 

include_once("DrupalREST_pbot.php");

 

 

$api = new DrupalREST_pbot('http://proofreadbot.com/server/', 'YOURUSERNAME', 'YOURPASS', TRUE);

$node = new stdClass();

$node->type = 'proofreading';

$node->language = "und";

 

//provide either a URL or a text to search

//$node->field_url[$node->language][0]["url"] = "http://proofreadbot.com";

$node->body[$node->language][0]["value"] = "Your five word long text.";

 

$api->login();

 

$result = $api->createNode(array('node' => $node));

 

print_r($result);

 

$result_body = json_decode($result->body);

if (isset($result->nid)) {

$node = $api->retrieveNode($result->nid);

print_r($node);

}

 

if (isset($result_body[0])) {

print_r($result_body[0]);

}

 

?>

You also need this DrupalREST.php file ( modified from its original version on github)

 

 
<?php
 
class DrupalREST_pbot {
var $username;
var $password;
var $session;
var $endpoint;
var $debug;
 
function __construct($endpoint, $username, $password, $debug)
{
$this->username = $username;
$this->password = $password;
//TODO: Check for trailing slash and fix if needed
$this->endpoint = $endpoint;
$this->debug = $debug;
}
 
function login()
{
$ch = curl_init($this->endpoint . 'user/login.json');
$post_data = array(
'username' => $this->username,
'password' => $this->password,
);
$post = http_build_query($post_data, '', '&');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
            "Accept: application/json",
            "Content-type: application/x-www-form-urlencoded"
));
$response = json_decode(curl_exec($ch));
//Save Session information to be sent as cookie with future calls
if (!is_object($response)) {
print_r($response[0]);
}
 
$this->session = $response->session_name . '=' . $response->sessid;
 
// GET CSRF Token
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
));
curl_setopt($ch, CURLOPT_COOKIE, "$this->session"); 
// $csrf_token = curl_exec($ch);
//print_r($csrf_token);
 
$ret = new stdClass;
 
$ret->response = curl_exec($ch);
$ret->error    = curl_error($ch);
$ret->info     = curl_getinfo($ch);
 
//curl_close($ch);
 
//print_r($ret->response);
$this->csrf_token = $ret->response;
}
 
// Retrieve a node from a node id
function retrieveNode($nid)
{
//Cast node id as integer
$nid = (int) $nid;
$ch = curl_init($this->endpoint . 'node/' . $nid );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
"Accept: application/json",
"Cookie: $this->session"
));
$result = $this->_handleResponse($ch);
 
curl_close($ch);
 
return $result;
}
 
function createNode($node)
{
$post = http_build_query($node, '', '&');
$ch = curl_init($this->endpoint . 'node/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array (
"Accept: application/json",
"Content-type: application/x-www-form-urlencoded",
"Cookie: $this->session",
'X-CSRF-Token: ' .$this->csrf_token
));
 
$result = $this->_handleResponse($ch);
 
curl_close($ch);
 
return $result;
}
 
// Private Helper Functions
private function _handleResponse($ch)
{
$response = curl_exec($ch);
$info = curl_getinfo($ch);
 
//break apart header & body
$header = substr($response, 0, $info['header_size']);
$body = substr($response, $info['header_size']);
 
$result = new stdClass();
 
if ($info['http_code'] != '200')
{
$header_arrray = explode("\n",$header);
$result->ErrorCode = $info['http_code'];
$result->ErrorText = $header_arrray['0'];
} else {
$result->ErrorCode = NULL;
$decodedBody= json_decode($body);
$result = (object) array_merge((array) $result, (array) $decodedBody );
}
 
if ($this->debug)
{
$result->header = $header;
$result->body = $body;
}
 
return $result;
}
}
 
 
 
?>