Trouble with authorization request

妖精的绣舞 提交于 2019-12-02 11:30:42

I post the code that work to me.

$phone ="+460000000" //Put the destination number here
$key = "XXXXXX";    
$secret = "XXXXXX"; 
$message = $first_name . ', thanks you for signing in. We will text you when we\'re ready for you';
$phone   = $phone_number;

$body = json_encode(array('From' => $rented_number, 'Message'=>$message, ));

$timestamp = date("c");

$path                  = "/v1/sms".$phone;
$content_type          = "application/json";
$canonicalized_headers = "x-timestamp:" . $timestamp;

$content_md5 = base64_encode( md5( utf8_encode($body), true ));

$string_to_sign =
    "POST\n".
    $content_md5."\n".
    $content_type."\n".
    $canonicalized_headers."\n".
    $path;

$signature = base64_encode(hash_hmac("sha256", utf8_encode($string_to_sign), base64_decode($secret), true));
$authorization = "Application " . $key . ":" . $signature;

$service_url = 'https://messagingapi.sinch.com'.$path;
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'content-type: '.$content_type,
    'x-timestamp:' . $timestamp,
    'authorization:' . $authorization
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
// @todo: checking response / working with results
curl_close($curl);

Thank drew010 to your help, was amazing!!

From their API documentation, I was able to create this code which generates a signature that matches what they have. You should be able to modify it to get it working with your code.

<?php

$applicationKey    = '5F5C418A0F914BBC8234A9BF5EDDAD97';
$applicationSecret = 'JViE5vDor0Sw3WllZka15Q==';

$ts        = '2014-06-04T13:41:58Z';
$resource  = '/v1/sms/+46700000000';
$timestamp = new DateTime($ts);

$body = [
    'message' => 'Hello world',
];

$message = json_encode($body);

$md5 = base64_encode(md5($message, true));

$timestamp = $ts;

$stringToSign = "POST\n" .
                $md5 . "\n" .
                "application/json\n" .
                'x-timestamp:' . $timestamp . "\n" .
                $resource;

$signature = base64_encode(
                 hash_hmac('sha256',
                           $stringToSign,
                           base64_decode($applicationSecret),
                           true
                 )
              );

echo $md5 . "\n" . $signature . "\n";
// jANzQ+rgAHyf1MWQFSwvYw==
// qDXMwzfaxCRS849c/2R0hg0nphgdHciTo7OdM6MsdnM=
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!