php-guzzleHow to use GraphQL with PHP Guzzle?
GraphQL can be used with PHP Guzzle to make HTTP requests to a GraphQL endpoint.
Example code
$client = new \GuzzleHttp\Client();
$response = $client->post('http://example.com/graphql', [
'json' => [
'query' => '{ hello }'
]
]);
Output example
{
"data": {
"hello": "world"
}
}
Code explanation
$client = new \GuzzleHttp\Client();
- creates a new Guzzle client$response = $client->post('http://example.com/graphql', [
- makes a POST request to the GraphQL endpoint'json' => [
- sets the request body to JSON'query' => '{ hello }'
- sets the GraphQL query
Helpful links
More of Php Guzzle
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...