Question

I need to make a repeating array for this part of xml as an array, but as you can see there is a repeating item here.

    <orderItems>
      <OrderItemRequest>
        <line_id>int</line_id>
        <product_id>string</product_id>
        <product_variant_id>string</product_variant_id>
        <product_desc>string</product_desc>
        <quantity>int</quantity>
        <variant_desc>string</variant_desc>
        <item_total>decimal</item_total>
      </OrderItemRequest>
      <OrderItemRequest>
        <line_id>int</line_id>
        <product_id>string</product_id>
        <product_variant_id>string</product_variant_id>
        <product_desc>string</product_desc>
        <quantity>int</quantity>
        <variant_desc>string</variant_desc>
        <item_total>decimal</item_total>
      </OrderItemRequest>
    </orderItems>

This is what I had tried to do:

$orderItemRequest = "";

foreach($requestArray as $array) {
    foreach ($array as $key => $val) {
        $orderItemRequest .= $comma."<$key>";

        foreach ($val as $key2 => $val2) {
            $orderItemRequest .= "<$key2>$val2</$key2>";
        }

        $orderItemRequest .= "</$key>";
    }
}

array('carrier_code' => 'UPS',
'dob' => date("Y-m-d", strtotime($dob)),
'creditcard_number' => $CCNumber,
'card_type' => $CCType,
'card_expire_date' => $CCExpireMonth."/".$CCExpireYear,
'CV_Code' => $CCSecurity,
'Customer_Email' => $email,
'orderItems' => $orderItemRequest,
'ChallengeQuestion' => '',
'ChallengeAnswer' => '',
'localeSetting' => '',
'ignoreAddressValidation' => '1',
'auth_code' => '',
'affiliate_code' => '');

I've tried to insert the xml directly as the value of orderItems but get the error: nusoap_client: got wsdl error: phpType is struct, but value is not an array: see debug output for details.

Here is the last bit of the debug info:

2013

