I have a graphQL query that looks like this:
$query = <<<'JSON'
query{
mainProducts(where:{productType:{eq:2}}) {
name, productID
}
}
JSON;
$variables = '';
$json = json_encode(['query' => $query, 'variables' => $variables]);
$chObj = curl_init();
curl_setopt($chObj, CURLOPT_URL, ‘https://whateverurlitis.com’);
curl_setopt($chObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chObj, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($chObj, CURLOPT_HEADER, true);
curl_setopt($chObj, CURLOPT_VERBOSE, true);
curl_setopt($chObj, CURLOPT_POSTFIELDS, $json);
curl_setopt($chObj, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json;charset=utf-8',
'Authorization: bearer '.MYTOKEN
)
);
$response = curl_exec($chObj);
echo $response;
My issue is, at the moment I need to replace the query parameter with a variable to make it dynamic, and the variable can be either a number or array of numbers and I would need to loop them.
Could someone please enlighten me on how to replace the number 2 for productType with a variable?