phpアリクラウドメールインタフェースPOSTとGET方式アクセス成功demo

5057 ワード

]*>([\\x00-\\xFF]*)/";  
    if(preg_match_all($reg, $xml, $matches)){  
        $count = count($matches[0]);  
        for($i = 0; $i < $count; $i++){  
        $subxml= $matches[2][$i];  
        $key = $matches[1][$i];  
            if(preg_match( $reg, $subxml )){  
                $arr[$key] = xml_to_array( $subxml );  
            }else{  
                $arr[$key] = $subxml;  
            }  
        }  
    }  
    return @$arr;  
}  

if(empty($mobile)){  
    //exit('        ');  
}  
  
if(empty($_SESSION['send_code']) or $send_code!=$_SESSION['send_code']){  
    //         
    //exit('    ,        ');  
}  
  
function percentEncode($str)  
{  
    //   urlencode   , "+","*","%7E"      ECS API         
    $res = urlencode($str);  
    $res = preg_replace('/\+/', '%20', $res);  
    $res = preg_replace('/\*/', '%2A', $res);  
    $res = preg_replace('/%7E/', '~', $res);  
    return $res;  
}  

function computeSignature($parameters, $accessKeySecret)  
{  
    //    Key         
    ksort($parameters);  
    //             
    $canonicalizedQueryString = '';  
    foreach($parameters as $key => $value)  
    {  
    $canonicalizedQueryString .= '&' . percentEncode($key)  
        . '=' . percentEncode($value);  
    }  
    //              stringToSign  
    $stringToSign = 'GET&%2F&' . percentencode(substr($canonicalizedQueryString, 1));  
    //echo "
".$stringToSign."
"; // , accessKeySecret '&' $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true)); return $signature; } // GMT date_default_timezone_set("GMT"); $target = "https://dysmsapi.aliyuncs.com/?"; $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; // ISO8601 $accessKeyId = 'XXXXXXXX'; // Access Key ID $accessKeySecret = 'XXXXXXXXXXXXXXXXX'; // Access Key Secret $ParamString="{\"XXName\":\"test\",\"XXXXType\":\"test\"}"; $data = array( 'AccessKeyId' => $accessKeyId, 'Action' => 'SendSms', 'Format' => 'XML', 'OutId' => '123', 'PhoneNumbers' => '183XXXXXXXX', 'RegionId' => 'cn-hangzhou', 'SignName'=>'XXXX ', 'SignatureMethod' => 'HMAC-SHA1', 'SignatureNonce'=> uniqid(), 'SignatureVersion' => '1.0', 'TemplateCode' => 'SMS_9705XXXX', 'TemplateParam' => $ParamString, 'Timestamp' => date($dateTimeFormat), 'Version' => '2017-05-25' ); // $data['Signature'] = computeSignature($data, $accessKeySecret); // $result = xml_to_array(https_request($target.http_build_query($data))); echo $result['Error']['Code']."--->".$result['Error']['Message']; echo "

".$target . http_build_query($data); ?>

post方式でアクセスするには3つの変更が必要です:1、computeSignature関数内のピンイン文字GETをPOSTに変更します
 //              stringToSign  
$stringToSign = 'POST&%2F&' . percentencode(substr($canonicalizedQueryString, 1));  

2.httpリクエストを送信するPHP関数https_リクエストはPOST方式で送信するように変更されました.
   function https_request($url)  
{  
    $curl = curl_init();
    //     url
    curl_setopt($curl, CURLOPT_URL, 'https://dysmsapi.aliyuncs.com');
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    //               
    curl_setopt($curl, CURLOPT_HEADER, 1);
    //                ,       。
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    //  post    
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $url);
    $data = curl_exec($curl);  
    if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}  
    curl_close($curl);  
    return $data;  
}  

3、httpsを呼び出すリクエストのパラメータはPOSTで転送する値に調整する必要があります
$result = xml_to_array(https_request(http_build_query($data)));  

DONE!