2

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!

Community
  • 1
  • 1
axelra82
  • 517
  • 8
  • 23
  • Clarify "but nothing happens." Be as specific as possible - something is happening; is it returning nothing? Is execution halting at a certain line? Etc. – Sensei James Oct 03 '16 at 15:13

2 Answers2

2

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.

axelra82
  • 517
  • 8
  • 23
0

Assign the curl_exec() command to a variable and see what it returns.

$content = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);
if($headers['http_code'] < 400){
    /* Do stuff as needed starting with perhaps:
       echo "<pre>";
       print_r($content); */
}

Have a look at the php manual on curl here.

RigidBody
  • 656
  • 3
  • 11
  • 26
  • Thank you for replying. I tried it and I got "411 Length Required" back. But I don't really know what to do with it. Googling it I came up with some questions that seem to be far more advanced. I'm just not sure how I pass the API KEY in the cURL in PHP? It works just fine when I do it in the unix command line, but I can't seem to find what the command is in PHP. – axelra82 Oct 03 '16 at 14:27
  • Maybe this can clear up the question as to what it is I need to do (from the "support" at geckoboard) curl https://api.geckoboard.com/ -u "[API_KEY]:" is the command in command line. How is this translated to cURL in PHP? : ) – axelra82 Oct 03 '16 at 14:29
  • Try adding "Content-Length: $length" to your CURLOPT_HTTPHEADER array and set $length = count($data_array); right after your $data_array assignment. – RigidBody Oct 03 '16 at 15:07