Last active
March 3, 2024 22:22
-
-
Save mohamedhafezqo/f813a871d5324a867ecea7baba136b38 to your computer and use it in GitHub Desktop.
GraphQL Client For PHP Using Guzzle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$endPoint = 'https://api.github.com/graphql'; | |
$query = <<<'GRAPHQL' | |
query getUsers { | |
user { | |
id | |
name | |
} | |
} | |
GRAPHQL; | |
graphqlQuery($endPoint, $query, 'oauth-token'); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
So how do i pass variables to my Query