2016-09-08 15 views
-2

C#のコードは次のようになります。PHP SOAP POSTリクエストにC#SOAP POSTリクエストを翻訳

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

:成功wihtout PHPで、

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

そして、私の努力のこれまでのところ、他の多くのバリエーションの中でC#コードは動作しますが、PHPで動作させることはできません。どんなアイデアも高く評価されます。ありがとう。

+0

* *正確に*動作していませんか?あなたの研究努力を分かち合いましょう。コードの詳細、実行しようとしていること、発生している問題をさらに説明してください。数十〜数百行のコードでエラーを見つけるのは難しいです。 [*最小、完全、および検証可能な例を作成する方法](http://stackoverflow.com/help/mcve)および[どうすれば良い質問をしますか?](http://stackoverflow.com)を参照してください。/help/how-to-ask)。 –

+0

ありがとう、C#からPHPへの純粋な翻訳に興味がありました。 utf8_encode($ soapRequest)という問題を解決したSOAPリクエストをutf8でエンコードしていました。 – suthers

答えて

0

翻訳では、私はutf8エンコーディング部分がありませんでした。以下の行を追加すると問題が解決し、リクエストが正常に機能します:

$soapUtf8Request = utf8_encode($samlMessage);