Ronald Huereca
Ronald Huereca

Reputation: 266

WordPress wp_remote_get and Keen.io Event Creation

I'm using WordPress and wp_remote_get.

I keep getting a 404 error trying to post to my event collection and I'm not sure if I'm even doing this right.

Here is the code I'm currently using:

$bucket = array( 'purchase' => json_encode( $options ) );

$keen_url = 'https://api.keen.io/3.0/projects/PROJECTNAME/events/installs?api_key=KEY';

$headers = array( 
    'Content-Type' => 'application/json'
);

$response = wp_safe_remote_get( $keen_url, array( 'headers' => $headers, 'body' => $bucket ) );
die( '<pre>' . print_r( $response, true ) );

Upvotes: 1

Views: 74

Answers (1)

Michelle Wetzler
Michelle Wetzler

Reputation: 734

There are two methods to posting to Keen, and perhaps they are getting mixed here.

If you're posting a single event directly to the installs URL, I would expect your body to only be the install events themselves, and not contain a collection name like "purchase". What if you remove purchase from your $bucket and just have the JSON-encoded event properties?

Your final request URL would look something like this, where data is the URL-encoded AND base-64 encoded event body, e.g.:

https://api.keen.io/3.0/projects/PROJECT_ID/events/installs?api_key=WRITE_KEY&data=ENCODED_DATA

Perhaps you are using the method for recording multiple events, which takes an array of collections and their events. If you want to try that method, keep the purchases part in your $bucket, and try modifying your $keen_url to:

https://api.keen.io/3.0/projects/PROJECTNAME/events?api_key=KEY' (notice the request goes directly to events and not events/COLLECTION_NAME

Here's an example of what that request looks like in cURL:

$ curl https://api.keen.io/3.0/projects/PROJECT_ID/events \
    -H 'Authorization: WRITE_KEY' \
    -H 'Content-Type: application/json' \
    -d '{
      "signups": [
        { "name" : "bob" },
        { "name" : "mary" }
      ],
      "purchases": [
        { "price": 10 },
        { "price": 20 }
      ]
    }'

Upvotes: 0

Related Questions