Convert between CSV, JSON and SQL files in PHP using the Sqlify API

Once you sign up and get an API key, you will get access to our REST API which lets you upload and convert files without the need to use our web interface.

So before using the API, go ahead and sign up to get an API key if you don’t have one yet.


We don’t have an official PHP SDK yet, but our standard REST API can be used from any programming language with a standard HTTP client.

PHP comes with builtin bindings for cURL, so that’s what we’ll use to make the API calls.

Here an example code that takes a example.json file and converts it to CSV:

<?php

$api_key = "YOUR_API_KEY";
$url = 'https://api.sqlify.io/v1/pipelines/sqlify/convert/run';

$fields = [
  'file' =>
     new \CurlFile('example.json', 'application/octet-stream'),
  'options[to]' => 'csv',
  'return_output' => 'true'
];

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => $fields,
  CURLOPT_HTTPHEADER => [
    'X-Api-Key: ' . $api_key
  ]
]);

$response = curl_exec($curl);

print $response;

Simple isn’t it? Let’s go through the code to see what actually going on:

$api_key = "YOUR_API_KEY";

We will store our API key in this variable so we can reference it later. Remember to change it to be your API key.


$fields = [
  'file' =>
     new \CurlFile('example.json', 'application/octet-stream'),
  'options[to]' => 'csv',
  'return_output' => 'true'
];

These are the fields that will be posted, see all the options suported here.

  • file is the file that we want to convert, we’re using CurlFile here to post the file without having to manually read it.
  • options[to] tells the conversion engine that we want to convert the JSON file to CSV, other formats available are json and sql.
  • return_output tells the API to wait for the conversion to finish and return the result in CSV, if we omit this option, the job will be run in the background and we’ll have to check to see when it’s finished.

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => $fields,
  CURLOPT_HTTPHEADER => [
    'X-Api-Key: ' . $api_key
  ]
]);

$response = curl_exec($curl);

This last step makes the actuall HTTP call to the API and stores the result in $response. The response can be printer or saved in a different file!


Don’t forget to checkout the full documentation for all the different options and capabilities of the API and let us know what kind of tutorials you want to see more of in the future!

Follow us on Twitter.

Shoot us a message if you have any questions: hello@sqlify.io.