dongzuan4491 2019-04-24 16:21
浏览 204

如何在php中创建SOAP请求,就像在SoapUI中一样?

I am all new to php and all lost in doing a soap request. I have this other question How to send a SOAP request in javascript, like in SoapUI, which I have just got answered, but I have now decided to work in php instead of Node.js. As I want my php code to do the exact same, I will repeat my earlier question down below:

I have this WSDL site that I need to get some answers from. I have figured out how to do this in SoapUI, but I have no idea how to do it in php. The request that I am sending in soapUI looks like this:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:uni="https://uni-login.dk">
   <soap:Header/>
   <soap:Body>
      <uni:hentDataAftaler>
         <uni:wsBrugerid>?</uni:wsBrugerid>
         <uni:wsPassword>?</uni:wsPassword>
      </uni:hentDataAftaler>
   </soap:Body>
</soap:Envelope>

I also have the wsdl-link: https://wsiautor.uni-login.dk/wsiautor-v4/ws?WSDL

I hope you I have some suggestions and tell me if you need any more information to answer my question :)

The answer for my previous question was the following

    const soapRequest = require('easy-soap-request');
    const url = 'https://wsiautor.uni-login.dk/wsiautor-v4/ws';
    const headers = {
      'Content-Type': 'application/soap+xml;charset=UTF-8',
      'soapAction': 'https://wsiautor.uni-login.dk/hentDataAftaler',
};
// example data
const xml = `
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:uni="https://uni-login.dk">
   <soap:Header/>
   <soap:Body>
      <uni:hentDataAftaler>
         <uni:wsBrugerid>?</uni:wsBrugerid>
         <uni:wsPassword>?</uni:wsPassword>
      </uni:hentDataAftaler>
   </soap:Body>
</soap:Envelope>
`;


// usage of module
soapRequest(url, headers, xml).then(({response: {body, statusCode}}) => {
    console.log(body);
    console.log(statusCode);
}).catch((errorBody) => {
    console.error(errorBody);
});
  • 写回答

2条回答 默认 最新

  • ds000001 2019-04-24 19:23
    关注

    There are at least two extensions you can use to enable that.

    I would recommend the old and good curl extension.

    Another tip is that with Postman you can generate code for HTTP requests using curl or http-request. You can find it in the right up corner, near the send button.

    Certify you install the extension first (in my case, installed using apt):

    sudo apt-get install php-curl
    

    Anyway, I believe you can use this code to accomplish it:

    <?php
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_PORT => "80",
      CURLOPT_URL => "https://wsiautor.uni-login.dk/wsiautor-v4/ws",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_SSL_VERIFYPEER => false,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" 
      xmlns:uni=\"https://uni-login.dk\">
        <soapenv:Header/>
         <soapenv:Body>
           <uni:hentDataAftaler>
             <uni:wsBrugerid>?</uni:wsBrugerid>
             <uni:wsPassword>?</uni:wsPassword>
           </uni:hentDataAftaler>
          </soapenv:Body>
        </soapenv:Envelope>",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/soap+xml;charset=UTF-8",
        "soapAction: https://wsiautor.uni-login.dk/hentDataAftaler"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R