简体   繁体   中英

PHP - send SOAP web service with method parameters

I want to write a PHP code to send authentication and data to the web service. Please see the web service SOAP XML structure below:

This is a URL to access web API https://portal.xyzonline.com/API/ABCAPIServer.asmx?WSDL

I will really appreciate if you can help me to just write basic code.

This is what I am trying to do:

PHP Code

$client = new SoapClient("https://portal.xyzonline.com/API/ABCAPIServer.asmx?WSDL");
$params = array(
'addCaseReq' => array(
    'DlcpmCase' => array(
        'CaseID' => '1234',
        'CustomerID' => '222',
        'PatientFirst' => 'John'
        ),
     'Auth' => array(
        'AppName' => 'appname',
        'UserName' => 'username',
        'Password' => 'password'
        )
     )
   );
try { 
    $responsetest = $client->AddCase(new SoapParam(array($params)));
} catch(SoapFault $fault) { 
    print_r($fault); 
}

XML

<wsdl:types>
 <s:schema>
  <s:element name="AddCase">
   <s:complexType>
    <s:sequence>
     <s:element minOccurs="0" maxOccurs="1" name="addCaseReq" type="tns:ABCAPIAddCaseRequest"/>
    </s:sequence>
   </s:complexType>
  </s:element>
  <s:complexType name="ABCAPIAddCaseRequest">
   <s:complexContent mixed="false">
    <s:extension base="tns:ABCAPIRequest">
     <s:sequence>
      <s:element minOccurs="0" maxOccurs="1" name="AbcCase" type="tns:ABCCase"/>
     </s:sequence>
    </s:extension>
   </s:complexContent>
  </s:complexType>
  <s:complexType name="ABCAPIRequest" abstract="true">
   <s:sequence>
    <s:element minOccurs="0" maxOccurs="1" name="Auth" type="tns:ABCAPIAuthentication"/>
   </s:sequence>
  </s:complexType>
  <s:complexType name="ABCAPIAuthentication">
   <s:sequence>
    <s:element minOccurs="0" maxOccurs="1" name="AppName" type="s:string"/>
    <s:element minOccurs="0" maxOccurs="1" name="UserName" type="s:string"/>
    <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string"/>
   </s:sequence>
  </s:complexType>
  <s:complexType name="ABCCase">
   <s:sequence>
    <s:element minOccurs="0" maxOccurs="1" name="CaseID" type="s:string"/>
    <s:element minOccurs="1" maxOccurs="1" name="CaseNumber" type="s:int"/>
    <s:element minOccurs="0" maxOccurs="1" name="CustomerID" type="s:string"/>
    <s:element minOccurs="0" maxOccurs="1" name="PatientFirst" type="s:string"/>
   </s:sequence>
  </s:complexType>
 </s:schema>
</wsdl:types>

Screenshot of WCFStorm

I am using WCFStorm and everything is working fine. Please see the screenshot:

在此处输入图片说明

If you need to write a php script to send the request, I would strongly advise you to use a WSDL to php generator such as the PackageGenerator project. It will really ease you the construction of the data to send and the handling of the response. You won't have to use directly the native SoapClient, SoapVar, SoapParam classes as you'll only deal with PHP classes that match the required parameters. The PHP objects you'll construct will be mapped to generate the XML request by the SoapClient class. With the generated package, you'll then need to have a good IDE such as Eclipse PDT or PHP Storm to know the parameters and values to use with the auto completion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM