douyun6781 2013-07-17 19:03
浏览 31

使用php curl发布saml文件并将C#代码转换为php

I am trying to httpost saml file in php using curl for sso purposes but the xml contents need to be encoded into byte utf8 and then base64(that's the requirements of the company i am posting to) so they gave me this piece c# and they told me to convert it to php this is what i get to so far with my php curl saml post:


<?php
$url = "https://my.sandbox.company.com/sso/authenticate.ashx";
$filename = "saml.xml";
$handle = fopen($filename, "r");
$XPost = fread($handle, filesize($filename));
echo urlencode($XPost);
fclose($handle);

$decdata = urlencode($XPost);
$raw = base64_encode($decdata);


$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);


curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 40); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);
curl_setopt($ch, CURLOPT_POST, 1);

$result = curl_exec($ch); // run the whole process
if (empty($result)) {
die(curl_error($ch));
curl_close($ch); // close cURL handler
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
if (empty($info['http_code'])) {
die("No HTTP code was returned");
} 
else {
echo "The server responded: 
";
echo $info['http_code'] . " " . $http_codes[$info['http_code']];
}
}

echo "RESULT: $result"; //contains response from server

?>

The C# code given

string responseStr = doc.OuterXml;
byte[] base64EncodedBytes = Encoding.UTF8.GetBytes(responseStr);
string returnValue = System.Convert.ToBase64String(base64EncodedBytes);

Please let me know if my code is wrong. Thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波
    • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
    • ¥15 钢筋实图交点识别,机器视觉代码
    • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
    • ¥50 400g qsfp 光模块iphy方案
    • ¥15 两块ADC0804用proteus仿真时,出现异常
    • ¥15 关于风控系统,如何去选择
    • ¥15 这款软件是什么?需要能满足我的需求
    • ¥15 SpringSecurityOauth2登陆前后request不一致