SOAP仅列出数据库表中的第一项

时间:2019-06-04 18:53:58

标签: php for-loop soap

因此,有一个函数可以从数据库表中获取所有行,而且我试图将所有行都放入SOAP响应中,在SOAP UI中,如果您运行请求,则不需要提交任何参数,它应该返回响应中的所有行,但仅返回第一行,如下所示:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:listInventoryResponse xmlns:ns1="urn:tshirt">
         <ArrayOfString xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType=":[1]">
            <item xsi:type="xsd:">
               <productCode xsi:type="xsd:string">TestCode</productCode>
               <size xsi:type="xsd:string">S</size>
               <description xsi:type="xsd:string">Test</description>
               <count xsi:type="xsd:string">3</count>
            </item>
         </ArrayOfString>
      </ns1:listInventoryResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

应该是这样的:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:listInventoryResponse xmlns:ns1="urn:tshirt">
         <ArrayOfString xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType=":[1]">
            <item xsi:type="xsd:">
               <productCode xsi:type="xsd:string">TestCode</productCode>
               <size xsi:type="xsd:string">S</size>
               <description xsi:type="xsd:string">Test</description>
               <count xsi:type="xsd:string">63</count>
            </item>
            <item xsi:type="xsd:">
               <productCode xsi:type="xsd:string">TestCode2</productCode>
               <size xsi:type="xsd:string">S</size>
               <description xsi:type="xsd:string">Test</description>
               <count xsi:type="xsd:string">33</count>
            </item>
            <item xsi:type="xsd:">
               <productCode xsi:type="xsd:string">TestCode3</productCode>
               <size xsi:type="xsd:string">M</size>
               <description xsi:type="xsd:string">Test</description>
               <count xsi:type="xsd:string">13</count>
            </item>
         </ArrayOfString>
      </ns1:listInventoryResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我添加了一个ComplexType,看起来像这样:

server->wsdl->addComplexType(
  'ArrayOfString',
  'complexType',
  'array',
  'sequence',
  '',
  array(
    'productCode' => array(
      'name' => $value['productCode'], 
      'type' => 'xsd:string',
      'minOccurs' => '0', 
      'maxOccurs' => 'unbounded'
    ),
    'size' => array(
      'name' => $value['size'], 
      'type' => 'xsd:string',
      'minOccurs' => '0', 
      'maxOccurs' => 'unbounded'
    ),
    'description' => array(
      'name' => $value['description'], 
      'type' => 'xsd:string',
      'minOccurs' => '0', 
      'maxOccurs' => 'unbounded'
    ),
    'shirt_count' => array(
      'name' => $value['shirt_count'], 
      'type' => 'xsd:int',
      'minOccurs' => '0', 
      'maxOccurs' => 'unbounded'
    )
  )
);

这就是我所说的:

$server->register('listInventory',
            array(),  //parameter
            array('ArrayOfString' => 'tns:ArrayOfString'),  //output
            'urn:tshirt',   //namespace
            'urn:tshirt#listInventory' //soapaction
            ); 

最后是我的功能代码,它从数据库中获取所有行

function listInventory(){
    global $dbconn;
      $sql = "SELECT productCode, size, description, shirt_count
              FROM inventory";
      // prepare sql and bind parameters
      $stmt = $dbconn->prepare($sql);
      // insert a row
      $stmt->execute();

      $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

      $dbconn = null;

      foreach ($data as $key => $value) {
        $response = array();
        $response['inventory'] = array();
        $response['inventory']['productCode'] = $value['productCode'];
        $response['inventory']['size'] = $value['size'];
        $response['inventory']['description'] = $value['description'];
        $response['inventory']['count'] = $value['shirt_count'];

        return $response;
    }

  }

不确定为什么只列出第一项?

0 个答案:

没有答案