programing tip

CURL을 사용하는 PHP의 SOAP 요청

itbloger 2020. 11. 20. 08:42
반응형

CURL을 사용하는 PHP의 SOAP 요청


php.net의 SOAP 매뉴얼은 멍청한 편이 아니고 좋은 예를 찾을 수 없었기 때문에 여기에 질문을 게시 할 것입니다.

이렇게 보이도록 PHP SOAP 요청을 생성하려면 어떻게해야합니까?

POST /MySERVER/myWSDLservice.asmx HTTP/1.1
Host: connection.mywebsite.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://connection.mywebsite.com/MySERVER/GetCarType"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <GetCarType xmlns="http://connection.mywebsite.com/MySERVER/">
    <IDNumber>string</IDNumber>
  </GetCarType>
 </soap:Body>
</soap:Envelope>

참고 :

  • 사용자 / 패스 인증이 있습니다.
  • SSL 연결

모든 제안 / 링크 / 예는 대단히 감사합니다.


테스트 및 작동 중!

  • https, 사용자 및 비밀번호 사용

            <?php 
            //Data, connection, auth
            $dataFromTheForm = $_POST['fieldName']; // request data from the form
            $soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
            $soapUser = "username";  //  username
            $soapPassword = "password"; // password
    
            // xml post structure
    
            $xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
                                <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                                  <soap:Body>
                                    <GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your's WSDL URL
                                      <PRICE>'.$dataFromTheForm.'</PRICE> 
                                    </GetItemPrice >
                                  </soap:Body>
                                </soap:Envelope>';   // data from the form, e.g. some ID number
    
               $headers = array(
                            "Content-type: text/xml;charset=\"utf-8\"",
                            "Accept: text/xml",
                            "Cache-Control: no-cache",
                            "Pragma: no-cache",
                            "SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice", 
                            "Content-length: ".strlen($xml_post_string),
                        ); //SOAPAction: your op URL
    
                $url = $soapUrl;
    
                // PHP cURL  for https connection with auth
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    
                // converting
                $response = curl_exec($ch); 
                curl_close($ch);
    
                // converting
                $response1 = str_replace("<soap:Body>","",$response);
                $response2 = str_replace("</soap:Body>","",$response1);
    
                // convertingc to XML
                $parser = simplexml_load_string($response2);
                // user $parser to get your data out of XML response and to display it.
        ?>
    

참고 URL : https://stackoverflow.com/questions/7120586/soap-request-in-php-with-curl

반응형