2016-11-09 95 views
1

我嘗試使用API發送短信。我已經按照從這個例子的代碼,而是轉換爲GuzzleLaravel Guzzle Http Auth不工作

實現的例子在PHP的POST REST請求

<?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) 
{ 
// 
} 

我與狂飲代碼:

$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」}}

什麼我收到錯誤?

+0

您正在使用哪個guzzle版本? – clinical

+0

「guzzlehttp/guzzle」:「^ 6.2」,這是在composer.json中 – EBuzila

+0

您的拼圖要求看起來不錯。你確定你的簽名參數設置正確嗎?我不確定是否time()被Zend_Date使用。也許嘗試$ now = new DateTime(); $ nonce = $ now-> getTimestam(); – clinical

回答

1

我能夠通過將$url更改爲/prepaid/message來解決此問題。

相關問題