Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit of OHT API Client library for PHP

  • Loading branch information...
commit 06c49f4dc94d6020468ed624c9d65fc9bed7bae0 0 parents
Oren yagev authored
0  README
No changes.
43 examples/callback.php
@@ -0,0 +1,43 @@
+<?php
+
+$result = array();
+
+$type = (!empty($_POST['type'])) ? $_POST['type'] : NULL ;
+$result['type'] = $type;
+
+switch ($type) {
+ case 'status_change' :
+ $result['project_id'] = (!empty($_POST['project_id'])) ? (int)$_POST['project_id'] : NULL ;
+ $result['word_count'] = (!empty($_POST['word_count'])) ? (int)$_POST['word_count'] : NULL ;
+ $result['source'] = (!empty($_POST['source'])) ? $_POST['source'] : NULL ;
+ $result['target'] = (!empty($_POST['target'])) ? $_POST['target'] : NULL ;
+ $result['credits'] = (!empty($_POST['credits'])) ? (int)$_POST['credits'] : NULL ;
+ $result['project_status'] = (!empty($_POST['project_status'])) ? $_POST['project_status'] : NULL ;
+ $result['estimate_finish'] = (!empty($_POST['estimate_finish'])) ? $_POST['estimate_finish'] : NULL ;
+ $result['translation_ready'] = (!empty($_POST['translation_ready'])) ? (bool)$_POST['translation_ready'] : NULL ;
+ $result['project_url'] = (!empty($_POST['project_url'])) ? $_POST['project_url'] : NULL ;
+ break;
+ case 'translation_submitted':
+ $result['project_id'] = (!empty($_POST['project_id'])) ? (int)$_POST['project_id'] : NULL ;
+ $result['original_content'] = (!empty($_POST['original_content'])) ? base64_decode($_POST['original_content']) : NULL ;
+ $result['translated_content'] = (!empty($_POST['translated_content'])) ? base64_decode($_POST['translated_content']) : NULL ;
+ $result['content_type'] = (!empty($_POST['content_type'])) ? $_POST['content_type'] : NULL ;
+ break;
+
+}
+
+ob_start();
+
+print_r($result);
+
+$ob = ob_get_clean();
+ob_end_clean();
+
+$h = fopen('/tmp/callback','a');
+fwrite($h,"*************\n");
+fwrite($h,date('Y-m-d H:i:s')."\n");
+fwrite($h,$ob);
+fwrite($h,"\n*************\n\n");
+fclose($h);
+
+?>
18 examples/getAccountDetails.php
@@ -0,0 +1,18 @@
+<?php
+ require_once '../lib/OHTAPI.php';
+ try{
+ OHTAPI::config(array(
+ 'account_id'=> 269 , //demo user account
+ 'secret_key' => '5a68561984276108fa42d7cffbbf91a5' , //demo user account
+ true //use sandbox
+ ));
+ $oht = OHTAPI::instance();
+
+ $result = $oht->getAccountDetails();
+
+ var_dump($result);
+ }catch(Exception $e){
+ echo $e;
+ }
+
+?>
18 examples/getProjectContents.php
@@ -0,0 +1,18 @@
+<?php
+ require_once '../lib/OHTAPI.php';
+ try{
+ OHTAPI::config(array(
+ 'account_id'=> 269 , //demo user account
+ 'secret_key' => '5a68561984276108fa42d7cffbbf91a5' , //demo user account
+ true //use sandbox
+ ));
+ $oht = OHTAPI::instance();
+
+ $result = $oht->getProjectContents(1418);
+
+ var_dump($result);
+ }catch(Exception $e){
+ echo $e;
+ }
+
+?>
18 examples/getProjectDetails.php
@@ -0,0 +1,18 @@
+<?php
+ require_once '../lib/OHTAPI.php';
+ try{
+ OHTAPI::config(array(
+ 'account_id'=> 269 , //demo user account
+ 'secret_key' => '5a68561984276108fa42d7cffbbf91a5' , //demo user account
+ true //use sandbox
+ ));
+ $oht = OHTAPI::instance();
+
+ $result = $oht->getProjectDetails(1418);
+
+ var_dump($result);
+ }catch(Exception $e){
+ echo $e;
+ }
+
+?>
31 examples/newProject.php
@@ -0,0 +1,31 @@
+<?php
+ require_once '../lib/OHTAPI.php';
+ try{
+ OHTAPI::config(array(
+ 'account_id'=> 269 , //demo user account
+ 'secret_key' => '5a68561984276108fa42d7cffbbf91a5' , //demo user account
+ true //use sandbox
+ ));
+ $oht = OHTAPI::instance();
+
+ $result = $oht->newProject(
+ 'en-us', //from English
+ 'fr-fr', //to French
+ 'A book is a set of printed sheets of paper held together between two covers.
+ The sheets of paper are usually covered with a text:
+ language and illustrations: that is the main point of a printed book.
+ A book can also be a text in a larger collection of texts.
+ This text has some features that do not apply to the collection as a whole.
+ aps written by one author, or it only treats one subject area.
+ Books in this sense can often be understood without knowing the whole collection.',
+ 0, //word count - leave 0 or false to auto-count
+ 'Please be accurate!', //note to the translator (will not be translated)
+ 'http://www.example.com/path/to/callback' //callback url
+ );
+
+ var_dump($result);
+ }catch(Exception $e){
+ echo $e;
+ }
+
+?>
351 lib/OHTAPI.php
@@ -0,0 +1,351 @@
+<?php
+/*
+ * OneHourTranslation API Client Library for PHP Platform
+ * Version 1.0
+ * Copyright (c) 2010 OneHourTranslation(tm) Ltd. <info@onehourtranslation.com>
+ */
+
+
+if (!function_exists('curl_init')) {
+ throw new Exception('OneHourTranslation needs the CURL PHP extension.');
+}
+if (!function_exists('json_decode')) {
+ throw new Exception('OneHourTranslation needs the JSON PHP extension.');
+}
+
+define('OHT_API_ACCOUNT_ID' , '269'); //demo account
+define('OHT_API_SECRET_KEY' , '5a68561984276108fa42d7cffbbf91a5'); //demo account
+define('OHT_API_SANDBOX' , true);
+
+class OHTAPI_Exception extends Exception{
+
+ protected
+ $httpCode ,
+ $statusCode ,
+ $statusMessage
+ ;
+
+ function __construct($http_code , $status_code , $status_message){
+ parent::__construct(sprintf("#%d %s (HTTP %d)" , $status_code , $status_message , $http_code ));
+ $this->setHttpCode($http_code);
+ $this->setStatusCode($status_code);
+ $this->setStatusMessage($status_message);
+
+ }
+
+
+ /**
+ *
+ * @return
+ */
+ public function getHttpCode()
+ {
+ return $this->httpCode;
+ }
+
+ /**
+ *
+ * @param $httpCode
+ */
+ public function setHttpCode($httpCode)
+ {
+ $this->httpCode = $httpCode;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public function getStatusCode()
+ {
+ return $this->statusCode;
+ }
+
+ /**
+ *
+ * @param $statusCode
+ */
+ public function setStatusCode($statusCode)
+ {
+ $this->statusCode = $statusCode;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public function getStatusMessage()
+ {
+ return $this->statusMessage;
+ }
+
+ /**
+ *
+ * @param $statusMessage
+ */
+ public function setStatusMessage($statusMessage)
+ {
+ $this->statusMessage = $statusMessage;
+ }
+
+ function __toString(){
+ return $this->getMessage();
+ }
+}
+
+class OHTAPI {
+ /**
+ * @var string
+ */
+ static $account_id ;
+
+ /**
+ * @var string
+ */
+ static $secret_key;
+
+ /**
+ * @var boolean
+ */
+ static $sandbox;
+
+ /**
+ * @var OHTAPI
+ */
+ protected static $instance = NULL;
+
+ const
+ VERSION='1.0b' ,
+ OHT_PRODUCTION_URL='https://www.onehourtranslation.com/api/1' ,
+ OHT_SANDBOX_URL='https://sandbox.onehourtranslation.com/api/1'
+
+ ;
+
+
+
+ /**
+ * @var string
+ */
+ protected $_account_id ;
+ /**
+ * @var string
+ */
+ protected $_secret_key;
+
+ /**
+ * @var boolean
+ */
+ protected $_sandbox;
+
+ /**
+ * Performes preliminary configuration on the OHTAPI class.<br />Make sure to run it before calling OHTAPI::instance() for the first time
+ *
+ * @param array $conf - contains the following parameters:<br />
+ * <ol>
+ * <li>'account_id' - Your OHT account ID</li>
+ * <li>'secret_key' - Your OHT secret API key</li>
+ * <li>'sandbox' - (boolean) Use OHT sandbox</li>
+ * </ol>
+ *
+ */
+ static public function config($conf=array()){
+ self::$account_id = (!empty($conf['account_id'])) ? $conf['account_id'] : OHT_API_ACCOUNT_ID;
+ self::$secret_key = (!empty($conf['secret_key'])) ? $conf['secret_key'] : OHT_API_SECRET_KEY;
+ self::$sandbox = (!empty($conf['sandbox'])) ? $conf['sandbox'] : OHT_API_SANDBOX;
+
+ }
+
+ /**
+ * Fetch OHTAPI Instance. Make sure to run this right after running config()
+ *
+ * @return OHTAPI
+ */
+ static public function instance(){
+ if (!self::$instance){
+ $className = __CLASS__;
+ self::$instance = new $className (self::$account_id , self::$secret_key , self::$sandbox);
+ }
+ return self::$instance;
+ }
+
+ /**
+ * @param $account_id
+ * @param $secret_key
+ * @param $sandbox boolean true to use OHT sandbox
+ */
+ public function __construct($account_id = OHT_API_ACCOUNT_ID , $secret_key = OHT_API_SECRET_KEY , $sandbox=OHT_API_SANDBOX){
+ $this->setAccountId( $account_id );
+ $this->setSecretKey($secret_key);
+ $this->setSandbox( $sandbox );
+ }
+
+ /**
+ * Create a new Translation Project
+ * @param string $source
+ * @param string $target
+ * @param string $content
+ * @param integer $word_count (optional)
+ * @param string $notes (optional)
+ * @param string $callback_url (optional)
+ * @return stdClass response object
+ */
+ public function newProject($source , $target , $content , $word_count=0 , $notes='' , $callback_url=''){
+ $url = '/project/new/';
+ $method = 'post';
+ $params = array(
+ 'source'=>$source,
+ 'target'=>$target ,
+ 'content' => $content ,
+ 'word_count' => $word_count ,
+ 'notes' => $notes ,
+ 'callback_url' => $callback_url
+ );
+ return $this->request($url , $method , $params);
+
+ }
+
+ /**
+ * Fetch project details by project id
+ *
+ * @param integer $project_id
+ * @return stdClass response object
+ */
+ public function getProjectDetails($project_id){
+ $url = "/project/{$project_id}/details/";
+ $method='get';
+ return $this->request($url , $method);
+ }
+ /**
+ * Fetch project translated text and source text by project id
+ *
+ * @param integer $project_id
+ * @return stdClass response object
+ */
+ public function getProjectContents($project_id){
+ $url = "/project/{$project_id}/contents/";
+ $method='get';
+ $result = $this->request($url , $method);
+ if (!empty($result->original_content)){
+ $result->original_content = base64_decode($result->original_content);
+ }
+ if (!empty($result->translated_content)){
+ $result->translated_content = base64_decode($result->translated_content);
+ }
+ return $result;
+ }
+
+ /**
+ * Fetch account details and credits balance
+ *
+ * @return stdClass response object
+ */
+ public function getAccountDetails(){
+ $url = "/account/details/";
+ $method='get';
+ return $this->request($url , $method);
+ }
+
+ protected function request($requestURL , $method='get' , $params=array()){
+ $ch = curl_init();
+ $url = $this->getBaseURL() . $requestURL;
+ $opts = array();
+ $params['account_id'] = $this->getAccountId();
+ $params['secret_key'] = $this->getSecretKey();
+
+ if ($method=='post'){
+ $opts[CURLOPT_URL] = $url;
+ $opts[CURLOPT_POSTFIELDS] = $params;
+ }else{
+ $opts[CURLOPT_URL] = $url. '?' . http_build_query($params);
+ }
+ $opts[CURLOPT_RETURNTRANSFER] = TRUE;
+ //$opts[CURLOPT_HEADER] = TRUE;
+
+ curl_setopt_array($ch , $opts);
+ $result = curl_exec($ch);
+
+
+ $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
+
+ curl_close($ch);
+
+ if ($http_code==404){
+ throw new Exception('OneHourTranslation could not be reached.');
+ }else{
+ $obj = json_decode($result);
+ if (!is_object($obj)){
+ throw new Exception('OneHourTranslation response was malformed.');
+ }elseif($http_code != 200){
+ throw new OHTAPI_Exception($http_code , $obj->status_code , $obj->status_msg);
+ }else{
+ return $obj;
+ }
+
+ }
+ }
+
+
+ /**
+ *
+ * @return
+ */
+ public function getAccountId()
+ {
+ return $this->_account_id;
+ }
+
+ /**
+ *
+ * @param $_account_id
+ */
+ public function setAccountId($_account_id)
+ {
+ $this->_account_id = $_account_id;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public function getSecretKey()
+ {
+ return $this->_secret_key;
+ }
+
+ /**
+ *
+ * @param $_secret_key
+ */
+ public function setSecretKey($_secret_key)
+ {
+ $this->_secret_key = $_secret_key;
+ }
+
+ /**
+ *
+ * @return
+ */
+ public function getSandbox()
+ {
+ return (boolean)$this->_sandbox;
+ }
+
+ /**
+ *
+ * @param $_sandbox
+ */
+ public function setSandbox($_sandbox)
+ {
+ $this->_sandbox = (boolean)$_sandbox;
+ }
+
+ public function getBaseURL(){
+ if ($this->getSandbox()){
+ return self::OHT_SANDBOX_URL;
+ }
+ return self::OHT_PRODUCTION_URL;
+ }
+}
+
+
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.