php - Laravel Guzzle Http Auth 不起作用

标签 php http

我尝试使用 API 发送短信。我遵循了这个示例中的代码,但转换为 Guzzle:

POST Rest 请求的 PHP 实现示例

<?php
…....
try
{

//POST
$httpClient = new Zend_Http_Client();
$httpClient->setUri("http://www.web2sms.ro/prepaid/message");
$httpClient->setMethod(Zend_Http_Client::POST);
$httpClient->setHeaders("Content-Type", "application/json");

//method param
$crtDate = new Zend_Date();
$apiKey = ""; //value provided
$nonce = $crtDate->get(Zend_Date::TIMESTAMP);
$method = "POST";
$url = "/prepaid/message";
$sender = "";
$recipient = "07xxxxxxxx";//To be fill in !
$message = "";
$visibleMessage = "How the message do you want to appear on the interface. If empty string than $message value will be shown";
$scheduleDate = ''; //Format yyyy-MM-dd HH:mm:ss
$validityDate = ''; //Format yyyy-MM-dd HH:mm:ss
$callbackUrl = '';
$secret = ""; // value provided

$string = $apiKey . $nonce . $method . $url . $sender . $recipient . $message . $visibleMessage . $scheduleDate . $validityDate . $callbackUrl . $secret;

$signature = hash('sha512', $string);
$httpClient->setAuth($apiKey, $signature);

$data = array(
"apiKey" => $apiKey,
"sender" => $sender,
"recipient" => $recipient,
 message" => $message,
"scheduleDatetime" => $scheduleDate,
"validityDatetime" => $validityDate,
"callbackUrl" => $callbackUrl,
"userData" => "",
"visibleMessage" => $visibleMessage,
"nonce" => $nonce);
$httpClient->setRawData(Zend_Json::encode($data));

$httpResponse = $httpClient->request();
var_dump($httpResponse);
var_dump($httpResponse->getBody());
var_dump(json_decode($httpResponse->getBody()));
}
catch (Zend_Http_Client_Exception $e)
{
//
}

我的 Guzzle 代码:

$apiKey = "xxxxxxxxx";
$nonce = time();
$method = "POST";
$url = "http://www.web2sms.ro/prepaid/message";
$sender = "XXXXXXXX";
$recipient = "0768814244";
$message = "Un mesaj";
$visibleMessage = "How the message do you want to appear on the interface. If empty string than value will be shown";
$secret = "xxxxxxxxxx";

$string = $apiKey . $nonce . $method . $url . $sender . $recipient . $message . $visibleMessage . $secret;

$signature = hash('sha512', $string);

$client = new GuzzleHttp\Client([
    'headers' => ['Content-Type' => 'application/json']
]);

$data = array(
    "apiKey" => $apiKey,
    "sender" => $sender,
    "recipient" => $recipient,
    "message" => $message,
    "visibleMessage" => $visibleMessage,
    "nonce" => $nonce);

$body = GuzzleHttp\json_encode($data);

$request = $client->request('POST', 'http://www.web2sms.ro/prepaid/message', ['auth' => [$apiKey, $signature], 'body' => $body]);

但还是不行; 我收到此错误:

Client error: `POST http://www.web2sms.ro/prepaid/message` resulted in a `401 Unauthorized` response:

{“错误”:{“代码”:268435459,“消息”:“IDS_App_Controller_Rest_Message__E_INVALID_REQUEST_DATA_WRONG_SIGNATURE”}}

我哪里错了??

最佳答案

我可以通过将 $url 更改为 /prepaid/message 来解决这个问题。

关于php - Laravel Guzzle Http Auth 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40503301/

相关文章:

android - Android DefaultHttpClient 中超时值的默认连接

java - 在基于 REST 的应用程序上是否有与 Python 的请求模块等效的 Java

php - ZF2 Query remove table name in column 函数

javascript - 如何在 Twig 中包含样式表

javascript - 如何动态生成下拉菜单

php - 访问 $_POST 或其他超全局变量时是否可能触发错误?

javascript - 通过 https nginx 网站 http 到 Node 服务器

express - DNS 预取如何危害安全/隐私?

php - 特定 XMLHttpRequest 和 PHP 不允许 POST 405

http - Delphi 突触 TTCPBlockSocket