给定参数时API错误没有参数

时间:2018-02-28 09:12:26

标签: php laravel parameters

这是我得到的错误,因为您可以看到网址中有一个参数,但错误显示没有给出任何参数。有人可以帮助我吗?

  

客户端错误:PUT https://webapi.teamviewer.com/api/v1/devices/d38237721?alias=laptop-test导致400 Bad Request响应:   {"错误":" invalid_request"," error_description":"未给出任何参数。"," error_code" :1}

这是我的代码

public function update($device_id, $options)
{
    $token = 'thereisatokenhere';

    $client = new Client(['base_uri' => 'https://webapi.teamviewer.com/api/v1/']);
    $headers = [
        'Authorization' => 'Bearer ' . $token,
        'Accept-Language' => 'en-US',
        'Content-Type' => 'application/json'
    ];


    $response = $client->request('PUT', 'devices/' . $options['device_id'], [
        'headers'         => $headers,
        'form_params'            => [
            'alias' => $options['alias'],
        ],
    ]);

    $response = json_decode($response->getBody()->getContents(), true);

    $deviceIdsAPI = $response['devices'];

    return $deviceIdsAPI;
}

第二

 $request = new Request('PUT', 'https://webapi.teamviewer.com/api/v1/devices/' . $options['device_id'], ['alias' => $options['alias']]);
        $response = $client->send($request, ['timeout' => 2, 'headers' => $headers]);

1 个答案:

答案 0 :(得分:1)

以下是Guzzle中PUT请求的示例:

$client->put('devices/' . $options['device_id'], [
    'body'            => [
        'alias' => $options['alias'],
        'other_field' => '123'
    ],
    'headers'         => $headers,
    'allow_redirects' => false,
    'timeout'         => 5
]);

<强>更新

在最新版本(Guzzle 6)中应该是这样的:

use GuzzleHttp\Psr7\Request;

$request = new Request('PUT', 'http://httpbin.org/put', ['test' => '123']);
$response = $client->send($request, ['timeout' => 2, 'headers' => $headers]);

请参阅this回答,这是官方Guzzle documentation