0

我正在尝试使用 curl 在 php 中将 saml 文件 httpost 用于 sso 目的,但 xml 内容需要编码为字节 utf8,然后是 base64(这是我发布到的公司的要求)所以他们给了我这块 c#,他们告诉我将它转换为 php 这是我到目前为止的 php curl saml 帖子:


<?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: \n";
echo $info['http_code'] . " " . $http_codes[$info['http_code']];
}
}

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

?>

给出的 C# 代码

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

如果我的代码有误,请告诉我。谢谢

4

0 回答 0