-04-27 05:22:42.816864 wsdl: in serializeType: name=orderItems, type=http://phepartnerws/:ArrayOfOrderItemRequest, use=literal, encodingStyle=, unqualified=qualified
value=string(845) "<OrderItemRequest><line_id>0</line_id><product_id>005K</product_id><product_variant_id>005K</product_variant_id><product_desc>TANTRIC AURA MASSAGER</product_desc><variant_desc>TANTRIC AURA MASSAGER</variant_desc><quantity>1</quantity><item_total>24.98</item_total></OrderItemRequest><OrderItemRequest><line_id>1</line_id><product_id>754</product_id><product_variant_id>754</product_variant_id><product_desc>SENSUAL SUBMISSION KIT</product_desc><variant_desc>SENSUAL SUBMISSION KIT</variant_desc><quantity>2</quantity><item_total>31.48</item_total></OrderItemRequest><OrderItemRequest><line_id>2</line_id><product_id>239</product_id><product_variant_id>239</product_variant_id><product_desc>G-SPOT VIBE AND LUBE</product_desc><variant_desc>G-SPOT VIBE AND LUBE</variant_desc><quantity>1</quantity><item_total>14.95</item_total></OrderItemRequest>"
2013-04-27 05:22:42.816888 wsdl: in serializeType: got a prefixed type: ArrayOfOrderItemRequest, http://phepartnerws/
2013-04-27 05:22:42.816904 wsdl: in getTypeDef: type=ArrayOfOrderItemRequest, ns=http://phepartnerws/
2013-04-27 05:22:42.816918 wsdl: in getTypeDef: have schema for namespace http://phepartnerws/
2013-04-27 05:22:42.816937 nusoap_xmlschema: <http://phepartnerws/> in getTypeDef, found complexType ArrayOfOrderItemRequest
2013-04-27 05:22:42.816954 wsdl: in getTypeDef: found type ArrayOfOrderItemRequest
2013-04-27 05:22:42.816968 wsdl: in serializeType: found typeDef
typeDef=array(6) {
  ["name"]=>
  string(23) "ArrayOfOrderItemRequest"
  ["typeClass"]=>
  string(11) "complexType"
  ["phpType"]=>
  string(6) "struct"
  ["simpleContent"]=>
  string(5) "false"
  ["compositor"]=>
  string(8) "sequence"
  ["elements"]=>
  array(1) {
    ["OrderItemRequest"]=>
    array(6) {
      ["minOccurs"]=>
      string(1) "0"
      ["maxOccurs"]=>
      string(9) "unbounded"
      ["name"]=>
      string(16) "OrderItemRequest"
      ["nillable"]=>
      string(4) "true"
      ["type"]=>
      string(37) "http://phepartnerws/:OrderItemRequest"
      ["form"]=>
      string(9) "qualified"
    }
  }
}
2013-04-27 05:22:42.817006 wsdl: in serializeType: uqType: ArrayOfOrderItemRequest, ns: http://phepartnerws/, phptype: struct, arrayType: 
2013-04-27 05:22:42.817022 wsdl: in serializeType: phpType is struct, but value is not an array
2013-04-27 05:22:42.817037 wsdl: in serializeType: returning: 
2013-04-27 05:22:42.817058 wsdl: in serializeType: name=CV_Code, type=http://www.w3.org/2001/XMLSchema:string, use=literal, encodingStyle=, unqualified=qualified
value=string(3) "123"
2013-04-27 05:22:42.817080 wsdl: in serializeType: got a prefixed type: string, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817095 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817115 wsdl: in getTypeDef: type=string, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817131 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817146 wsdl: in serializeType: returning: <CV_Code>123</CV_Code>
2013-04-27 05:22:42.817166 wsdl: in serializeType: name=ChallengeQuestion, type=http://www.w3.org/2001/XMLSchema:string, use=literal, encodingStyle=, unqualified=qualified
value=string(0) ""
2013-04-27 05:22:42.817189 wsdl: in serializeType: got a prefixed type: string, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817203 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817222 wsdl: in getTypeDef: type=string, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817238 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817252 wsdl: in serializeType: returning: <ChallengeQuestion></ChallengeQuestion>
2013-04-27 05:22:42.817273 wsdl: in serializeType: name=ChallengeAnswer, type=http://www.w3.org/2001/XMLSchema:string, use=literal, encodingStyle=, unqualified=qualified
value=string(0) ""
2013-04-27 05:22:42.817294 wsdl: in serializeType: got a prefixed type: string, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817309 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817328 wsdl: in getTypeDef: type=string, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817344 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817359 wsdl: in serializeType: returning: <ChallengeAnswer></ChallengeAnswer>
2013-04-27 05:22:42.817382 wsdl: in serializeType: name=localeSetting, type=http://www.w3.org/2001/XMLSchema:string, use=literal, encodingStyle=, unqualified=qualified
value=string(0) ""
2013-04-27 05:22:42.817404 wsdl: in serializeType: got a prefixed type: string, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817419 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817438 wsdl: in getTypeDef: type=string, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817454 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817469 wsdl: in serializeType: returning: <localeSetting></localeSetting>
2013-04-27 05:22:42.817492 wsdl: in serializeType: name=ignoreAddressValidation, type=http://www.w3.org/2001/XMLSchema:boolean, use=literal, encodingStyle=, unqualified=qualified
value=string(1) "1"
2013-04-27 05:22:42.817514 wsdl: in serializeType: got a prefixed type: boolean, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817529 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817546 wsdl: in getTypeDef: type=boolean, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817561 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817576 wsdl: in serializeType: returning: <ignoreAddressValidation>true</ignoreAddressValidation>
2013-04-27 05:22:42.817596 wsdl: in serializeType: name=auth_code, type=http://www.w3.org/2001/XMLSchema:string, use=literal, encodingStyle=, unqualified=qualified
value=string(0) ""
2013-04-27 05:22:42.817618 wsdl: in serializeType: got a prefixed type: string, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817633 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817651 wsdl: in getTypeDef: type=string, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817667 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817681 wsdl: in serializeType: returning: <auth_code></auth_code>
2013-04-27 05:22:42.817702 wsdl: in serializeType: name=affiliate_code, type=http://www.w3.org/2001/XMLSchema:string, use=literal, encodingStyle=, unqualified=qualified
value=string(0) ""
2013-04-27 05:22:42.817723 wsdl: in serializeType: got a prefixed type: string, http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817738 wsdl: in serializeType: type namespace indicates XML Schema or SOAP Encoding type
2013-04-27 05:22:42.817757 wsdl: in getTypeDef: type=string, ns=http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817773 wsdl: in getTypeDef: do not have schema for namespace http://www.w3.org/2001/XMLSchema
2013-04-27 05:22:42.817787 wsdl: in serializeType: returning: <affiliate_code></affiliate_code>
2013-04-27 05:22:42.817805 wsdl: in serializeType: returning: <oOrderRequest><RequestCredentials><CallerID>livereps</CallerID><Timestamp>2013-04-27T01:22:42-04:00</Timestamp><Token>1/kUrVDZmoYqbj1/PhCE4w==</Token></RequestCredentials><company></company><xml></xml><partnerID>livereps</partnerID><sourceCode></sourceCode><item_total>102.89</item_total><amount_total>122.89</amount_total><amount_tax>6.69</amount_tax><shipping_charge>20.00</shipping_charge><shipping_total>20.00</shipping_total><shipping_allowance>0</shipping_allowance><amount_ins>0</amount_ins><order_create_date>2013-04-27</order_create_date><amount_disc></amount_disc><bfirst>James</bfirst><blast>James</blast><baddr1>Jewell</baddr1><baddr2></baddr2><bcity>Cincinnati</bcity><bstate>OH</bstate><bcountry>US</bcountry><bzip>45255</bzip><sfirst>James</sfirst><slast>James</slast><saddr1>Jewell</saddr1><saddr2></saddr2><scity>Cincinnati</scity><sstate>OH</sstate><scountry>US</scountry><szip>45255</szip><carrier_code>UPS</carrier_code><dob>1970-01-01</dob><creditcard_number>5434651900577797</creditcard_number><card_type>M</card_type><card_expire_date>01/14</card_expire_date><Customer_Email>jamesnjewell@yahoo.com</Customer_Email><CV_Code>123</CV_Code><ChallengeQuestion></ChallengeQuestion><ChallengeAnswer></ChallengeAnswer><localeSetting></localeSetting><ignoreAddressValidation>true</ignoreAddressValidation><auth_code></auth_code><affiliate_code></affiliate_code></oOrderRequest>
2013-04-27 05:22:42.817823 wsdl: in serializeType: returning: <CreateOrder xmlns="http://phepartnerws/"><oOrderRequest><RequestCredentials><CallerID>livereps</CallerID><Timestamp>2013-04-27T01:22:42-04:00</Timestamp><Token>1/kUrVDZmoYqbj1/PhCE4w==</Token></RequestCredentials><company></company><xml></xml><partnerID>livereps</partnerID><sourceCode></sourceCode><item_total>102.89</item_total><amount_total>122.89</amount_total><amount_tax>6.69</amount_tax><shipping_charge>20.00</shipping_charge><shipping_total>20.00</shipping_total><shipping_allowance>0</shipping_allowance><amount_ins>0</amount_ins><order_create_date>2013-04-27</order_create_date><amount_disc></amount_disc><bfirst>James</bfirst><blast>James</blast><baddr1>Jewell</baddr1><baddr2></baddr2><bcity>Cincinnati</bcity><bstate>OH</bstate><bcountry>US</bcountry><bzip>45255</bzip><sfirst>James</sfirst><slast>James</slast><saddr1>Jewell</saddr1><saddr2></saddr2><scity>Cincinnati</scity><sstate>OH</sstate><scountry>US</scountry><szip>45255</szip><carrier_code>UPS</carrier_code><dob>1970-01-01</dob><creditcard_number>5434651900577797</creditcard_number><card_type>M</card_type><card_expire_date>01/14</card_expire_date><Customer_Email>jamesnjewell@yahoo.com</Customer_Email><CV_Code>123</CV_Code><ChallengeQuestion></ChallengeQuestion><ChallengeAnswer></ChallengeAnswer><localeSetting></localeSetting><ignoreAddressValidation>true</ignoreAddressValidation><auth_code></auth_code><affiliate_code></affiliate_code></oOrderRequest></CreateOrder>
2013-04-27 05:22:42.817840 wsdl: serializeRPCParameters returning: <CreateOrder xmlns="http://phepartnerws/"><oOrderRequest><RequestCredentials><CallerID>livereps</CallerID><Timestamp>2013-04-27T01:22:42-04:00</Timestamp><Token>1/kUrVDZmoYqbj1/PhCE4w==</Token></RequestCredentials><company></company><xml></xml><partnerID>livereps</partnerID><sourceCode></sourceCode><item_total>102.89</item_total><amount_total>122.89</amount_total><amount_tax>6.69</amount_tax><shipping_charge>20.00</shipping_charge><shipping_total>20.00</shipping_total><shipping_allowance>0</shipping_allowance><amount_ins>0</amount_ins><order_create_date>2013-04-27</order_create_date><amount_disc></amount_disc><bfirst>James</bfirst><blast>James</blast><baddr1>Jewell</baddr1><baddr2></baddr2><bcity>Cincinnati</bcity><bstate>OH</bstate><bcountry>US</bcountry><bzip>45255</bzip><sfirst>James</sfirst><slast>James</slast><saddr1>Jewell</saddr1><saddr2></saddr2><scity>Cincinnati</scity><sstate>OH</sstate><scountry>US</scountry><szip>45255</szip><carrier_code>UPS</carrier_code><dob>1970-01-01</dob><creditcard_number>5434651900577797</creditcard_number><card_type>M</card_type><card_expire_date>01/14</card_expire_date><Customer_Email>jamesnjewell@yahoo.com</Customer_Email><CV_Code>123</CV_Code><ChallengeQuestion></ChallengeQuestion><ChallengeAnswer></ChallengeAnswer><localeSetting></localeSetting><ignoreAddressValidation>true</ignoreAddressValidation><auth_code></auth_code><affiliate_code></affiliate_code></oOrderRequest></CreateOrder>
2013-04-27 05:22:42.817889 nusoap_client: got wsdl error: phpType is struct, but value is not an array: see debug output for details

