缺少参数

时间:2018-04-13 10:46:38

标签: php laravel guzzle

我在使用GuzzleHttp在POST请求中传递version参数时遇到问题。

客户端错误:GET https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone导致400 Bad Request响应:{“code”:400,“sub_code”:“C00005”,“error”:“在网址中缺少次要版本参数。要使用最新版本,请添加th(截断...)

这是我尝试过的最新消息:

    $client = new \GuzzleHttp\Client([
        'base_uri' => 'https://gateway.watsonplatform.net/'
    ]);
    $toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
        'auth' => ['{username}', '{password}'],
        'headers' => [
            'Content-Type' => 'text/plain',
        ],
        'form_params' => [
            'version' => '2017-09-21',
            'tone_input' => $text,
            'sentences' => true
        ]
    ]);

这也失败了:

$client = new \GuzzleHttp\Client([
    'base_uri' => 'https://gateway.watsonplatform.net/'
]);
$toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
    'auth' => ['{username}', '{password}'],
    'headers' => [
        'Content-Type' => 'text/plain',
    ],
    'version' => '2017-09-21',
    'tone_input' => $text,
    'sentences' => true
]);

如果使用GET而不是POST,则失败。

如果我更改URI并添加版本,那么它工作正常(好吧,它失败了,因为它没有要在请求中分析的文本):

更改:tone-analyzer / api / v3 / tone

To:tone-analyzer / api / v3 / tone?version = 2017-09-21

所以,我猜问题是您如何使用GuzzleHttp在请求中传递URL参数?

注意:我的CURL命令工作正常(http 200):

curl -X POST -u "{username}":"{password}" --header 'Content-Type: text/plain' --header 'Accept: application/json' --header 'Content-Language: en' --header 'Accept-Language: en' -d 'I hate these new features On #ThisPhone after the update.\r\n \ 
     I hate #ThisPhoneCompany products, you%27d have to torture me to get me to use #ThisPhone.\r\n \ 
     The emojis in #ThisPhone are stupid.\r\n \ 
     #ThisPhone is a useless, stupid waste of money.\r\n \ 
     #ThisPhone is the worst phone I%27ve ever had - ever .\r\n \ 
     #ThisPhone another ripoff, lost all respect SHAME.\r\n \ 
     I%27m worried my #ThisPhone is going to overheat like my brother%27s did.\r\n \ 
     #ThisPhoneCompany really let me down... my new phone won%27t even turn on.' 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2017-09-21&sentences=true'

1 个答案:

答案 0 :(得分:2)

您需要发送"查询"查询字符串的参数。请检查以下代码:

$toneAnalyserResponse = $client->request('POST', 'tone-analyzer/api/v3/tone', [
    'auth' => ['{username}', '{password}'],
    'headers' => [
        'Content-Type' => 'text/plain',
    ],
    'query' => [
       'version' => '2017-09-21'
    ]
]);