2016-11-09 4 views
1

Ich versuche, eine API zum Senden von SMS verwenden. Ich habe den Code aus diesem Beispiel gefolgt, sondern umgewandelt Guzzle:Laravel Guzzle Http Auth nicht arbeiten

Implementierung Beispiel in PHP für POST Rast Anfrage

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

Mein Code mit 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]); 

aber immer noch nicht funktioniert ; erhalte ich diesen Fehler:

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

{ "Fehler": { "Code": 268435459, "message": "IDS_App_Controller_Rest_Message__E_INVALID_REQUEST_DATA_WRONG_SIGNATURE"}}

Was ich falsch bin immer ??

+0

welche guzzle version verwenden Sie? – clinical

+0

"guzzehlhttp/guzzle": "^ 6.2", das ist in composer.json – EBuzila

+0

Ihre guzzle Anfrage sieht gut aus. Sind Sie sicher, dass Ihre Signaturparameter richtig eingestellt sind? Ich bin mir nicht sicher, ob time() von Zend_Date benutzt wird. Vielleicht probiere $ now = new DateTime(); $ nonce = $ jetzt-> getTimestam(); – clinical

Antwort

1

Ich konnte es lösen, indem ich $url zu /prepaid/message änderte.