php create several objects within object - php

I am trying to make a soap request and it needs to have this structure
$requestBody=new sendDataExtraccionRequest(new sendDataExtraccionSubterranea("18-09-2020","00:02:01",1234567891,12345678.12,12345678.12),
new sendDataExtraccionSubterranea("18-09-2020","00:03:01",1234567891,12345678.12,12345678.12));
thought that by creating an array with each object and then cast it should do, but getting an error on the soap call that the date field is missing
$array_datos[] = new sendDataExtraccionSubterranea("03-02-2021","00:02:01",1234567891,12345678.12,12345678.12);
$array_datos[] = new sendDataExtraccionSubterranea("03-02-2021","00:03:01",1234567891,12345678.12,12345678.12);
$requestBody=new sendDataExtraccionRequest( (object)$array_datos );
Also tried a solution that involved json encoding and decoding the array, but same error
Any hint on how to achieve it?
Thanks

Use Spread Operator like this
$requestBody=new sendDataExtraccionRequest(...$array_datos);

Related

What is the difference between SoapServer features option

I am reading the documentation for php class SoapServer but found nothing about description of options which will be passed in SoapServer constructor:
There is also a features option which can be set to
SOAP_WAIT_ONE_WAY_CALLS,
SOAP_SINGLE_ELEMENT_ARRAYS,
SOAP_USE_XSI_ARRAY_TYPE.
What is the difference between this option values?
SOAP_WAIT_ONE_WAY_CALLS
Without this, SOAP will not wait for a response on one way calls. It will just continue on and assume that all is well in the world. A one way call is anything that does not have a response in the WSDL.
SOAP_SINGLE_ELEMENT_ARRAYS
Your SOAP call may return a single value, or it may return an array of values. However, if you enable this flag, then it will force that single value to be an array with just the single value. You'll know what your data looks like without having to check it.
SOAP_USE_XSI_ARRAY_TYPE
This sets the deserialization type. If an error like this comes up "No deserializer defined for array type {http://www.w3.org/2001/XMLSchema}", then look to enable this feature.

Instantiate an stdClass object from json schema specification in PHP

I have a set of JSON requests that I must send to a RESTFul API in order to get some response objects, you know, the usual thing for a webapp, however these API request objects are properly documented with a json schema specification for each, so I would like to load those schema files and create stdClass object instances based on that info automagically.
Is there some way to do this with a library or something in PHP? (don't want to reinvent the wheel)
Thanks!
Edit: Have a look at this schema file which contains an example of what I want to load and build object instances from.
Disclaimer: I do know json_encode / json_decode which is not what I'm looking for. Using that I'd need to traverse through the returned schema object and then create another object/array based on the schema read, which is not what I want.
I don't think there's a built-in way of doing this, but it should be relatively trivial to implement:
function createObj( $json ) {
$obj_schema = json_decode($json, true);
$new_obj = new StdClass;
foreach($obj_schema['properties'] as $property) {
$new_obj->{$property} = null;
}
return $new_obj;
}

JSON view in Recess Framework

I am trying to create an api with Recess and I have a question about its JsonView. Currently, if I do a GET request on, for example, /users/1 (which routes to a function that gets all the details for the user with id 1 and responds with Json), I get the following:
{"users":{"id":"1","username":null,"password":null,"datejoined":false}}
How can I make it so that I get the following instead:
{"id":"1","username":null,"password":null,"datejoined":false}
That is, I don't want all the details wrapped inside "users":{}.
By default, Recess's JsonView responds with the properties of your controller. So your $users property is getting directly encoded into JSON.
You can override this by returning a custom response object:
return new OkResponse($this->request, (array)$this->users);
Not sure about recess specifically, but if you are using JsonView method/function with an input parameter (array) $result, then changing $result to $result['users'] may give you the answer you are looking for.
For example using plain PHP:
first object: echo json_encode($result);
second object: echo json_encode($result['users']);

SoapVar/Param and nested, repeated elements in SOAP

My goal is to be able to create a soap request that can contain items like so:
<flexFields>
<names>
<names>IAG Group</names>
<names>Ticket #</names>
</names>
</flexFields>
However, every combination of soapvar and soapparam I've been able to think up either makes it impossible for me to duplicate the nested 'names' tag. I can get 1 sub tag like so:
$flexFields = array(
'names'=> new SoapVar(
new SoapVar(array('names'=>'IAG Group'),SOAP_ENC_OBJECT),
SOAP_ENC_OBJECT)
);
This generates:
<flexFields xsi:type="ns2:SoapNamedValues">
<names xsi:type="names">
<names xsi:type="xsd:string">IAG Group</names>
</names>
</flexFields>
But any attempt I make to get the names tag to repeat either generates a dreaded BOGUS element if I use SOAP_ENC_OBJECT, or wraps every item in another 'item' element if I use SOAP_ENC_ARRAY, which is also not desirable.
I know I could just manually create what I want and load it with XSD_ANYXML, but that is getting close to the line of defeating the purpose of using the SOAP library.
Can anyone provide an example of just how to perfectly balance the soapvar/soapparam + array nesting to get this to actually work? Or am I attempting the impossible with PHP's SOAP library?
I have a similar problem, try this:
$Names=array();
$Names[]=new SoapVar("IAG Group",XSD_STRING,null,null,'names');
$Names[]=new SoapVar("Ticket #",XSD_STRING,null,null,'names');
$BigNames=new SoapVar($Names,SOAP_ENC_OBJECT,null,null,'Names');
This creates and array of of SoapVar objects ($Names) and places them in the BigNames object, creating an output like this:
<Names>
<names>IAG Group</names>
<names>Ticket #</names>
</Names>
You can then create another SoapVar object for FlexFields, but for some reason you can't place a SoapVar object directly into another, it has to be stored in an array...
I want to do this:
$FlexFields=new SoapVar($BigNames,SOAP_ENC_OBJECT,null,null,'FlexFields');
This works:
$FF=array($BigNames);
$FlexFields=new SoapVar($FF,SOAP_ENC_OBJECT,null,null,'FlexFields');
I ran into the BOGUS tag problem also. My solution involved using an ArrayObject in place of array primitives. The objects are all then converted to SoapVar objects. It seems the soap library really wants to deal with objects everywhere. I have a more complete writeup here:
http://www.fischco.org/blog/2011/3/26/php-soapserver-objects-arrays-and-encoding.html

PHP Web Service - Same call: Multiple records = array, 1 record=single object - How to work around?

I'm consuming a web service in PHP. If the service returns 2 or more records the object comes back as an array. However, if I call the same service that returns 1 record, the object is not an array. This makes for some messy logic having to watch for both cases when one would think PHP could be smart enough to handle this appropriately and always return an array of 1 element.
So my question is - is there a way to force the return object to always be an array? Some property in the call or something?
EDIT
I'm using PHP's soapclient library. The service is an in-house one that returns an array of a custom class.
you could try the following:
$client = new SoapClient("http://host/services/some.wsdl",
array('feature' => SOAP_SINGLE_ELEMENT_ARRAYS));
This should make php behave the way you want.
Also you might find this dotvoid article interesting.
HTH

Categories