I tried a few methods but couldn't get it to work,
I am sending a post via curl for query data so can't
simplexml_load_string directly to the url. Posted
content is formed as xml, response is xml, so I'd
like to just load as SXML and return it, but I'm
evidently doing something wrong, here's the curl
function.
I would separate the curl_exec() into a separate step, so that you can more easily differentiate between cURL errors and SimpleXML errors, something like:
PHP Code:
<?php
// add $errors array as pass-by-reference arg
function worldvision_curlcall($dest, $payload, &$errors=array())
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $dest);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, 'payload=' . $payload);
$result = curl_exec($curl);
if ($result == false) { // deal with cURL failure here
$errors[] = curl_error($curl);
return false;
}
curl_close($curl);
libxml_use_internal_errors(true); // requires PHP 5.1+
$data = simplexml_load_string($result);
if ($data == false) { // deal with XML error here
$errors = libxml_get_errors();
}
return $data;
}
// USAGE:
$xml = worldvision_curlcall($foo, $bar, $errors);
if($xml == false) {
print_r($errors); // or whatever you want to do with any error data
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Cool beans, thanks nog you always write something good.
I have a quick question I want to fly by you. This data will
most likely be appended multiple times, (this is a first call
to get basic data, afterwards I will be appending in image
urls, full profiles, etc). Would it be easier/better to keep this
as SXML, or convert to an array? How would you treat/utilize it?
It's probably easier to work with an array and possibly a bit faster, but that might be offset to some degree if you have to convert it back into XML at the end?
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks