我想用以下代码进行发布:
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);
}
但是,它无法成功进行发布。出现以下消息:
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
$ data的var_dump:
string 'time_created=1368652731&time_completed=1368656325&sub_type=1&calories=333' (length=73)
$ options的var_dump:
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文档显示以下示例:
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
好的,您想使用file_get_contents发出POST请求。
我认为错误在这一行:
´"header" => "Content-Type: Authorization: Bearer {$_COOKIE['access_token']}\r\n"
有两种元素Content-Type
和Authorization
。
我建议将其更改为:
$options = array(
'http' => array(
"header" => [
"Authorization: Bearer " . $_COOKIE['access_token'],
"Content-Type: application/x-www-form-urlencoded"],
'method' => 'POST',
'content' => $data
),
);
我不知道是否Content-Type
正确,通常是API的使用"Content-Type: application/json"
或者只是丢掉它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句