【发布时间】:2015-01-25 16:45:16
【问题描述】:
我目前正在处理一个项目并尝试进行 POST 调用。 API 文档说明如下
POST https://jawbone.com/nudge/api/v.1.1/users/@me/mood HTTP/1.1
Host: jawbone.com
Content-Type: application/x-www-form-urlencoded
title=MoodTest&sub_type=2
我的代码是:
$url = "http://jawbone.com/nudge/api/v.1.1/users/@me/mood";
$data = array('title' => 'moodTest', 'sub_type' => 2);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($data)
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
更改特定数据需要title 和sub_type。 我收到以下错误:
Warning: file_get_contents(http://...@me/mood): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in C:\wamp\www\main.php on line 53
Call Stack
# Time Memory Function Location
1 0.0028 265776 {main}( ) ..\main.php:0
2 0.9006 268584 postMood( ) ..\main.php:15
3 0.9007 271680 file_get_contents ( ) ..\main.php:53
我做错了什么?
【问题讨论】:
-
尝试将url改成:jawbone.com/nudge/api/v.1.1/users/@me/mood(用https代替http)
-
遗憾的是它并没有改变任何东西
-
您是否将 'http' => 数组(更改为 'https'?
-
我会用 xhr 请求代替你...
-
@halfer,我相信你是对的