【问题标题】:Laravel 5.1 Guzzle - Undefined offset: 0Laravel 5.1 Guzzle - 未定义的偏移量:0
【发布时间】:2018-10-09 04:03:27
【问题描述】:

我需要访问一个 API,所以我使用 guzzle6 并编写了一个函数:

public function test()
    {

$client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]);

try {
$res = $client->post('https://example.com/api/v2/oauth/token?grant_type=client_credentials', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],

    'auth' => [
        'Username' => 'user_5639',
        'Password' => 'pass_asdhbas67yausihd7qaw8'
]
            ]);

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

catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

    return response()->json(['data' => $result]);
}

    }

但我得到了错误:

Client.php 第 346 行中的 ErrorException:未定义偏移量:0

当我在 POSTMAN 尝试相同的请求时,一切都很好:

如何解决我的问题?

【问题讨论】:

    标签: api laravel-5.1 guzzle


    【解决方案1】:

    如果您查看Guzzle Manual for the auth-option,您会发现它需要一个数字索引数组,用户名在索引 0 上,密码在索引 1 上。

    所以这应该有效:

    $res = $client->post('https://example.com/api/v2/oauth/token?grant_type=client_credentials', [
        'headers' => [
            'Content-Type' => 'application/x-www-form-urlencoded',
        ],
        'auth' => [
            'user_xxxx', 'pass_xxxxx'
        ]
    ]);
    

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 2015-12-20
      • 2020-01-02
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2019-03-21
      • 1970-01-01
      相关资源
      最近更新 更多