【问题标题】:POST call does not work correctlyPOST 调用无法正常工作
【发布时间】: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,我相信你是对的

标签: php html api rest post


【解决方案1】:

你的问题似乎是你没有通过身份验证。

如果您打开此请求:

https://jawbone.com/nudge/api/v.1.1/users/@me/mood?title=asd&sub_type=2

在您的浏览器上,您将看到错误的详细信息。如果您检查响应中的标头,您会看到状态代码是“404 Not Found”。

我建议您查看 API 文档,了解如何进行身份验证或切换到支持的 API 版本(因为响应消息是“不支持的 API 版本 1.1,除非使用 OAuth 标头调用”)。

【讨论】:

  • 我想我在某处读到该消息具有误导性。但是我不认为身份验证是问题,因为 GET 请求工作得很好,而且他们之前也需要身份验证。
  • 我目前正在使用 Postman 尝试使用您提供的文档生成 POST 请求,但我无法获得与该响应不同的任何内容。您是否能够以某种方式生成发布请求(与使用 php 不同)?
  • 要进行 API 调用,您需要一个 Jawbone 帐户,也许这就是原因?遗憾的是,我无法以另一种方式生成发布请求..(API 的文档:jawbone.com/up/developer
  • 这就是问题所在,在您的 stream_context_create 选项中,您没有在标头上提供任何身份验证。
  • 在 GET 请求中我有这样的:'header' => "Authorization: Bearer {$access_token}\r\n" .... 但是,正如您在我的第一篇文章中看到的那样,标题现在有“内容类型”的东西..我该如何更改它?
【解决方案2】:

使用javascript:

function getJson() {
            var xhr = new XMLHttpRequest();
            xhr.open("get", "https://jawbone.com/nudge/api/v.1.1/users/@me/mood", true);
            xhr.onload = function(){
                var response = JSON.parse(xhr.responseText);
            }
            xhr.send(null);
}

【讨论】:

  • 这不会违反同源政策吗?
  • @halfer 不应该。我为我开发的网站使用了相同的脚本,并且运行良好。
【解决方案3】:

php POST 最好的方式是使用 curl (http://php.net/manual/ru/function.curl-exec.php)

所以在您的情况下,您可以使用示例 (http://php.net/manual/ru/function.curl-exec.php#98628):

function curl_post($url, array $post = NULL, array $options = array()) 
{ 
    $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => http_build_query($post) 
    ); 

    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    if( ! $result = curl_exec($ch)) 
    { 
        trigger_error(curl_error($ch)); 
    } 
    curl_close($ch); 
    return $result; 
} 

$url = "http://jawbone.com/nudge/api/v.1.1/users/@me/mood";
$data = array('title' => 'moodTest', 'sub_type' => 2);

$result = curl_post($url, $data);

【讨论】:

  • 我只收到一个通知,而 var_dump 显示一个空字符串
  • @user3549524:你提到的通知是什么?
  • 尝试将trigger_error(curl_error($ch)); 改为var_dump(curl_error($ch));
  • 这显示了一个空字符串。 @halfer:那些已经不在了
  • 顺便说一句,刚刚在浏览器中打开这个 url 它返回 {"meta": {"error_detail": "Unsupported API version 1.1, unless called with an OAuth header", "code": 404, “error_type”:“endpoint_error”,“time”:1422207074,“message”:“未找到”,“user_xid”:“”},“data”:{}}
猜你喜欢
  • 1970-01-01
  • 2017-06-18
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 2012-05-18
相关资源
最近更新 更多