dongweizhen2009 2014-02-18 15:55
浏览 677

使用CURL调用SOAP WSDL Web服务

I'm trying to call a SOAP webservice using CURL and it is not working. This is the code I have used to invoke webservice using soap client

 (object)$ProgramClient = new SoapClient('https://mywebserviceurl?wsdl',array(
'login' => "username", 'password' => "password",
'location' => 'https://someotherurl'
));

(object)$objResult1 = $ProgramClient->GetEvents(array (
'EventsRequest' => array (
'Source' => array (
'SourceId' => 'SOURCEID', 'SystemId' => 'SYTEMID')
 )));

 $event_info = $objResult1->EventsResponse;

This is working fine and I converted this call using CURL and it stopped working. Here is my code using CURL

      $credentials = "username:password"; 
      $url = "https://mywebserviceurl?wsdl";
      $body = '<?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>
                            <GetEvents xmlns="https://umywebserviceurl?wsdl"> 
                             </GetEvents >
                          </soap:Body>
                        </soap:Envelope>'; 
        $headers = array( 
'Content-Type: text/xml; charset="utf-8"', 
'Content-Length: '.strlen($body), 
'Accept: text/xml', 
'Cache-Control: no-cache', 
'Pragma: no-cache', 
'SOAPAction: "GetEvents"'
); 

  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
   curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

  // Stuff I have added
 curl_setopt($ch, CURLOPT_POST, true); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
 curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
 curl_setopt($ch, CURLOPT_USERPWD, $credentials);

  $data = curl_exec($ch);


  echo '<pre>';
  print_r($data);

Could anyone tell me what I'm missing here..

Regards Jayesh

  • 写回答

1条回答 默认 最新

  • dry0106 2016-03-17 05:37
    关注

    Its basic example what you need and used to call SOAP webservice using CURL You will also used extra parameters which you are used in your web service call.

    $soapDo = curl_init();
    
    curl_setopt($soapDo, CURLOPT_URL, "[productionURL]"); // Used "[testingURL]" in testing
    
    curl_setopt($soapDo, CURLOPT_USERPWD, "[username]:[password]");
    
    curl_setopt($soapDo, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    
    curl_setopt($soapDo, CURLOPT_CONNECTTIMEOUT, 10);
    
    curl_setopt($soapDo, CURLOPT_TIMEOUT, 10);
    
    curl_setopt($soapDo, CURLOPT_RETURNTRANSFER, true);
    
    curl_setopt($soapDo, CURLOPT_FOLLOWLOCATION, true);
    
    curl_setopt($soapDo, CURLOPT_SSL_VERIFYPEER, false);
    
    curl_setopt($soapDo, CURLOPT_SSL_VERIFYHOST, false);
    
    curl_setopt($soapDo, CURLOPT_POST, true );
    
    curl_setopt($soapDo, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // The system running this script must have TLS v1.2 enabled, otherwise this will fail
    
    curl_setopt($soapDo, CURLOPT_POSTFIELDS, $xml); // This has to be an XML  string matching the Advantex XML requirements from the WSDL.
    
    curl_setopt($soapDo, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-Length: " . strlen($xml)));
    
    $result = curl_exec($soapDo);
    
    $err = curl_error($soapDo);
    
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥50 成都蓉城足球俱乐部小程序抢票