简体   繁体   中英

Soap Call in PHP with Multiple Elements - Need to Pass in As Array?

I am trying to send multiple repeated elements via a Soap Call. I tried building an array and sending, but it would process only the first element. The following post was helpful as I adapted the code using SoapVar SoapVar/Param and nested, repeated elements in SOAP . The issue that I am facing now, however, is that when I try to send off my soap request, the soap call needs the request as an array, and the following code gets a failure from the soap server. The WSDL file I am working with is at https://ecomapi.networksolutions.com/soapservice.asmx?wsdl

I have replaced the header elements for security (my name, my cert number, my token) - but other than that, the complete code is below. Any thoughts on what I am doing wrong here?

<?php

$ns = 'urn:networksolutions:apis';
$header->Application = 'my name';
$header->Certificate = 'my cert number';
$header->UserToken = 'my token';

$credentials = new SOAPHeader($ns, "SecurityCredential", $header);

$client = new SoapClient('https://ecomapi.networksolutions.com/soapservice.asmx?wsdl',
                array('soap_version' => SOAP_1_1 ,
                                'trace' => 1));
 $array1=array();
 $array1[]=new SoapVar("9",XSD_STRING,null,null,'ProductId');
 $array1[]=new SoapVar("500",XSD_STRING,null,null,'QtyInStock');
 $soap1 = new SoapVar($array1, SOAP_ENC_OBJECT, null, null, "Inventory");
 $interim = array($soap1);
 $test = array();
 $test[] = new SoapVar($interim, SOAP_ENC_OBJECT, null, null, "UpdateInventoryRequestList");


 $array2=array();
 $array2[]=new SoapVar("10",XSD_STRING,null,null,'ProductId');
 $array2[]=new SoapVar("500",XSD_STRING,null,null,'QtyInStock');
 $soap2 = new SoapVar($array2, SOAP_ENC_OBJECT, null, null, "Inventory");
 $interim2 = array($soap2);
 $test[] = new SoapVar($interim2, SOAP_ENC_OBJECT, null, null, "UpdateInventoryRequestList");

 $submit1 = array($test);
 $submit = new SoapVar($submit1, SOAP_ENC_OBJECT, null, null, "PerformMultipleRequest");
 $final_submit = array($submit);

 $result = $client->__soapCall("PerformMultiple", $final_submit, NULL, $credentials);

 echo "REQUEST:\n" . $client->__getLastRequest() . "\n";   // gets last SOAP request
 echo "RESPONSE:\n" . $client->__getLastResponse() . "\n"; // gets last SOAP respone

?>

Why so complicated?

PHPs SoapClient does all that stuff automatically for you:

$array = array();
$array[] = new Inventory(50, 100);
$array[] = new Inventory(51, 10);
$client = new SoapClient('https://ecomapi.networksolutions.com/soapservice.asmx?wsdl',
                array('soap_version' => SOAP_1_1, 'trace' => 1));
$client->PerformMultiple($array);

The Inventory needs to be something like:

class Inventory
{
    public $ProductId;
    public $QtyInStock;

    public function __construct($id, $qty)
    {
        $this->ProductId = $id;
        $this->QtyInStock = $qty;
    }
}

I'm using many Soap-Services that way. Only the other way around it is a little tricky, because parameters as arrays are not directly accessible, eg if you expect $param to be ObjectClass[] you get that array from $param->ObjectClass.

regards

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