2

我需要为 xml 的这一部分创建一个重复数组作为数组,但是你可以看到这里有一个重复项。

    <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>

这就是我试图做的:

$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' => '');

我尝试将 xml 直接插入为的值,orderItems但得到错误:nusoap_client: got wsdl error: phpType is struct, but value is not an array: see debug output for details

这是调试信息的最后一点:

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

我对使用soap/nusoap还是很陌生,但我相信错误是我直接传入的xml值。如果我删除该行'orderItems' => $orderItemRequest,,我不会收到错误消息。有没有办法将具有相同键的多个数组放入同一个数组中?

4

3 回答 3

5

这不是我所期望的工作方式,但以下工作:

$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']
        )
    );
}

重复对象不会被多次指定 ( OrderItemRequest),而是被分配了多个数组。这导致对象本身在 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
于 2013-04-29T15:23:42.550 回答
1

我从互联网上找到了这个答案,它对我有很大帮助:

想象一下请求将如下所示:

XML图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>

真正的问题是:我如何设置我的参数数组来适应这个。答案:

$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 );

这是我运行 print_r($basket) 时 $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
)
)
)

这是一个完整的示例,服务器和客户端:

服务器.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);
?>

客户端.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
于 2014-08-31T06:22:28.440 回答
0

似乎您的 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
于 2013-04-27T06:36:35.817 回答