在 laravel 中使用 guzzle 的 Infobip 单一短信服务
Infobip single sms service using guzzle in laravel
我正在尝试使用我的 laravel 应用程序发送短信。我有 infobip 测试帐户并使用该详细信息发送短信但收到此错误:
ClientException in RequestException.php line 111:
Client error: `POST https://api.infobip.com/sms/1/text/single` resulted in a `401 Unauthorized` response:
{"requestError":{"serviceException": {"messageId":"UNAUTHORIZED","text":"Invalid login details"}}}
代码:
$data= '{
"from":"InfoSMS",
"to":"923227124444",
"text":"Test SMS."
}';
$userstring = 'myuserame:password';
$id =base64_encode ( 'myuserame:password' );
echo 'Basic '.$id;
$client = new \GuzzleHttp\Client();
$request = $client->post('https://api.infobip.com/sms/1/text/single',array(
'content-type' => 'application/json'
),['auth' => ['myusername', 'password']]);
$request->setHeaders(array(
'accept' => 'application/json',
'authorization' => 'Basic '.$id,
'content-type' => 'application/json'
));
$request->setBody($data); #set body!
$response = $request->send();
echo $res->getStatusCode(); // 200
echo $res->getBody();
return $response;
用户名和密码是正确的,因为我试图从该站点发送直接文本消息并且它在那里工作。
谁能帮我解决我做错了什么?
谢谢!
您在 infobip api 开发人员手册中阅读时不需要传递用户名/密码。
试试这个:
$authEncoded = base64_encode('myuserame:password');
$data = array(
"from" => "InfoSMS",
"to" => "923227124444",
"text" => "Test SMS."
);
$request = new Request('POST', 'https://api.infobip.com/sms/1/text/single',
array(
'json' => $data,
'headers' => array(
'Authorization' => 'Basic ' . $authEncoded,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
)
)
);
$client = new \GuzzleHttp\Client();
$response = $client->send($request);
echo $response->getBody();
我现在无法自己测试它,所以如果它有效或出现错误,请及时通知我。
所以我厌倦了使用 curl 解决问题,我开始为其他人放置代码。
代码:
$data_json = '{
"from":"Infobip",
"to":"9232271274444",
"text":"test msg."
}';
$authorization = base64_encode('username:password');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json',"Authorization: Basic $authorization"));
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.infobip.com/sms/1/text/single');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
//var_dump(curl_getinfo($ch));
var_dump($response);
curl_close($ch);*/
我正在尝试使用我的 laravel 应用程序发送短信。我有 infobip 测试帐户并使用该详细信息发送短信但收到此错误:
ClientException in RequestException.php line 111:
Client error: `POST https://api.infobip.com/sms/1/text/single` resulted in a `401 Unauthorized` response:
{"requestError":{"serviceException": {"messageId":"UNAUTHORIZED","text":"Invalid login details"}}}
代码:
$data= '{
"from":"InfoSMS",
"to":"923227124444",
"text":"Test SMS."
}';
$userstring = 'myuserame:password';
$id =base64_encode ( 'myuserame:password' );
echo 'Basic '.$id;
$client = new \GuzzleHttp\Client();
$request = $client->post('https://api.infobip.com/sms/1/text/single',array(
'content-type' => 'application/json'
),['auth' => ['myusername', 'password']]);
$request->setHeaders(array(
'accept' => 'application/json',
'authorization' => 'Basic '.$id,
'content-type' => 'application/json'
));
$request->setBody($data); #set body!
$response = $request->send();
echo $res->getStatusCode(); // 200
echo $res->getBody();
return $response;
用户名和密码是正确的,因为我试图从该站点发送直接文本消息并且它在那里工作。
谁能帮我解决我做错了什么?
谢谢!
您在 infobip api 开发人员手册中阅读时不需要传递用户名/密码。
试试这个:
$authEncoded = base64_encode('myuserame:password');
$data = array(
"from" => "InfoSMS",
"to" => "923227124444",
"text" => "Test SMS."
);
$request = new Request('POST', 'https://api.infobip.com/sms/1/text/single',
array(
'json' => $data,
'headers' => array(
'Authorization' => 'Basic ' . $authEncoded,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
)
)
);
$client = new \GuzzleHttp\Client();
$response = $client->send($request);
echo $response->getBody();
我现在无法自己测试它,所以如果它有效或出现错误,请及时通知我。
所以我厌倦了使用 curl 解决问题,我开始为其他人放置代码。 代码:
$data_json = '{
"from":"Infobip",
"to":"9232271274444",
"text":"test msg."
}';
$authorization = base64_encode('username:password');
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json',"Authorization: Basic $authorization"));
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'https://api.infobip.com/sms/1/text/single');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
//var_dump(curl_getinfo($ch));
var_dump($response);
curl_close($ch);*/