Am using Insomnia to test my graphQL queries, and for this one all works fine. However, moving over to PHP am not winning, and my code looks like this using curl:
$data = array ("query" => "{
viewer {
zones(filter: { zoneTag: '" . $zone . "' }) {
httpRequestsAdaptive(
filter: {
datetime_geq: '2023-05-10T08:00:00Z'
datetime_lt: '2023-05-10T08:05:00Z'
}
limit: 10000
orderBy: [
datetime_DESC
]
) {
device: clientDeviceType
clientIP: clientIP
}
}
}
}
");
$data = http_build_query($data);
$headers = [];
$headers[] = 'Content-Type: application/json';
$headers[] = "Authorization: Bearer $token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/graphql");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result_plans = curl_exec($ch);
$result_plans = json_decode($result_plans, true);
curl_close($ch);
$response = $result_plans;
I get the following message when I run the script:
failed to recognize JSON request: 'invalid character 'q' looking for beginning of value'
How can I run this query using PHP curl?