How to use the POEditor API

Tutorial on how to use poeditor.com api with php http requests.

To use the API for POEditor, you need to use HTTP requests. However, last we checked, the documentation for the API was somewhat poor, leaving lots of room for confusion for beginners trying to use it the first time – especially true if they have not worked with APIs before. This tutorial will show you how to connect to the API using HTTP POST requests in PHP.

The response you'll get will be a JSON encoded object, you can easily decode this to a neat PHP array.

Locating your api_token

To connect to the API you will need a so-called api_token – don't worry, this is easy to find. Open My account at poeditor.com, then click on options – a drop-down will open up – click on API access; this is where you'll find your api_token.

After you have located your API_TOKEN, place it in the $API_TOKEN variable of the example script.

Communicating with the API

Sending requests to the poeditor API is done using HTTP requests, this tutorial will show you how communicate with the API using PHP – the principles are the same in other languages, so it should be easy to port it – lets get started!

See the Tutorial for details: HTTP Requests in PHP

The following example shows how to send a simple POST Request. To be clear: the arguments mentioned in the API documentation should be sent as POST data. Do not try to send it as URL parameters!

<?php
  // Arguments
  $API_TOKEN = '';
  $ACTION = 'export';
  $PROJECT_ID = '1234';
  $LANGUAGE_CODE = 'da';
  $OUTPUT_FORMAT = 'po';
  
  
  // --- Where we mess around! ---
  // The POST URL
  $sURL = "http://poeditor.com/api/";
  // The POST Data
  $sPD = "api_token=$API_TOKEN&action=$ACTION&id=$PROJECT_ID&language=$LANGUAGE_CODE&type=$OUTPUT_FORMAT";
  
  // Headers
  $aHTTP['http']['header'] = "Content-type: application/x-www-form-urlencoded\r\n";
  $aHTTP['http']['header'] .= "User-agent: Brugbart POEditor API Example Script\r\n";

  // Request type and POST data, if any...
  $aHTTP['http']['method'] = "POST";
  $aHTTP['http']['content'] = $sPD;

  // Prepare Request to be sent
  $context = stream_context_create($aHTTP);
  // Perform Request and save Response body to $contents variable
  $contents = file_get_contents($sURL, false, $context);

  echo $contents;

The POST Arguments

In case you are confused about the argument variables, let us take a moment to explain how to fill them out in the below table:

ArgumentWhat is this?
$API_TOKENThe api_token authenticates you to use the API - see: (external) POEditor API::Authentication
$ACTIONThe action to perform I.e. Export
$PROJECT_IDThe ID of the translation project – you can find this in the URL of your project.
$LANGUAGE_CODEThe language code, likely as specified in ISO-639. I.e. en, da, de, fr
$LANGUAGE_CODEThe format you want to export in – we used po in this tutorial.

Finally. By using this example, you should be able to avoid errors such as the following:

{"response":{"status":"fail","message":"Please use a POST request.","code":4012}}
{"response":{"status":"fail","message":"You don't have permission for this resource","code":"403"}}
{"response":{"status":"fail","message":"Request missing api token","code":401}}