Content · Web Development · PHP

Calling GraphQL APIs from PHP

POST a parameterized GraphQL query and variables as JSON to an approved.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

POST a parameterized GraphQL query and variables as JSON to an approved endpoint. The examples separate the essential operation from the conditions a production implementation must enforce.

What It Does


POST a parameterized GraphQL query and variables as JSON to an approved endpoint.

Prerequisites


Use a dedicated development environment with test accounts and records. Separate authorization, validation and error handling from response formatting; never copy demonstration-only secrets into production.

Example 1: Core Behavior


The first example isolates the core operation so its result is easy to understand.

<?php
$query='query Product($id: ID!) { product(id: $id) { id name } }';
$payload=['query'=>$query,'variables'=>['id'=>'p_12']];
echo json_encode($payload,JSON_THROW_ON_ERROR);

Example 2: Alternative or Failure Case


The second example demonstrates an alternative outcome, rule or failure condition.

<?php
$response=['data'=>null,'errors'=>[['message'=>'Access denied']]];
if (!empty($response['errors'])) { echo 'GraphQL errors: ' . count($response['errors']); }

Practical Example: Applying the Rule


This practical workflow performs the relevant operation, with strict error checks. Configure the required transport or database driver before running it.

<?php
$query = 'query Product($id: ID!) { product(id: $id) { id name } }';
$payload = json_encode(['query' => $query, 'variables' => ['id' => 'p_12']], JSON_THROW_ON_ERROR);
$ch = curl_init('https://graphql.example.test/graphql');
curl_setopt_array($ch, [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $payload, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 8, CURLOPT_FOLLOWLOCATION => false]);
$body = curl_exec($ch);
if ($body === false) throw new RuntimeException('GraphQL transport failure');
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status !== 200) throw new RuntimeException('HTTP ' . $status);
$decoded = json_decode($body, true, 512, JSON_THROW_ON_ERROR);
if (!empty($decoded['errors'])) throw new RuntimeException('GraphQL operation error');
if (!isset($decoded['data']['product'])) throw new UnexpectedValueException('Missing product data');
echo json_encode($decoded['data']['product']);

Important Behavior and Edge Cases


GraphQL may return HTTP 200 with a nonempty errors array; validate both fields and response shape.

Comparison with Related Functions or Patterns


ApproachWhen it differs
Calling GraphQL APIs from PHPPOST a parameterized GraphQL query and variables as JSON to an approved endpoint.
Related API or patternREST resources expose multiple endpoints instead of a GraphQL operation document.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the errors field just because the HTTP status is 200GraphQL may return HTTP 200 with a nonempty errors array; validate both fields and response shape.
Substituting a related API without checking its semanticsREST resources expose multiple endpoints instead of a GraphQL operation document.

Key Takeaways


  • POST a parameterized GraphQL query and variables as JSON to an approved endpoint.
  • GraphQL may return HTTP 200 with a nonempty errors array; validate both fields and response shape.
  • REST resources expose multiple endpoints instead of a GraphQL operation document.

Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.

Share this resourceSend this page to someone who may find it useful.