I need to pass multiple NameValueLists via SOAP, but I have no idea how to include their data.
This is how it should look like:
<Variations>
<VariationSpecificsSet>
<NameValueList>
<Name>Size</Name>
<Value>XL</Value>
</NameValueList>
<NameValueList>
<Name>Color</Name>
<Value>Black</Value>
</NameValueList>
</VariationSpecificsSet>
</Variations>
Part of my PHP code:
$params->Item->Variations = new ArrayObject();
$params->Item->Variations->VariationSpecificsSet = new ArrayObject();
$params->Item->Variations->VariationSpecificsSet->NameValueList = new ArrayObject();
$list = new ArrayObject();
$list->name = 'title';
$list->value = "value";
$arr[0] = $list;
$arr[1] = $list;
$params->Item->Variations->VariationSpecificsSet->NameValueList = $arr;
$ebay->ebayCall( "VerifyAddFixedPriceItem", $params );
Debug output of $params:
[Variations] => ArrayObject Object
(
[VariationSpecificsSet] => ArrayObject Object
(
[NameValueList] => Array
(
[0] => ArrayObject Object
(
[name] => title
[value] => value
[storage:ArrayObject:private] => Array
(
)
)
[1] => ArrayObject Object
(
[name] => title
[value] => value
[storage:ArrayObject:private] => Array
(
)
)
[2] => ArrayObject Object
(
[name] => title
[value] => value
[storage:ArrayObject:private] => Array
(
)
)
)
[storage:ArrayObject:private] => Array
(
)
)
[storage:ArrayObject:private] => Array
(
)
)
The resulting request: No names, neither values in the NameValueLists
<ns1:Variations>
<ns1:VariationSpecificsSet>
<ns1:NameValueList/>
<ns1:NameValueList/>
<ns1:NameValueList/>
</ns1:VariationSpecificsSet>
</ns1:Variations>
How do I put the data correctly in the NameValueLists? The debug output seems good to me, but it wont show up in the XML. I can't be the first one..?
Edit: I found a question on SO that describes exactly my problem:
Php soap client multiple node
I tried this approach before asking here, but it still dont work for me. I'm guessing, it has something to do with the eBay wsdl, but I cant figure out what exactly
Missing capital Letters at $list->name and $list->value is the answer. hope that helps anyone else sometime. grrr
Related
I'm using the namecheap API to do some stuff, it's the first time I've used a API and I'm running into a bit of a problem.
This is what I have so far:
$ApiKey = "**********************";
$ApiUser = "*****";
$UserName = "*********";
$ClientIP = "********";
$NamecheapURI = "https://api.namecheap.com/xml.response";
$executionURL = $NamecheapURI."?ApiUser=".$ApiUser."&ApiKey=".$ApiKey."&UserName=".$UserName."&Command=namecheap.domains.check&ClientIp=".$ClientIP."&DomainList=".$domain;
$xml = simplexml_load_file($executionURL);
print_r($xml);
When print $xml I am returned simple XML objects:
SimpleXMLElement Object
(
[#attributes] => Array
(
[Status] => OK
)
[Errors] => SimpleXMLElement Object
(
)
[Warnings] => SimpleXMLElement Object
(
)
[RequestedCommand] => namecheap.domains.check
[CommandResponse] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Type] => namecheap.domains.check
)
[DomainCheckResult] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Domain] => facebook.com
[Available] => false
[ErrorNo] => 0
[Description] =>
[IsPremiumName] => false
[PremiumRegistrationPrice] => 0
[PremiumRenewalPrice] => 0
[PremiumRestorePrice] => 0
[PremiumTransferPrice] => 0
[IcannFee] => 0
[EapFee] => 0
)
)
)
[Server] => PHX01APIEXT03
[GMTTimeDifference] => --5:00
[ExecutionTime] => 0.008
)
My question is beyond this, how do I move forward and pull data from this?
I've tried treating this as an array but I am getting nowhere, when using is_array() to test if it was an array it says it's not which I don't understand...
I apologise if this is a noob question, I am a bit new to this. In short, what do I need to do to pull data from this?
Thanks in advance!
Learning to use SimpleXML is much better than trying to convert it to arrays/json/anything else and simple (hence the name). A quick example...
$response = '<?xml version="1.0" encoding="UTF-8"?>
<CommandResponse Type="namecheap.domains.check">
<DomainCheckResult Domain="facebook.com">
<Element>1234</Element>
<Element>12345</Element>
</DomainCheckResult>
</CommandResponse>';
$xml = simplexml_load_string($response);
echo "DOmain=".$xml->DomainCheckResult['Domain'].PHP_EOL;
foreach ( $xml->DomainCheckResult->Element as $value) {
echo "Value=".(string)$value.PHP_EOL;
}
outputs...
DOmain=facebook.com
Value=1234
Value=12345
You have to adapt this to your own XML, but the idea is that if you want to access an element of an item you use object notation -> and if you need to get an attribute, use array notation [].
So in the above code, the first echo ($xml->DomainCheckResult['Domain']) gets the <DomainCheckResult> element and outputs the Domain attribute.
Then the foreach loop says fetch each <Element> within <DomainCheckResult> and output the value.
I've retrieved data from an XML file like so
$response = simplexml_load_file($url);
print_r displays the following
SimpleXMLElement Object
(
[artist] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => Group
[id] => b9fb5447-7f95-4a6a-a157-afed2d7b9f4c
)
[name] => He Is Legend
[sort-name] => He Is Legend
[country] => US
[area] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 489ce91b-6658-3307-9877-795b68554c98
)
[name] => United States
[sort-name] => United States
[iso-3166-1-code-list] => SimpleXMLElement Object
(
[iso-3166-1-code] => US
)
)
)
)
So I can output fields like name and country by
echo $response->artist->name;
echo $response->artist->country;
However I'm stuck when it comes to being able to access data in the attribute arrays.
How can I get the type of group from the first attributes array for example?
Edit
I'm also trying to return the details from a function like so
func getDetails($id) {
$response = simplexml_load_file('http://musicbrainz.org/ws/2/artist/'.$id);
$data = array();
$data['type'] = $response->artist->attributes()->type;
$data['country'] = $response->artist->country;
return $data;
}
print_r(getDetails());
Gives me
MusicBrainz Object
(
)
You can access them using the attributes() method:
echo $response->artist->attributes()->type;
The example #5 in the SimpleXML documentation shows another example.
I would like to get methods from REST XML file via PHP.
I have local REST file, which is in this format:
SimpleXMLElement Object
(
[doc] => SimpleXMLElement Object
(
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[base] => https://**url**
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => xml/{accesskey}/project
)
[param] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => accesskey
[style] => template
[type] => xs:string
)
)
[method] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => getAllProjects
[name] => GET
)
[response] => SimpleXMLElement Object
(
[representation] => SimpleXMLElement Object
(
[#attributes] => Array
(
[mediaType] => application/xml; charset=utf-8
)
)
)
)
... and so on
I have the following code, but it returns just the first method name:
$file="application.wadl";
$xml = simplexml_load_file($file);
foreach($xml->resources[0]->resource->method->attributes() as $a => $b) {
echo $b,"\n";
}
I would like to extract all of them, not just the first one. How to do that?
Rather than looping over the attributes of one element, you need to loop over all the elements with the same name. Due to the magic of SimpleXML, this is as simple as this:
foreach($xml->resources->resource->method as $method) {
echo $method['id'],"\n";
}
When followed immediately by another operator, as with ->resources, SimpleXML assumes you just want the first element with that name. But if you loop over, it will give you each of them, as a SimpleXML object.
EDIT : It looks like the nesting of your XML means you need some form of recursion (you need to look at $xml->resources->resource->resource->resource->method etc).
Something like this perhaps (untested example)?
function get_methods($base_url, $node)
{
$all_methods = array();
// Child resources: build up the path, and recursively fetch all methods
foreach ( $node->resource as $child_resource )
{
$child_url = $base_url . '/' . (string)$child_resource['path'];
$all_methods = array_merge(
$all_methods,
get_methods($child_url, $child_resource)
);
}
// Methods in this resource: add to array directly
foreach ( $node->method as $method )
{
$method_url = $base_url . '/' .(string)$method['id'];
$all_methods[$method_url] = (string)$method['id'];
}
return $all_methods;
}
print_r( get_methods('/', $xml->resources) );
Incidentally, print_r won't always give you the best view of a SimpleXML object, because they are actually wrappers around non-PHP code. Try this simplexml_dump() function instead.
I want to display the Itinerary details from developer.ean.com API. By passing the customer's Itinerary ID and Email ID I got the details of reservation.
The result is comming in json format so I decoded it and creating array by using :
$array=json_decode($result);
The problem is whatever the result comming from API contain problem like :
For some records it providing array like this:
[Itinerary] => stdClass Object
(
[HotelConfirmation] => Array
(
[0] => stdClass Object
(
[supplierId] => 13
[chainCode] => EP
[arrivalDate] => 07/24/2012
[departureDate] => 07/26/2012
)
[Hotel] => Array
(
[0] => stdClass Object
(
[hotelId] => 394808
[statusCode] => A
[name] => Barkston Youth Hostel
)
)
)
)
In this case the HotelConfirmation and Hotel is Array which contain [0] as object
and for some records it providing array like this:
[Itinerary] => stdClass Object
(
[HotelConfirmation] => stdClass Object
(
[supplierId] => 13
[chainCode] => EP
[arrivalDate] => 07/24/2012
[departureDate] => 07/26/2012
)
[Hotel] => stdClass Object
(
[hotelId] => 394808
[statusCode] => A
[name] => Barkston Youth Hostel
)
)
and In this case the HotelConfirmation and Hotel is itself an object
I providing only few data here actually its big array and I want to provide list of it. But the array containing ambiguity like this. How can I handle this issue. Is there any solution.
Thanks in advance.
Pass true as the second argument to json_decode. This will create an array instead of stdClass
$array=json_decode($result, true);
you can normalize the input like so:
// get objects as arrays
$array = json_decode($result, true);
// remove the sub-level [0], when necessary
foreach ($array as $key => $value) {
if (is_array($value[0])) {
$array[$key] = $value[0];
}
}
Then the result always looks the same:
[Itinerary] => Array
(
[HotelConfirmation] => Array
(
[supplierId] => 13
[chainCode] => EP
[arrivalDate] => 07/24/2012
[departureDate] => 07/26/2012
)
[Hotel] => Array
(
[hotelId] => 394808
[statusCode] => A
[name] => Barkston Youth Hostel
)
)
First do this:
$array = json_decode($result, true);
Which will convert objects into associative arrays
And do adjustment like this:
if (isset($array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'][0])) {
$array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'] = $array['HotelItineraryResponse']['Itinerary']['HotelConfirmation'][0];
}
It will definitly work.
You can use is_array() to check for it:
if (is_array($hotel)) {
$hotel = $hotel[0];
}
just change to this:
$array=json_decode($result,TRUE);
and handle arrays always?
Looks like you may have to account for both possibilities in your model... Checking to see if the hotel node contains and array or an obj and operating accordingly.
you can type case the object inside of an array.
$array = json_decode($result);
$array = (array)$array
or alternatively you can pass true as the second argument in your json_decode();
according to php documentation
When TRUE, returned objects will be converted into associative arrays.
$array = json_decode($result, true);
Check for object or array:
if( is_object($Itinerary -> HotelConfirmation)) {
// do one thing or nothing
} elseif( is_array($Itinerary -> HotelConfirmation)) {
$Itinerary -> HotelConfirmation = array_shift( $Itinerary -> HotelConfirmation );
}
I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
$value = (array) $object;
Third Solution
$value = json_decode(json_encode($object), true);
to get value of converted array
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
$object->values->{'0'}->id
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
echo get_object_vars($object)['values']['0']['id'];
I had the same issue, still not so sure why but I was able to get it working using this workaround:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements can be Array to but I chose to demonstrate with json string.
I hope this helps somehow !!!
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
I'm doing the same thing and all I did was this;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
this can help you accessing subarrays in php using codeigniter framework
foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
echo $novo_puto_ultimos_30_dias;
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
Object['values'][0]['id']
or
Object['values'][0]->id
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.