Geckoboard data set PHP cURL API

axelra82

OK, so I've been playing around with the new Data Sets from Geckoboard This (see ex #1 bellow) is an example from the documentation (which I've tried in a unix command line and it works just fine). But I need to run this in a PHP file (I'm collecting data from WooCommerce before this in my PHP).

Ex #1

  curl https://api.geckoboard.com/datasets/sales.gross/data \
        -X PUT \
        -u '222efc82e7933138077b1c2554439e15:' \
        -H 'Content-Type: application/json' \
        -d '{
        "data": [
            {
            "timestamp": "2016-01-01T12:00:00Z",
            "amount": 819
            },
            {
            "timestamp": "2016-01-02T12:00:00Z",
            "amount": 409
            },
            {
            "timestamp": "2016-01-03T12:00:00Z",
            "amount": 164
            }
    ]
}'

I've tried this

/*****************
*
*   GECKOBOARD
*
*****************/

// API URL
$url    = 'https://api.geckoboard.com/datasets/sales.[DATA_SET_NAME]/data';
$key    = '[MY_API_KEY]';

$data_array = array(
    'net' => $weeklynet,
    'timestamp' => date('Y-m-d h:m:s')
);

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
$options = array(
    CURLOPT_URL => $url,
    CURLOPT_PUT => 1,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Authorization', 'OAuth ' . $key
        ),
    CURLOPT_POSTFIELDS => $data_array
);

curl_setopt_array($ch, $options);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

But nothing happens. I'm not to well versed in cURL with PHP (or at all for that matter). Is there someone who could help me "translate" the command line cURL to a "correct" PHP version? Also, not sure the date will output correctly, it has to be in the format YYYY-MM-DDTHH:MM:SSZ

Thankful for any help!

axelra82

OK, after searching and testing all night I finally came to a working solution. I'm not sure why I was getting the 411 in the first place, but it turns out that removing

CURLOPT_PUT => 1

and replacing it with

CURLOPT_CUSTOMREQUEST => 'PUT'

at least let me send data. Then I got a 401 (Bad request) instead. This was simply because of the way authentication was passed. So I had to remove

'Authorization', 'OAuth ' . $key

from the header array and instead use

CURLOPT_USERPWD =>  $key . ': '

This way I'm passing in the API key (as a user) from geckoboard with a blank password (user:password).

Then there where a set of other issues, but I finally got this working anyway :)

PS. Thank you @Chris for trying to help! Always equally amazed by the general helpfulness of the stackoverflow community.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related