转换c# SOAP Post请求到PHP SOAP Post请求

本文关键字:请求 SOAP Post PHP 转换 | 更新日期: 2023-09-27 18:06:19

c#代码是这样的:

private static string SendSoapRequest(string request, string destinationUrl)
    {
        string soapRequest = String.Format("<?xml version='"1.0'"    encoding='"utf-8'"?>" +
                                           "<soap:Envelope xmlns:soap='"http://schemas.xmlsoap.org/soap/envelope/'" " +
                                           "soap:encodingStyle='"http://schemas.xmlsoap.org/soap/encoding/'">" +
                                           "<soap:Body>{0}</soap:Body></soap:Envelope>", request);
        HttpWebRequest req = (HttpWebRequest) WebRequest.Create(destinationUrl);
        byte[] buffer = Encoding.UTF8.GetBytes(soapRequest);
        req.Method = "POST";
        req.ContentType = "application/soap+xml";
        req.ContentLength = buffer.Length;
        req.CookieContainer = new CookieContainer(); // enable cookies
        req.Referer = "localhost";
        req.Credentials = CredentialCache.DefaultCredentials;
        Stream reqst = req.GetRequestStream(); // add form data to request stream
        reqst.Write(buffer, 0, buffer.Length);
        reqst.Flush();
        reqst.Close();
        HttpWebResponse res = (HttpWebResponse) req.GetResponse();
        Stream responseStream = res.GetResponseStream();
        if (responseStream != null)
        {   
            StreamReader sr = new StreamReader(responseStream);
            string response = sr.ReadToEnd();
            return response;
        }
        return string.Empty;
    }

和我的努力到目前为止,在许多其他的变化中,PHP没有成功:

public static function sendSoapCurl($samlMessage, $destination, $action) {
    $headers = array(
        "Content-type: application/soap+xml;charset='"utf-8'"",
        "Content-length: ".strlen($samlMessage),
    );
    if (isset($action)) {
        $headers[] = "SOAPAction: $action";
    }
    // $samlMessage = utf8_encode($samlMessage);
    // PHP cURL  for https connection with auth
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_URL, $destination);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $samlMessage); // the SOAP request
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // get soap response
    $soapresponsexml = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if ($httpCode != 200) {
        print_r("Status code: ".$httpCode."'n");
        print_r($soapresponsexml);exit();
    }
    if ($soapresponsexml === null || $soapresponsexml === "") {
        throw new 'Exception('Empty SOAP response, check peer certificate.');
    }
    try {
        $dom = new DOMDocument();
        $dom = OneLogin_Saml2_Utils::loadXML($dom, $soapresponsexml);
    } catch (RuntimeException $e) {
        throw new 'Exception('Not a SOAP response.', 0, $e);
    }
    $soapfault = self::getSOAPFault($dom);
    if (isset($soapfault)) {
        throw new 'Exception($soapfault);
    }
}

c#代码工作,但我不能让它在PHP中工作。任何想法都将不胜感激。谢谢。

转换c# SOAP Post请求到PHP SOAP Post请求

在翻译中,我错过了utf8编码部分。添加下面的行解决了我的问题,请求现在可以工作了:

$soapUtf8Request = utf8_encode($samlMessage);