【问题标题】:Trying to use bulksms.com API using guzzle in Laravel but returning errors尝试在 Laravel 中使用 guzzle 使用 bulksms.com API 但返回错误
【发布时间】:2019-11-19 08:53:37
【问题描述】:

我一直在尝试使用 guzzle 从 bulksms.com 发送批量短信,但它返回此错误,

guzzlehttp\exception\clientexception 客户端错误:发布 https://api.bulksms.com/v1/messages 导致 401 完整 访问此资源响应需要身份验证::“类型” "https://developer.bulksms.com/json/v1/errors#authentication-failed

我的代码

$client = new Client([
    'base_uri'=>'https://www.bulksms.com/',
    'timeout'=>'900.0'
]); 

//$result = $client->post('', [
//    'form_params' => [
//        'sample-form-data' => 'value'
//    ]
//]);

$result = $client->request('POST','https://api.bulksms.com/v1/messages', [
    'form_params' => [
        'username' => 'username',
        'password' => '****',
        'sender' => 'my appname',
        'recipients' => '+5555555555',
        'message' => 'Testing message',
    ]
]);

【问题讨论】:

  • 再次检查您通过的凭证。比如uname和密码
  • 这需要根据文档进行基本授权,请发送带有基本凭据的授权标头
  • @DhavalPurohit — 错误消息链接到“authentication-failed”(凭据验证失败)而不是“authentication-required”(未提供凭据)。
  • @Quentin 凭据正确!
  • 你尝试过头部授权吗?

标签: php laravel guzzle laravel-5.8 bulksms


【解决方案1】:

其他人已经向您指出正确使用身份验证,并使用 JSON 作为您的请求格式。此外,您使用了错误的变量名。例如,文档使用变量名to,而您使用的是recipients(也许您从其他地方复制并粘贴了该代码?)。

文档中有一个使用 curl 的 PHP 代码示例,位于:https://www.bulksms.com/developer/json/v1/#tag/Message - 为什么不以此为基础,然后将其转换为有效的 Guzzle 请求作为起点?

【讨论】:

  • 怎么样?请帮助我对这卷曲的东西不熟悉!你能提供示例代码吗? @PaoloC
  • 这是我现在遇到的错误客户端错误:POST https://api.bulksms.com/v1/messages/ 导致 400 Bad Request 响应:{ "type" : "developer.bulksms.com/json/v1/errors#bad-request", "title" : "Bad Request" , "状态" : 400, (截断...)
  • 我的代码 $result = $client->request('POST','/v1/messages/', [ 'json' => [ 'from' => 'App name', 'type '=> 'string', 'title'=> 'string', 'status'=> 0, 'detail'=> 'string', 'to' => '+101221', 'body' => '测试信息', 'Content-Type'=>'application/json', 'Authorization'=> '基本'.base64_encode("$username:$password") ], 'auth' => ['username', '*** **', ] ]);
【解决方案2】:

您是否查看过 API 文档中的 Authentication section? 您应该使用 HTTP Basic Auth 通过 API 进行身份验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多