I'm still fairly new to using soap/nusoap, but I do believe that the error is the xml value I am passing in directly. If I remove the line 'orderItems' => $orderItemRequest,, I do not get the error. Is there any way to put multiple arrays with the same key into the same array?

Was it helpful?

Solution

This is not how I would have expected it to work, but the below works:

$orderItemRequest = array();
$orderItemRequest['OrderItemRequest'] = array();
foreach ($item_cart as $product) {
    array_push(
        $orderItemRequest['OrderItemRequest'], array(
                'line_id' => $product['line_id'],
                'product_id' => $product['product_id'],
                'product_variant_id' => $product['product_variant_id'],
                'product_desc' => $product['product_desc'],
                'variant_desc' => $product['variant_desc'],
                'quantity' => $product['quantity'],
                'item_total' => $product['item_total']
        )
    );
}

The repeating object is not specified more than once (OrderItemRequest), but instead it is assigned multiple arrays. That caused the object itself to repeat in the xml.

            <orderItems>
                <OrderItemRequest>
                    <line_id>0</line_id>
                    <product_id>A533</product_id>
                    <product_variant_id>A533</product_variant_id>
                    <product_desc>Description</product_desc>
                    <quantity>1</quantity>
                    <variant_desc>Description</variant_desc>
                    <item_total>9.98</item_total>
                </OrderItemRequest>
                <OrderItemRequest>
                    <line_id>1</line_id>
                    <product_id>239</product_id>
                    <product_variant_id>239</product_variant_id>
                    <product_desc>Description</product_desc>
                    <quantity>1</quantity>
                    <variant_desc>Description</variant_desc>
                    <item_total>14.95</item_total>
                </OrderItemR

