Use PHP
Many different programming languages support GraphQL and PHP is one of them.
To execute a GraphQL query, you must send a POST
request to the Wino /graphql
endpoint with your desired query as below:
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer xxxx" \
-d '{ "query": "{ variants { id } }" }' \
https://wino-api-url/graphql
note
To have access to the API url, please contact the Wino team by email at hello@wino.fr
.
Building requests
If you are using PHP, you can create a graphql_query
utility within a graphql-client.php
to facilitate your calls to the GraphQL API as follows:
<?php
// > https://gist.github.com/dunglas/05d901cb7560d2667d999875322e690a
function graphql_query(string $endpoint, string $query, array $variables = [], ?string $token = null): array
{
// Prepare the headers
$headers = ['Content-Type: application/json'];
// Refer to the "Authentication" documentation for the following code:
if (null !== $token) {
$headers[] = "Authorization: Bearer $token";
}
// Run the request
try {
$data = file_get_contents($endpoint, false, stream_context_create([
'http' => [
'method' => 'POST',
'header' => $headers,
'content' => json_encode(['query' => $query, 'variables' => $variables]),
]
]));
return json_decode($data, true);
} catch (Exception $error) {
echo $error->getMessage(), "\n";
}
}
Once done, you can run the graphql_query
function with the desired parameters:
<?php
$query = <<<'GRAPHQL'
query GetVariant($id: ID!) {
variant (id: $id) {
name
product {
name
}
}
}
GRAPHQL;
$response = graphql_query('/graphql', $query, ['id' => 'f74b1dea-f4dd-4611-b372-598686ef3ecf'], 'my-oauth-token');
print_r($response);
The response returned will be as follows:
{
"data": {
"variant": {
"name": "2017, 0.75cL",
"product": {
"name": "Crozes Hermitage"
}
}
}
}
It's as simple as that!
PHP clients
- GraphQL PHP clients exist, you can use for example https://github.com/softonic/graphql-client. It has worked great for us !
- Alternatively, you can use
curl
with PHP to make your calls as detailed on the following link: https://stackoverflow.com/questions/54942131/php-curl-with-calling-an-api-with-graphql.