Skip to content

Instantly share code, notes, and snippets.

@dougkusanagi
Forked from mohamedhafezqo/example.php
Last active October 18, 2023 22:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougkusanagi/f4bd1bf8a0097f7a28f907c76130b7ff to your computer and use it in GitHub Desktop.
Save dougkusanagi/f4bd1bf8a0097f7a28f907c76130b7ff to your computer and use it in GitHub Desktop.
GraphQL Client For PHP Using Guzzle
<?php
$endPoint = 'https://api.github.com/graphql';
$query = <<<'GRAPHQL'
query getUsers {
user {
id
name
}
}
GRAPHQL;
graphqlQuery($endPoint, $query, 'oauth-token');
<?php
function graphqlQuery($endPoint, $query, $accessToken);
{
$response = new Client([
'base_uri' => $endPoint,
'headers' => [
'Authorization' => 'Bearer '.$accessToken,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'query' => $query,
]),
]);
return new ArrayCollection(json_decode($response->getBody()->getContents(), true));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment