How to pass an array into a PHP SoapClient call

后端 未结 6 1932
[愿得一人]
[愿得一人] 2020-12-05 19:33

Using PHP and SoapClient.

I need to pass the following XML into a soap request - i.e. multiple \'s within .

<
相关标签:
6条回答
  • 2020-12-05 19:49

    I also had this problem and found the solution. Stays needs to be an array with ascending keys starting with 0.

    $client = new SoapClient('http://myservice.com?wsdl');
    $stays[] = array('startDate'=>'01-01-2013', 'endDate'=>'02-02-2013');
    $stays[] = array('startDate'=>'02-02-2013', 'endDate'=>'03-03-2013');
    $params = array(
      'reservation' => array('stays'=>$stays)
    );
    $client->saveReservation($params);
    

    I found my answer on this page: https://bugs.php.net/bug.php?id=45284

    0 讨论(0)
  • 2020-12-05 19:50

    I had similar problem and I had to post data in this structure. Accepted answer didn't work for me

    $xml = array('reservation' => array(
        'stays' => array(
            array(
                'start_date' => '2011-01-01',
                'end_date'   => 2011-01-15
            ),
            array(
                'start_date' => '2011-01-01',
                'end_date'   => 2011-01-15
            )
        )
    ));
    

    maybe it might help somebody if accepted answear doesn't work for you

    + minor tip for all of you, use $xml = ['key' => 'val']; instead of $xml = array('key' => 'val');

    0 讨论(0)
  • 2020-12-05 19:54

    Assuming that when you instantiated $soapClient, you did so in WSDL mode, the following should work:

    $stay1 = new stdClass();
    $stay1->start_date = "2011-01-01";
    $stay1->end_date = "2011-01-15";
    $stay2 = new stdClass();
    $stay2->start_date = "2011-01-01";
    $stay2->end_date = "2011-01-15";
    $stays = array();
    $stays[0] = $stay1;
    $stays[1] = $stay2;
    $soapClient->saveReservation(
        array("reservation" => array("stays" => $stays))
    );
    
    0 讨论(0)
  • 2020-12-05 19:59

    I also ran into this issue calling soap with an parameter as an array. My array has to start with index 0 for it to work.

    $client->__soapCall(my_function_name, [
      'body' => [
        'date' => '201930', 
        'ids' => [0 => '32001', 1 => '32002'],
      ],
    ]);
    
    0 讨论(0)
  • 2020-12-05 20:00

    Try this:

    $xml = array(
      'stays' => array(
        'stay' => array(
          array( /* start end */ ),
          array( /* start end */ ),
          array( /* start end */ )
        )
      )
    );
    
    0 讨论(0)
  • 2020-12-05 20:10

    'stay' has to be defined just once. This should be the right answer:

    $xml = array('reservation' => array(
    'stays' => array(
        'stay' => array(
                        array(
                              'start_date' => '2011-01-01',
                              'end_date'   => 2011-01-15
                        ),
                        array(
                              'start_date' => '2011-01-01',
                              'end_date'   => 2011-01-15
                        )
                  )  
        )
    ));
    
    0 讨论(0)
提交回复
热议问题