TL;DR: Run the snippet below in under 60 seconds. Replace <API_KEY> with your API secret. You'll get JSON back with your data.
No Composer or SDK required. Copy, paste, run:
<?php
$endpoint = "https://api.apito.io/secured/graphql";
$query = '{"query":"{ todos { id title status } }"}';
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer <API_KEY>'
]);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Expected output: JSON with a todos array. Example:
{
"data": {
"todos": [
{ "id": "1", "title": "My first task", "status": "INPROGRESS" }
]
}
}
:::info
Schema note: If your project uses a different model (e.g. movies instead of todos), change the query to match. See your API Explorer for available types.
:::
Before you start:
php -vcomposer --versionInstall the PHP GraphQL client:
composer require gmostafa/php-graphql-client
Create a client and run a query:
<?php
require 'vendor/autoload.php';
use GraphQL\Client;
use GraphQL\Query;
$client = new Client(
'https://api.apito.io/secured/graphql',
['Authorization' => 'Bearer <API_KEY>']
);
$gql = (new Query('todos'))
->setSelectionSet(['id', 'title', 'status']);
$results = $client->runQuery($gql);
$data = $results->getData();
print_r($data->todos);
Expected output: Array of todo objects.
The client needs your GraphQL endpoint and auth header:
$client = new Client(
'https://api.apito.io/secured/graphql',
['Authorization' => 'Bearer <API_KEY>']
);
:::tip
Replace <API_KEY> with your API secret from Settings > API Secrets in the Apito console. See Getting API Secrets.
:::
You can pass Guzzle request options as a third argument:
$client = new Client(
'https://api.apito.io/secured/graphql',
[],
[
'connect_timeout' => 5,
'timeout' => 5,
'headers' => [
'Authorization' => 'Bearer <API_KEY>',
'User-Agent' => 'my-app/1.0',
],
]
);
Use your own HTTP client:
$client = new Client(
'https://api.apito.io/secured/graphql',
[],
[],
$myHttpClient
);
$gql = <<<QUERY
query {
todos {
id
title
status
}
}
QUERY;
$results = $client->runRawQuery($gql);
$data = $results->getData();
Object structure (default):
$results = $client->runQuery($gql);
$results->getData()->todos[0]->title;
Array structure:
$results = $client->runQuery($gql, true);
$results->getData()['todos'][0]['title'];
use GraphQL\Variable;
$gql = (new Query('todos'))
->setVariables([
new Variable('status', 'String', false, 'INPROGRESS'),
new Variable('limit', 'Int', false, 10)
])
->setArguments(['status' => '$status', 'first' => '$limit'])
->setSelectionSet(['id', 'title', 'status']);
$vars = ['status' => 'DONE', 'limit' => 5];
$results = $client->runQuery($gql, true, $vars);
use GraphQL\Mutation;
use GraphQL\RawObject;
$mutation = (new Mutation('createTodo'))
->setArguments([
'todoObject' => new RawObject('{title: "New task", status: "TODO"}')
])
->setSelectionSet(['id', 'title', 'status']);
$results = $client->runQuery($mutation);
$mutation = (new Mutation('createTodo'))
->setVariables([new Variable('todo', 'TodoInputObject', true)])
->setArguments(['todoObject' => '$todo']);
$vars = ['todo' => ['title' => 'My task', 'status' => 'TODO']];
$client->runQuery($mutation, true, $vars);
The package supports three ways to build queries:
| Style | Use case |
|---|---|
| Query class | Simple, static queries |
| QueryBuilder | Dynamic queries built at runtime |
| PHP GraphQL-OQM | Schema introspection, no manual query strings |
For OQM and QueryBuilder details, see the official GitHub repo.
| Issue | Fix |
|---|---|
| cURL SSL errors | Enable curl.cainfo in php.ini or use CURLOPT_SSL_VERIFYPEER => false (dev only) |
Class not found | Run composer dump-autoload |
401 Unauthorized | Check Authorization: Bearer <API_KEY> and API secret |
Empty or null data | Verify query fields exist in your schema (use API Explorer) |
| JSON decode errors | Ensure Content-Type: application/json header is set |
Do I need Composer?
No. The 60-second example uses plain PHP + cURL. Use Composer only if you want the SDK.
Which auth format?
Use Bearer <API_KEY>. Get the key from Apito Console > Settings > API Secrets.
Can I use a different query?
Yes. Replace todos with your model name (e.g. movies, users). Check the API Explorer for your schema.
example/php-graphql-apito-integration in this repo; run with php -S localhost:8000