doukekui0914 2016-09-08 06:02
浏览 81

将C#SOAP Post请求转换为PHP SOAP Post请求

The C# code looks like this:

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;
    }

And my effort so far, among many other variations, in PHP wihtout success:

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."
");
        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);
    }
}

The C# code works, but I can't get it to work in PHP. Any ideas will be greatly appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • dongshang6790 2016-09-11 10:41
    关注

    In the translation I was missing the utf8 encoding part. The addition of the line below solved my problem and the request now works:

    $soapUtf8Request = utf8_encode($samlMessage);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)