现有成功python接口代码样例,要求改成php.经过多天努力,还是不能成功,代码如下:
python:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Language Version: 2.7+
# Last Modified: 2016-08-08 15:22:37__all__ = []
__author__ = "aaa(email:[email protected] tel:aaa)"
__version__ = "0.0.1"import json
import uuid
import random
import requests
import base64
import hashlib
import datetimedef attach_header(msg):
    '''附加校验信息到msg消息中'''
    msg.pop('sign', None)
    app_key = msg['appkey']
    # app_id = find_appid(app_key)
    app_id = 'L9HASRNCM0IQ'  # XXX FOR TEST
    nonce_len = 16
    try:
        with open('/dev/urandom', 'rb') as fd:
            nonce = fd.read(nonce_len)
    except:
        # 没找到硬件随机数发生器, 使用系统自带的随机数函数
        try:
            nonce = os.urandom(nonce_len)
        except:
        # 没找到系统随机数发生器, 使用伪随机数函数
            nonce = bytes(random.randint(0, 255) for i in range(nonce_len))
    # nonce = nonce.hex()
    nonce = base64.b85encode(nonce).decode('utf8')
    created = datetime.datetime.now(datetime.timezone.utc)
    created = created.isoformat()
    result = {
        'nonce': nonce,
        'ts': created,
        }
    msg.update(result)
    data = sorted(msg.items(), key=lambda x:x[0])
    data = ["{}={}".format(*i) for i in data]
    data = ','.join(data)
    # print('data:', data)
    data = hashlib.sha256(data.encode('utf8'))
    data.update(app_id.encode('utf8'))
    sign = base64.b64encode(data.digest()).decode('utf8')
    msg.update({'sign': sign})
    return msgdef csms(phoneNun,txt):
    msg = {'ms': phoneNun,
            'tid':'10073000',
           'appkey': 'hwh0',
           'ct': txt
           }    msg = attach_header(msg)
    print('req:', msg)
    r = requests.post('http://*:*/csms', json=msg)
    print(r.json())
    print(r.reason)
    print('------------------------------------------')
if __name__=='__main__':    csms('13888888888','短信进程挂断且重启失败')
PHP:<?php
function attach_header($msg) { $app_key = $msg['appkey'];
$app_id = 'L9HASRNCM0IQ'; # XXX FOR TEST
$nonce_len = 16;
$nonce = create_nonce(16);
$created = create_created();
$msg['nonce'] =$nonce;
echo "--------------".$nonce."----------------";
$msg['tid'] = "10073000";
$msg['ts'] = $created;
$aaa = "";
foreach ($msg as $key => $value) {
$aaa.= $key . "=" . $value . ",";
//    echo  $key."=".$value."</br>";

}
$data = substr($aaa, 0, -1);
//$aaa = "appkey=hwh0,ct=短信进程挂断且重启失败,ms=18600699809,nonce=zzzP~sjPT@gRkz4?4b^l,tid=10073000,ts=2016-12-19T09:43:46.104064+00:00";
//echo $aaa."</br>";
//$aaa = utf8_encode($aaa);
 //echo "aaaaa=".$aaa;
//$aaa = utf8_encode($aaa);

echo "ccccccccccccccccccccccccccccc</br>";
// $data = utf8_encode($data);
// echo $data;
//echo bin2hex($aaa);
echo "</br>ccccccccccccccccccccccccccccc";
$sign = utf8_decode(base64_encode(hash_hmac('sha256', utf8_encode($data), utf8_encode($app_id), true)));

// echo "--------------".$msgs."----------------";
// $msgs = base64_decode($msgs);
// echo "1111111111111".$msgs."1111111111111111111";
// $sign = $msgs ;
// $sign = "EEojJMrextWbkvmj/0b+LsqyKldE1o30zoFLpLnAb0A=";
// echo base64_encode($msgs);
// $bbb=$aaa.",sign=".$sign;
$msg['sign'] = $sign;
//    data = hashlib.sha256(data.encode('utf8'))
//    data.update(app_id.encode('utf8'))
//    sign = base64.b64encode(data.digest()).decode('utf8')
//   msg.update({'sign': sign})
foreach ($msg as $key => $value) {
$aaa.= $key . "=" . $value . ",";
// echo  $key."=".$value."</br>";

}
return $msg;
}
function create_nonce($pw_length = 8) {
$randnonce = '';
for ($i = 0;$i < $pw_length;$i++) {
$randnonce.= chr(mt_rand(33, 126));
}
//return $randnonce;
//return base64_encode("zzzP~sjPT@gRkz4?4b^l");
return "zzzP~sjPT@gRkz4?4b^l";
}
function create_created($pw_length = 8) {
$mytime = time();
date_default_timezone_set("UTC");
$created = date('c');
//return $created;
return "2016-12-19T09:43:46.104064+00:00";
}
// $pw_length = 6
//echo create_password(6);
function csms($phoneNun, $txt) {
$msg = array("appkey" => "hwh0", "ct" => '短信进程挂断且重启失败', "ms" => '18600699809',
//      "tid"=>"10073000",
);

$msg = attach_header($msg);// foreach ($msg as $key => $value) {
// echo  $key."=".$value."</br>";

// }
return $msg;
// echo $msg;

};
function post($url, $post_data = '', $timeout = 5) { //curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
if ($post_data != '') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, false);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
return $file_contents;

}
//echo "11111111111111111111111</br>";
$csmsa = csms('13888888888', '短信进程挂断且重启失败');
//echo gettype($csmsa);
// foreach ($csmsa as $key => $value) {
// echo  $key."=".$value."</br>";
//
// }foreach($csmsa as $key=>$value)
//echo $key."=>".$value;
json_encode($csmsa);$ccc=post("http://*:*/csms",json_encode($csmsa));
//echo "____________________________</br>";
//echo $ccc;
//echo $csms;
// hoVbePfdCDc201We8BKbfujy24Pxa1MkzEM795R0+I4=
// EEojJMrextWbkvmj/0b+LsqyKldE1o30zoFLpLnAb0A=?>