Ruby Script to PHP


Ruby Script to PHP

我用Ruby编写了下面的脚本。我想知道有没有人能帮我把它转换成PHP。我知道这是一个很大的要求。我希望将ruby脚本转换为PHP curl请求。

请参阅文档链接https://www.sinch.com/docs/rest-apis/api-documentation/#applicationsignedrequest

第一个代码是SAMPLE ruby脚本。而下面的第二个是我自己尝试用PHP编写的内容。没有成功,因为我得到"无效签名错误"。

require "base64"
require "openssl"
require "time"
require "net/http"
require "uri"
require "json"
to = "+4412345678"
message = "Test sms message"
key = "wwwwwwwwwxxxxxxxx" //Key as supplied by sinch.com
secret = "zzzzzzzyyyyyyyyy" // Secret as supplied by sinch.com
body = "{'"message'":'"" + message + "'"}"
timestamp = Time.now.iso8601
http_verb = "POST"
path = "/v1/sms/" + to
scheme = "Application"
content_type = "application/json"
digest = OpenSSL::Digest.new('sha256')
canonicalized_headers = "x-timestamp:" + timestamp
content_md5 = Base64.encode64(Digest::MD5.digest(body.encode("UTF-8"))).strip
string_to_sign = http_verb + "'n" + content_md5 + "'n" + content_type + "'n" + canonicalized_headers + "'n" + path        
signature = Base64.encode64(OpenSSL::HMAC.digest(digest, Base64.decode64(secret), string_to_sign.encode("UTF-8"))).strip
authorization = "Application " + key + ":" + signature
uri = URI.parse("https://messagingApi.sinch.com" + path)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
headers = {"content-type" => "application/json", "x-timestamp" => timestamp, "authorization" => authorization}
request = Net::HTTP::Post.new(uri.request_uri)
request.initialize_http_header(headers)
request.body = body
puts JSON.parse(http.request(request).body)

下面是我的剧本,我可以接受一个全新的剧本。我是一个超级红宝石新手。请帮忙。

$to="+4412345678";
$text="Hello there test message";
$curl_post_data = array(
    'Message' => $text
);
$curl_post_data=json_encode($curl_post_data);
$timestamp=date("c");
$key = "wwwwwwwwwwwxxxxxxxxxxx";
$secret = "zzzzzzzzzzzzyyyyyyyyyyy";
$http_verb="POST";
$path = "/v1/sms/".$to."";
$scheme = "Application ";
$content_type = "application/json";
$canonicalized_headers = "x-timestamp:".$timestamp."";
$content_md5=base64_encode( md5($curl_post_data,true) );
$string_to_sign = array(
'http_verb' => $http_verb, 
'content_md5' => $content_md5,
'content_type' => $content_type,
'canonicalized_headers' =>$canonicalized_headers
);      
$signature = hash_hmac("sha256", $secret,json_encode($string_to_sign));
$authorization = "".$scheme."".$key.":".$signature."";
$service_url = 'https://messagingapi.sinch.com/v1/sms/'.$to.'';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=UTF-8', 'x-timestamp: '.$timestamp.'','authorization: '.$authorization.''));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>

任何帮助都可以,无论何时遇到问题,Stackoverflow都是一个有用的资源,我希望我的问题也能帮助其他人。

我开始工作了,不得不重写一些东西。不管怎样,这里有一个适用于任何可能需要它的人的工作代码。

<?php
$to="+4412345678";
$text2="Test sms message from PHP";
$curl_post_data = array(
    'message' => $text2
);
$timestamp=date("c");
$key = "wwwwwwwwwwwwwxxxxxxxxxxxxxxx";
$secret ="zzzzzzzzzzzzyyyyyyyyyyyyyy";
$http_verb="POST";
$path = "/v1/sms/".$to."";
$scheme = "Application";
$content_type = "application/json";
$canonicalized_headers = "x-timestamp:".$timestamp."";
$content_md5=md5(json_encode($curl_post_data),true);
$string_to_sign ="".$http_verb."'n".$content_md5."'n".$content_type."'n".$canonicalized_headers."'n".$path."'n";    
$signature = hash_hmac("sha256", base64_encode($secret),utf8_encode($string_to_sign) true);
$signature=base64_encode($signature);
$authorization = "".$scheme."".$key.":".$signature."";

$curl_post_data=json_encode($curl_post_data);
$service_url = 'https://messagingapi.sinch.com/v1/sms/'.$to.'';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=UTF-8','x-timestamp:'.$timestamp.'','authorization:'.$authorization.''));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
?>

完全可工作的代码。此外,感谢"Stephen"从Ruby到PHP的基本移植。它为我节省了很多时间。一些注意事项:

  • 密钥应按原样复制自sinch帐户的应用程序菜单(无任何转换)
  • 要签名的字符串不应在末尾包含新行。此外,请通过设置注释代码中的默认值来检查每个更改
  • 在PHP hash_hmac函数中,cody应在secret之前定义(在Ruby中,参数应按其他顺序定义)
  • 如果实际的标头将包含字符集定义,那么string_to_sign也应该包含它。我已经把它们全部删除了。

    $key     = 'key';
    $secret  = 'secret';
    $message = 'your message';
    $phone   = '+000000000000';
    $body = json_encode(array('message'=>$message));
    $timestamp = date("c");
    // {{{ test values for checking code (from docs)
    /*
        $phone="+46700000000";
        $key = "5F5C418A0F914BBC8234A9BF5EDDAD97";
        $secret ="JViE5vDor0Sw3WllZka15Q==";
        $timestamp='2014-06-04T13:41:58Z';
        $body = '{"message":"Hello world"}';
    */
    // result:
    //      content-md5 should be 'jANzQ+rgAHyf1MWQFSwvYw=='
    //      signature should be 'qDXMwzfaxCRS849c/2R0hg0nphgdHciTo7OdM6MsdnM='
    // }}}
    $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;
    $curl_post_data=json_encode($curl_post_data);
    $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, $curl_post_data);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $curl_response = curl_exec($curl);
    // @todo: checking response / working with results
    curl_close($curl);