I'm using Zend_Soap_Client and encountering this issue:
<parent>
<child><name>abc</name></child>
<child><name>def</name></child>
</parent>
If there's more than one child element then Zend return array and I can access like
$result->parent->child[0]->name
but if there's only one child node it returns object like:
$result->parent->child->name
Can you please let me know what's wrong with my approach or how can I overcome it?
My sample code:
$client = new Zend_Soap_Client('url', array('wsdl'=>'url));
$result = $client->getResult();
I'm using zend 1.9. The same issue happens with PHP's native SoapClient
Thanks!
Personally I do not see the need to use Zend_Soap_Client instead of SoapClient because the Zend version does not add anything beneficial, but on the other hand the solution applies to both:
There is an options array parameter in the original SoapClient that accepts plenty of things, and especially this below (ref):
The features option is a bitmask of SOAP_SINGLE_ELEMENT_ARRAYS,...
With this option, all array structures in the soap response are not reduced to one single element if they contain only one, but left as is. You are always accessing an array then, which is easier than switching depending on the content.
Example:
$s = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
Related
In the previous version of AWS SDK there were many handy functions like getDescribeInstancesIterator which would return an array iterator for results (in this case an array of instances matching the filters).
This function (and similar) have been removed in the new AWS SDK. After some research I can find instead is a getIterator($name, array $args = []) function defined as an AwsClientTrait.
I'm unsure how I can use this new getIterator function to replace the getDescribeInstancesIterator function, i.e. to get an Array iterator for describeInstances function that returns list of matching instances like before (without having to worry about pagination, etc)?
Any code sample would be extremely useful.
Finally figured it out. The new syntax is like this
$ec2Client->getIterator('FunctionName', 'Values')
So getDescribeInstancesIterator now becomes:
$ec2Client->getIterator('DescribeInstances', [...'Filters'])
This syntax is same for all operations including iterating over files in buckets, getting running instances, etc.
For visual representation, for simplicity and of course to feed my curiosity, I'm wondering how to convert a PHP array into a valid PHP resource.
See the below example:
(example image created with dBug component available at http://dbug.ospinto.com/)
I've made 3 examples:
resource: this is the typical representation of a MySQL resource, visualized as a grid
object: a handmade create object from an array
array: a handmade multidimensional array
As you can see, the resource is a visual beauty, while the object and array are constructed by using multidimensional arrays, using poor numeric array indexes to bind them together :(
What I'm looking for, would probably be something like this:
$resource_var = (resource) $array_var;
What I'm looking for, would probably something like this:
$resource_var = (resource) $array(var)
You will never find that. A resource is an internal data-type in PHP. If (and only if) you write yourself a PHP extension and load it, you could do the following:
$resource = array_resource_create($array);
Your PHP extension then would create that resource (as the mysql extension for example creates its specific resource type) within that array_resource_create function. However, it would be useless, because there is no other function so far that could deal with that resource.
You can't create resource. but you can use native one.
Try with curl for example.
function makeResourceFromArray($array) {
$resource = curl_init();
curl_setopt($resource, CURLOPT_PRIVATE, serialize($array));
return $resource;
}
function makeArrayFromResource($resource) {
return unserialize(curl_getinfo($resource, CURLINFO_PRIVATE));
}
$resource = makeResourceFromArray(['name' => 'test']);
$array = makeArrayFromResource($resource);
The output you show there is nothing to do with it being a resource as such, but the pretty-print function you're using noticing that the variable you've given it points at a database result set, and fetching and displaying the results.
What PHP means by a resource is that the variable doesn't actually hold data within PHP, but is a pointer or reference usable by some lower-level module of code - in this case, a DB library which can use that reference to retrieve the results of the executed query.
If you just want the pretty-print to look similar for an array with a DB-resultset-like structure, then you should simply modify the pretty-print function to do so - you don't need to do anything to the array itself.
A resource is a special type. And a resource is specific to a source that's external. Therefore going backwards wouldn't be possible.
Theoretically, an interface with an instance of the resource would help manage the type - but this is just nonsense theoretical talk that is impossible in PHP.
My Issue: Unable to append XML data to prexisting XML data in a MYSQL database.
I have an array - $buyer. Inside this array is a $key and $value similar to (shippingTotal => 55). What I want to do is use something similar to
$param = array(
'shippingTotal' => $shippingTotal
);
$where['quote_data = ?'] = $quoteNumber
$n = $db->update('quote_xml', simplexml_load_string($param), $where);
My hiccup is that the current data inside quote_data is an XML element containing LOTS of information. Is there any way to just "stick" shippingTotal into said existing XML? When I use the above code I just end up with quote_data becoming empty.
I also created a variable called $shippingTotal so that I wouldn't have to use $buyer['shippingTotal']. Still not functional.
Thank you for your time and assistance with this issue.
Aaron
I see a few issues with this:
First, simplexml_load_string doesn't accept array parameters, only XML strings. Since the $params is not a valid argument, it is returning boolean false. Even when successful, it returns a SimpleXMLElement. To convert that to an XML string, you would have to call the asXML() method on the returned object before passing it to Zend_Db_Table::update().
Second, most likely XML cannot just be "appended" to other XML. I don't know exactly what your table holds, but the XML needs to be programmatically added to the existing XML. You can't append XML because the data you want to add needs to be added to the proper nodes.
What you will have to do is first read the value of that column, parse it using SimpleXML, add your new data to the appropriate node in the document using one of the SimpleXML functions and then perform the update.
Hope that helps.
So, I want to ultimately create a DOM object for the XML, however, $xml_dom seems to be empty. var_dump shows object(DOMElement)#3 (0) { }. $xml_simple is good. What am I doing wrong here?
$get_url_report = 'http://...'; // contains well-formatted XML data
$xml_simple = simplexml_load_string(file_get_contents($get_url_report));
$xml_dom = dom_import_simplexml($xml_simple);
DOM doesn't work well with var_dump(), for example read this comment. Additional there is already a bug report (for over two years now ...).
The object is probably not empty, even if it looks so. You should be able to use it like described in the manual.
var_dump isn't useful for objects such as SimpleXMLElement or DOMElement. Try something like $xml_dom->tagName or $xml_simple->asXML() to see whether those objects have content.
P.S. you may also use simplexml_load_file($get_url_report) instead of simplexml_load_string(file_get_contents($get_url_report));
which is better expression to make string into xml object[php]?
$xml = new SimpleXMLElement($xml_string);
vs
$xml = simplexml_load_string($xml_string);
same?
They're basically identical. SimpleXMLElement::__construct accepts data strings or file paths, if you set the appropriate options. simplexml_load_file and simplexml_load_string are basically convenience function wrappers that amount to the same thing as new SimpleXMLElement() with the correct options set.
They're essentially the same. Which one you use is based on personal taste. I'd go for the first. The simplexml_load_string function will create an object too, which makes it essentially an alias for the constructor.
The simplexml_load_string lets you specify the class of which you want to get the object of. If you call the simplexml_load_string without specifying the class as second parameter, then it automatically gives you the object of SimpleXMLElement.
So, in the way you are running it, they both will give you same results.