PHP SOAP - Passing parameters with same name - php

I'm calling an API using php and SOAP.
some parts of the request are like:
<fin:Customer>
<fin1:Address>
<fin1:City>City</fin1:City>
<fin1:Country>CA</fin1:Country>
</fin1:Address>
<fin:Customer>
Which is easy to do with arrays:
'Customer' => array(
'Address' => array (
'City' => $City,
'Country' => $Country,
),
But my challenge is for this part:
<fin:Criterions>
<fin:Criterion name="VALX">17</fin:Criterion>
<fin:Criterion name="VALOP">1</fin:Criterion>
<fin:Criterion name="VALLP">10</fin:Criterion>
<fin:Criterion name="TMS">3</fin:Criterion>
Because all tags have the same name, and they also have a name parameter and a value.
How can I pass that to SOAP API?
Is it possible to use arrays?
Thanks in advance!

The easiest way to construct a request, send it and handle the response is to use a WSDL to PHP generator as it'll normally generate each class for every parameter you need to send (in addition to the response objects and the operations classes)
Knowing this, you'll be able to easily see how to pass these "duplicated" tags as it is certainly an array of object that you must pass,
Try the PackageGenerator project which should work fine,

Solution:
'Criterions' => array(
'Criterion' => array(
array('name'=> 'VALX' , '_'=> '17' ),
array('name'=> 'VALOP' , '_'=> '1' ),
array('name'=> 'VALLP' , '_'=> '10' ),
array('name'=> 'TMS' , '_'=> '3' )
));
Be sure to use underscore ('_'), if other values (e.g. val, value, etc) didn't work.
Might save someone's time and effort.

Related

Passing multiple values to same parameter with a PHP array with CURL

I'm need to execute a REST API POST request with PHP.
The URL post parameters looks like this and works fine when executed as such:
https://example.com.json?userid=MYUSER&key=MYKEY&order=000000&server=ns1.example.com&server=ns2.example.net&server=ns3.example.net
I use the following PHP array with success before without any issues for passing unique values to the same API in my PHP Curl Request:
$data = array(
'userid' => 'value',
'key' => 'value',
'order' => 'value',
);
The problem now, looking at the URL is that the "server" parameters accepts more than one value (order is not important) and I'm supposed to pass this as an string of arrays, minimum of 2 values.
But this does not work:
$data = array(
'userid' => 'value',
'key' => 'value',
'order' => 'value',
'server' => array('ns1.example.net','ns2.example.net')
);
I tried multiple different ways. How do I pass multiple values to the same parameter on that array?

Getting JSON data

Im working with an api which stores data into a JSON file. This data is gathered from a form that the users fill in my website. The way its inserted goes as follow:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
And then:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
Now instead of post i want to get the data. I tried getting data like this:
$SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=*my name*');
I dont know if this is the right way, well it didn't work so obviously its not.
Anyways what im trying to achieve is with PHP i want to gather the name value of an existing person. this data is stored in /api/v2/crm/person.json
Api documentation (which i read but didn't understand to well) http://api.simplicate.nl/
It's been a while but i'm trying to answer all my open questions without an answer which i ended up solving on my own.
So for this.
You have to create a variable which makes the get request like this:
$test = $SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=My name');
Now you can for example do a var_dump($test);
And as output you will get all the data inside
/crm/organization?q[name]=My name

Check if data exists

I'm working with an API.
With an array I collect data like this:
$org_payload = array(
'name' => $_POST['billing_company'],
'phone' => $_POST['billing_phone'],
'email' => $_POST['billing_email'],
'note' => $_POST['order_comments'],
'relation_type' => array(
'id'=>'relationtype:c1ec3ae77036842d' //provide the relationtypeid, f.e. relationtype:796ce0d318a2f5db515efc18bba82b90
),
'visiting_address' => array(
'country_code' => 'NL',
'line_1' => $_POST['billing_address_1'],
'postal_code' => $_POST['billing_postcode'],
'locality' => $_POST['billing_city'],
'country' => $_POST['billing_country']
), // can be extented with other address data
'postal_address' => array(
'country_code' => 'NL'
) // can be extented with other address data
);
At one point i send this data to the program i'm working with. I achieve this with this code:
$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));
I gather this data from a form on my website. This data gets posted in the program.
I am trying to achieve that when data gathered from my form matches existing data in the program then don't add it. I would like a hint in the right direction for this, been looking on the internet without any luck.
What I would suggest is to have one extra call to the API.
Like you said in the comments - the company name and the phone number is unique.
If there is some call to get a user by those values and check what you got from the form, would be enough.
If they are unique - send them,
if not - show to the user or whatever you want to do here.
No need to keep one more database on your system as well.

Dynamically add fields to http_build_query

I have built an API that I want to test. By that reason I'm building a simple client to try out the different features (CRUD). Below is the function for updating a producer, which works fine. However, I also want to be able to update parts of a producer, e.g. address (/producers/8?method=put&address=milkyway).
The array producer always contains the same elements (name, address, zipcode etc) but I only want to update the producer with the elements in the array which contains of anything. What I mean with that is that if for example the name element in the array is empty then name shouldn't be included in *http_build_query*. If only the name element contains of anything then only name should be updated.
So, let's say that the array (except for id that of course is mandatory) contains of address. How can I dynamically add only that to *http_build_query* ?
Thanks in advance!
public function UpdateProducer($producer) {
$url = 'http://localhost/webbteknik2/Labb2/api/v1/producers/ . $producer['id'] . '?method=put';
$data = http_build_query(array(
'name' => $producer['name'],
'address' => $producer['address'],
'zipcode' => $producer['zipcode'],
'town' => $producer['town'],
'url' => $producer['url'],
'imgurl' => $producer['imgurl'],
'latitude' => $producer['latitude'],
'longitude' => $producer['longitude'],
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
...
the rest of the curl code
}
Note: I know this is bad coding in many ways, but as I said I only, asap want to be able to test the CRUD functionality through the client.
use array_filter to remove the empty elements....
$params = array(
'name' => $producer['name'],
'address' => $producer['address'],
'zipcode' => $producer['zipcode'],
'town' => $producer['town'],
'url' => $producer['url'],
'imgurl' => $producer['imgurl'],
'latitude' => $producer['latitude'],
'longitude' => $producer['longitude'],
);
$data = http_build_query(array_filter($params, 'is_null'));

Iterating through an array and replacing data within a template

I have an XML document with "tags" that are replaced based on data within an array. There are two types of tags, one is a parent tag to define a set, another is simply a tag that is replaced by a value. Here's an example of the data used to build and fill in the template:
$array = array(
'name' => 'name',
'city' => 'city',
'addresses' => array(
array(
'street' => '123',
'city' => 'main'
),
array(
'street' => '123',
'city' => 'main'
'phone' => array(
array(
'home' => '123456', 'work' => '1234567'
)
Here is an example template:
<name>%name%</name>
<city>%city%</city>
%%addresses%%
<street>%street%</street>
<city>%city%</city>
%%phone%%
<home>%%home%%</home>
<work>%%work%%</work>
%%/phone%%
%%/addresses%%
The key values of the array, match the tags within the template. If the key is an array itself, then it loops through the data contained within that key's tag (%%).
I've tried doing a recursive function but it only seems to work one level deep.
Does anyone have any suggestions? Thank you!
I suggest you use an existing simple template language that "just works"tm, like Mustache (there are plenty much others). I know that Mustache supports looping over arrays, used it, does the job, easy to integrate. Available for many languages.

Categories