2

I want to make a post call with the following code:

function addWorkout() {
// url for workouts
$url = "https://jawbone.com/nudge/api/v.1.1/users/@me/workouts";

// set data to send
$data = http_build_query(array(
    'time_created' => intval($_GET['starttime']),
    'time_completed' => intval($_GET['endtime']),
    'sub_type' => intval($_GET['sport']),
    'calories' => intval($_GET['calories']),
));

var_dump($data);

$options = array(
    'http' => array(
        "header" => "Content-Type: Authorization: Bearer {$_COOKIE['access_token']}\r\n",
        'method' => 'POST',
        'content' => $data
    ),
);

var_dump($options);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

}

However it doesn't successfully make the post call. The following message appears: Warning

Warning: file_get_contents(https://...@me/workouts): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\FitnessWebApp\.idea\html\sport.php on line 90
Call Stack
#   Time    Memory  Function    Location
1   0.0006  280568  {main}( )   ..\sport.php:0
2   1.2201  293408  addWorkout( )   ..\sport.php:19
3   1.2204  296272  file_get_contents ( )   ..\sport.php:90

var_dump of $data:

string 'time_created=1368652731&time_completed=1368656325&sub_type=1&calories=333' (length=73)

var_dump of $options:

    array (size=1)
  'http' => 
    array (size=3)
      'header' => string 'Content-Type: Authorization: Bearer ...

' (length=166)
      'method' => string 'POST' (length=4)
      'content' => string 'time_created=1368652731&time_completed=1368656325&sub_type=1&calories=333' (length=73)

API documentation shows the following example:

POST https://jawbone.com/nudge/api/v.1.1/users/@me/workouts HTTP/1.1
Host: jawbone.com
time_created=1368652731&time_completed=1368656325&sub_type=3&calories=115
ekad
  • 13,718
  • 26
  • 42
  • 44
user3549524
  • 227
  • 1
  • 2
  • 10

2 Answers2

0

file get contents does not work with https. You will have to use curl instead.

/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //will not complain about certs
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

then call

$returned_content = get_data('https://jawbone.com/nudge/api/v.1.1/users/@me/workouts');
Dimi
  • 1,235
  • 13
  • 19
  • thanks for that, however I do not really understand where I have to set "time_created", "time_completed", "sub_type" and "calories" (the things I need to do the POST call..? – user3549524 May 22 '15 at 18:34
  • Dimi - he wants to do a POST request. – Jens A. Koch May 22 '15 at 18:35
  • Might I suggest [Guzzle](https://github.com/guzzle/guzzle)? [Quickstart](http://docs.guzzlephp.org/en/latest/quickstart.html#make-a-request) – Sammitch May 22 '15 at 18:36
  • Thanks Sammitch, but I have to finish a project very soon and I don't want to change everything now – user3549524 May 22 '15 at 18:37
  • oops. a little overwhealmed with things at the moment. Will come back in a couple hours and add more detailed info. just use this for reference for now http://stackoverflow.com/questions/2138527/php-curl-http-post-sample-code – Dimi May 22 '15 at 18:38
0

Ok, you want to do a POST request with file_get_contents.

I think the error is in this line:

´"header" => "Content-Type: Authorization: Bearer {$_COOKIE['access_token']}\r\n"

There are two elements here Content-Type and Authorization.

I suggest to alter this into:

$options = array(
    'http' => array(
        "header" => [
            "Authorization: Bearer " . $_COOKIE['access_token'],
            "Content-Type: application/x-www-form-urlencoded"],
        'method' => 'POST',
        'content' => $data
    ),
);

I don't know if Content-Type is correct, normally API's use "Content-Type: application/json"

Or just leave it away.

Jens A. Koch
  • 34,456
  • 12
  • 100
  • 117
  • Amazing.... Thanks. I actually did it similar to your solution (with content-type) before but it showed me the same error, idk what the problem is.. but it works now, thank you very much. – user3549524 May 22 '15 at 18:45