EBuzila
EBuzila

Reputation: 229

Laravel Guzzle Http Auth not work

I try to use an API for sending sms. I've followed the code from this example, but converted to Guzzle:

Implementation example in PHP for POST Rest Request

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

My Code with 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]);

But still not working; I receive this error:

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

{"error":{"code":268435459,"message":"IDS_App_Controller_Rest_Message__E_INVALID_REQUEST_DATA_WRONG_SIGNATURE"}}

What I am getting wrong??

Upvotes: 1

Views: 566

Answers (1)

EBuzila
EBuzila

Reputation: 229

I was able to solve it by changing $url to /prepaid/message.

Upvotes: 1

Related Questions