I'm using Wix's API to query products. I've made progress converting their examples to PHP but am stumped with their filtering method. For example, this basic query:
curl -X POST \
'https://www.wixapis.com/stores/v1/products/query' \
--data-binary '{
"includeVariants": true
}' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>'enter code here
works fine when rewritten as:
public function getProducts($code){
$curl_postData = array(
'includeVariants' => 'true'
);
$params = json_encode($curl_postData);
$productsURL = 'https://www.wixapis.com/stores/v1/products/query';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $productsURL);
curl_setopt($ch, CURLOPT_HEADER, false);
$headers = [
'Content-Type:application/json',
'Authorization:' . $code
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlErr = curl_error($ch);
curl_close($ch);
return $output;
}
But I'm having trouble filtering to a specific collection. Here's their example:
curl 'https://www.wixapis.com/stores/v1/products/query' \
--data-binary '{
"query": {
"filter": "{\"collections.id\": { \"$hasSome\": [\"32fd0b3a-2d38-2235-7754-78a3f819274a\"]} }"
}
}' \
-H 'Content-Type: application/json' \
-H 'Authorization: <AUTH>'
And here's my interpretation of it ($collection is my variable to replace the fixed value in Wix's example):
public function getProducts($code, $collection){
$curl_postData = array(
'filter' => array(
'collections.id' => array(
'$hasSome' => $collection
)
)
);
$params = json_encode($curl_postData);
$productsURL = 'https://www.wixapis.com/stores/v1/products/query';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $productsURL);
curl_setopt($ch, CURLOPT_HEADER, false);
$headers = [
'Content-Type:application/json',
'Authorization:' . $code
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlErr = curl_error($ch);
curl_close($ch);
return $output;
}
The response is identical to the first example--I get all the products instead of just one collection. The problem is almost certainly my interpretation of their example. I think it's meant to be a multidimensional array, but I could be reading it wrong. I'd appreciate any suggestions.
CodePudding user response:
In the second code block, the value of the filter
property is not an object, but a JSON-encoded object. I'm not sure why the API requires that, but it means you have to use nested json_encode()
in PHP.
$curl_postData = array(
'filter' => json_encode(array(
'collections.id' => array(
'$hasSome' => $collection
)
))
);
CodePudding user response:
Thanks, Barmar. Your answer was correct with a minor change. Here's the final code segment that works properly.
$curl_postData = array(
'query' => array(
'filter' => json_encode(array(
'collections.id' => array(
'$hasSome' => $collection
)
))
)
);