简体   繁体   English

使用PHP使用此SOAP服务的正确方法

[英]Right way to consume this SOAP service with PHP

I'm trying to consume this webservice with php. 我正在尝试使用php消耗此webservice。 First I need to call the API method to make login. 首先,我需要调用API方法进行登录。 Here's my code: 这是我的代码:

     try {
    $opts = array(
        'http' => array(
            'user_agent' => 'PHPSoapClient'
        )
    );
    $context = stream_context_create($opts);

    $wsdlUrl = 'http://172.20.2.18:1024/ADInterface/services/ModelADService?wsdl';

    $soapClientOptions = array(
        'stream_context' => $context,
        'cache_wsdl' => WSDL_CACHE_NONE);

    $checkVatParameters = array(
        'user'=>'WebService',
        'pass'=>'WebService',
        'lang'=>'es_CL',
        'ClientID'=>'1000000',
        'RoleID'=>'1000014',
        'OrgID'=>'1000000',
        'WarehouseID'=>'1000001',
        'stage'=>'0');

    $modelCrud = array(
        'serviceType' => 'WSBPartner',
        'TableName' => 'XX_WEB_WSBPartner',
        'RecordID' => 0,
        'Filter' => '',
        'Action' => 'Read',
        'DataRow' => array(
               'field' => array(
                   'type' => 'integer',
                   'column' => 'C_BPartner_ID',
                   'lval' => '',
                   'disp' => '',
                   'edit' => '',
                   'error' => '',
                   'errorVal' => '',
                   'val' => 1000643,
               )
           )
      );

    $client = new SoapClient($wsdlUrl, $soapClientOptions);

    $result = $client->queryData(
        'ModelCRUDRequest', array(
            'ModelCRUD' => $modelCrud,
            'ADLoginRequest' => $checkVatParameters,
        )
    );

    print_r($result);
}
catch(Exception $e) {
    echo $e->getMessage();
}

here's the error: Parameter ModelCRUDRequest does not exist! 这是错误:参数ModelCRUDRequest不存在!

I want to be able to call those method like queryData . 我希望能够调用那些方法,例如queryData I hope I explained well, this is the first time I'm using webservices. 我希望我能很好地解释,这是我第一次使用Web服务。

Sometimes the SoapClient is just a "broken tool". 有时, SoapClient只是“损坏的工具”。 I would try to use curl or Guzzle as HTTP client and build the SOAP request manually. 我会尝试将curl或Guzzle用作HTTP客户端,并手动构建SOAP请求。 I have used SoapUI to generate a sample SOAP request according to the WSDL . 我已经使用SoapUI根据WSDL生成示例SOAP请求。

Example

<?php

// Change the url
$endpoint = 'http://172.20.2.18:1024/ADInterface/services/ModelADService';
$soapMethod = 'queryData';

// Basic Auth (optional)
$soapUser = ''.
$soapPassword = '';

// Created with SoapUI
$soap = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:adin="http://3e.pl/ADInterface">
   <soapenv:Header/>
   <soapenv:Body>
      <adin:queryData>
         <adin:ModelCRUDRequest>
            <adin:ModelCRUD>
               <adin:serviceType>?</adin:serviceType>
               <adin:TableName>?</adin:TableName>
               <adin:RecordID>?</adin:RecordID>
               <adin:Filter>?</adin:Filter>
               <adin:Action>?</adin:Action>
               <!--Optional:-->
               <adin:DataRow>
                  <!--Zero or more repetitions:-->
                  <adin:field type="?" column="?" lval="?" disp="?" edit="?" error="?" errorVal="?">
                     <adin:val>?</adin:val>
                     <!--Optional:-->
                     <adin:lookup>
                        <!--Zero or more repetitions:-->
                        <adin:lv val="?" key="?"/>
                     </adin:lookup>
                  </adin:field>
               </adin:DataRow>
            </adin:ModelCRUD>
            <adin:ADLoginRequest>
               <adin:user>?</adin:user>
               <adin:pass>?</adin:pass>
               <adin:lang>?</adin:lang>
               <adin:ClientID>?</adin:ClientID>
               <adin:RoleID>?</adin:RoleID>
               <adin:OrgID>?</adin:OrgID>
               <adin:WarehouseID>?</adin:WarehouseID>
               <adin:stage>?</adin:stage>
            </adin:ADLoginRequest>
         </adin:ModelCRUDRequest>
      </adin:queryData>
   </soapenv:Body>
</soapenv:Envelope>';

$headers = array(
    'Content-type: text/xml;charset="utf-8"',
    'Accept: text/xml',
    'Cache-Control: no-cache',
    'Pragma: no-cache',
    // Maybe change this url
    'SOAPAction: ' . $endpoint . '/' . $soapMethod,
    'Content-length: ' .strlen($soap),
); 

// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
// Basic Auth (optional)
curl_setopt($ch, CURLOPT_USERPWD, $soapUser. ':' .$soapPassword);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $soap);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Invoke request
$response = curl_exec($ch);
if($response === false) {
    echo 'HTTP error: ' . curl_error($ch);
    exit;
}

$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$xml = trim(substr($response, $headerSize));

curl_close($ch);

// Convert response to DOM document
$dom = new DOMDocument();
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml);

echo $dom->saveXML();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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