/* gets content from a URL via curl */function get_url($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); //Using Filters in your Analysis $filters =array("property_name" => "pageId", "operator" => "eq", "property_value" => "links-687"); $filters_string = json_encode($filters); $post = array( 'event_collection' => "pagelinks", 'filters' => $filters, 'timeframe' => 'this_14_days' ); $post_string = json_encode($post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Authorization: MY_READ_KEY', 'Content-Type: application/json', 'Content-Length: ' . strlen($post_string) )); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
var_dump($post_string); $content = curl_exec($ch); curl_close($ch); if($content){ return $content; }else{ return false; }}
$keenUrl = "https://api.keen.io/3.0/projects/".$projectId."/queries/count";
$result = get_url($keenUrl);
echo('</br></br>Result:'.$result);string(141) "{"event_collection":"pagelinks","filters":{"property_name":"pageId","operator":"eq","property_value":"links-687"},"timeframe":"this_14_days"}"
Result:{"message": "You must send 'filters' as a list (array).", "error_code": "FiltersMustBeListError"}{"event_collection":"pagelinks",
"filters":[
{"property_name":"pageId","operator":"eq","property_value":"links-687"}],
"timeframe":"this_14_days"
}
"filters" takes an array of filters (defined as lists of key-value pairs). It allows for multiple filters, something like this :
"filters": [
{"property_name":"property1","operator":"eq","property_value":"value1"},{"property_name":"property2","operator":"eq","property_value":"value2"}
]
But if you only use one filter, you still have to encapsulate the filter (key-value pair list) in an array (see above)
PHP arrays() don't really distinguish between a list of key-value pairs and an array of elements, so you'll have to add the encapsulating [] manually.
I hope this helps.
Kind regards,
Dieter
string(145) "{"event_collection":"pagelinks","timeframe":"this_14_days","filters":"[{"property_name":"pageId","operator":"eq","property_value":"1145"}]"}"
Result:{"message": "The specified JSON is invalid. Expecting , delimiter: line 1 column 74 (char 73)", "error_code": "InvalidJSONError"}