我有一个如下所示的GraphQL查询:
$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;
我的问题是,目前我需要用一个变量替换查询参数,使其成为动态的,该变量可以是一个数字或数字数组,我需要循环它们.
有人能告诉我如何用变量来代替数字2吗?