OTHER TIPS

I found this answer from internet and it helps me a lot:

Imagine the request is going to look like this:

XML Diagram 1:

<CountBasketItems>
    <Basket>
        <fruits>
            <fruit>
                <name>Apple</name>
                <color>red</color>
            </fruit>
            <fruit>
                <name>Bananna</name>
                <color>yellow</color>
            </fruit>
            <fruit>
                <name>Apple</name>
                <color>green</color>
            </fruit>
        </fruits>
        <vegetables>
            <vegetable>
                <name>Orion</name>
                <color>yellow</color>
            </vegetable>
            <vegetable>
                <name>Carot</name>
                <color>orange</color>
            </vegetable>
            <vegetable>
                <name>Orion</name>
                <color>red</color>
            </vegetable>
        </vegetables>
    </Basket>
</CountBasketItems>

The real question is: how do I setup my parameter array to accommodate this. The answer:

$fruits = array();
$fruits[] = array(‘name’ => ‘Apple’, ‘color’ => ‘red’);
$fruits[] = array(‘name’ => ‘Bananna’, ‘color’ => ‘yellow’);
$fruits[] = array(‘name’ => ‘Apple’, ‘color’ => ‘green’);

$vegetables = array();
$vegetables[] = array(‘name’ => ‘Orion’, ‘color’ => ‘yellow’);
$vegetables[] = array(‘name’ => ‘Carrot’, ‘color’ => ‘orange’);
$vegetables[] = array(‘name’ => ‘Orion’, ‘color’ => ‘red’);

$basket = array ( ‘fruits’ => $fruits, ‘vegetables’ => $vegetables );

