Struggling with PHP CURL filters as a list

815 views
Skip to first unread message

Nicholas Stees

unread,
Dec 3, 2015, 8:57:22 AM12/3/15
to Keen IO Community
I could use a little help on sending filters with my PHP CURL request. I can't find an example in the docs that shows how the formatting is supposed to be. Here is my code at the moment. 

/* 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);

which results in 

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"}

I tried using http_build_query($post); as well instead to json_encode(); but I would get errors with both. If someone could show my how to send filters correctly that would be great! 

Dieter Adriaenssens

unread,
Dec 4, 2015, 11:44:05 AM12/4/15
to Nicholas Stees, Keen IO Community
Hi there,

You'll have to end up with something like this :

{
"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

--
Kind regards,

Dieter Adriaenssens

Nicholas Stees

unread,
Dec 7, 2015, 12:18:58 PM12/7/15
to Keen IO Community, nst...@gmail.com

Thanks Dieter, 

I think I am getting close, I am manually constructing the post string now, but now I get an Invalid JSON Error. To me that looks like your example, but maybe I am missing something... 

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"}


Nicholas Stees

unread,
Dec 7, 2015, 12:22:48 PM12/7/15
to Keen IO Community, nst...@gmail.com
SOLVED!!! -- My mistake it was the quotes around the filters array. 

THANK YOU FOR ALL THE HELP!
Reply all
Reply to author
Forward
0 new messages