SOAP только перечисление первого элемента из таблицы базы данных - PullRequest
0 голосов
/ 04 июня 2019

Итак, есть функция, которая захватывает все строки из таблицы базы данных, и я пытаюсь получить их все в ответе SOAP, в пользовательском интерфейсе SOAP, если вы выполняете запрос, вам не нужно отправлять какие-либо параметры, и он должен вернутьвсе строки в ответе, но только возвращают первую строку следующим образом:

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

  }

Не уверенпочему в нем указан только первый элемент?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...