Here is what $basket looks like when I run print_r($basket):

Array
(
[fruits] => Array
(
[0] => Array
(
[name] => Apple
[color] => red
)
[1] => Array
(
[name] => Bananna
[color] => yellow
)
[2] => Array
(
[name] => Apple
[color] => green
)
)
[vegetables] => Array
(
[0] => Array
(
[name] => Orion
[color] => yellow
)
[1] => Array
(
[name] => Carrot
[color] => orange
)
[2] => Array
(
[name] => Orion
[color] => red
)
)
)

Here is a complete example, server and client:

server.php:

configureWSDL('maxfruitwsdl', 'urn:maxfruitwsdl');
// Put the WSDL schema types in the namespace with the tns prefix
$server->wsdl->schemaTargetNamespace = 'urn:maxfruitwsdl';
// Register the data structures used by the service
$server->wsdl->addComplexType(
'Fruit',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'color' => array('name' => 'color', 'type' => 'xsd:string'),
)
);
$server->wsdl->addComplexType(
'Fruits',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Fruit[]')
),
'tns:Fruit'
);
$server->wsdl->addComplexType(
'Vegetable',
'complexType',
'struct',
'all',
'',
array(
'name' => array('name' => 'name', 'type' => 'xsd:string'),
'color' => array('name' => 'color', 'type' => 'xsd:string'),
)
);
$server->wsdl->addComplexType(
'Vegetables',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Vegetable[]')
),
'tns:Vegetable'
);

$server->wsdl->addComplexType(
'Basket',
'complexType',
'struct',
'all',
'',
array(
'fruits' => array('name' => 'fruits', 'type' => 'tns:Fruits'),
'vegetables' => array('name' => 'vegetables', 'type' => 'tns:Vegetables'),
)
);

// Register the method to expose
$server->register('CountBasketItems', // method name
array('Basket' => 'tns:Basket'), // input parameters
array('return' => 'xsd:int'), // output parameters
'urn:maxfruitwsdl', // namespace
'urn:maxfruitwsdl#CountBasketItems', // soapaction
'rpc', // style
'encoded', // use
'Return the number of items in the basket' // documentation
);

function CountBasketItems( $basket )
{
return count($basket['fruits']) + count($basket['vegetables']);
}

// This returns the result
$server->service($HTTP_RAW_POST_DATA);
?>

client.php

$fruits[] = array('name' => 'Bananna', 'color' => 'yellow');
$fruits[] = array('name' => 'Apple', 'color' => 'green');

$vegetables = array();
$vegetables[] = array('name' => 'Orion', 'color' => 'yellow');
$vegetables[] = array('name' => 'Carrot', 'color' => 'orange');
$vegetables[] = array('name' => 'Orion', 'color' => 'red');

$basket = array ( 'fruits' => $fruits, 'vegetables' => $vegetables );

print "My Basket:";
echo str_replace(' ', '  ', nl2br(htmlspecialchars(print_r($basket, true))));

// Create the client instance
$client = new soapclient('http://www.modphp.org/nusoap/server.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo 'Constructor error' . $err . '';
// At this point, you know the call that follows will fail
}

// Create the proxy
$proxy = $client->getProxy();

// Call the SOAP method
$result = $proxy->CountBasketItems($basket);

// Check for a fault
if ($proxy->fault) {
echo 'Faul';
print_r($result);
echo '';
} else {
// Check for errors
$err = $proxy->getError();
if ($err) {
// Display the error
echo '
Error' . $err . '';
} else {
// Display the result
echo 'Result of CountBasketItems';
print_r($result);
echo '';
}
}
// Display the request and response
echo 'Request';
echo str_replace(' ', ' ', nl2br(htmlspecialchars($proxy->request, ENT_QUOTES)));
echo 'Response';
echo str_replace(' ', ' ', nl2br(htmlspecialchars($proxy->response, ENT_QUOTES)));
// Display the debug messages

It's seems like you don't have an header base element to your xml:

$orderItemRequest = "";

$orderItemRequest .= "<orderItems>"; //main element comes here

foreach($requestArray as $array) {
    foreach ($array as $key => $val) {
        $orderItemRequest .= $comma."<$key>";

        foreach ($val as $key2 => $val2) {
            $orderItemRequest .= "<$key2>$val2</$key2>";
        }

        $orderItemRequest .= "</$key>";
    }
}

$orderItemRequest .= "</orderItems>"; // closing main element comes here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top