PHP How to link Soap XML with XML Schema(.xsd) - php

I have two urls.
http://www.labs.skanetrafiken.se/v2.2/GetStartEndPoint.xsd
http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=lund&inpPointTo=ystad
How do I get these two to collaborate so I can extract the information via PHP?
How does one extract all the information out of the XML file into a PHP object or array.

I just answered my own question with this code:
/**
* convert xml string to php array - useful to get a serializable value
*
* #param string $xmlstr
* #return array
* #author Adrien aka Gaarf
*/
function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
}
elseif($v) {
$output = (string) $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['#attributes'] = $a;
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='#attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
$xml = 'http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=lund&inpPointTo=ystad';
$xmlstr = new SimpleXMLElement($xml, null, true);
$array = xmlstr_to_array($xmlstr->asXML());
print_r($array);
This returns an array with the XML, exactly what I want to be able to work with.

Related

How can I parse my Apple Mac contacts vCard correctly?

I am working with the vCard Parser from Sourceforge https://sourceforge.net/projects/vcardphp/files/vcardphp/vcardphp-1.1.2/
I just post an excerpt here:
vcard.php
function parse(&$lines)
{
$this->_map = null;
$property = new VCardProperty();
while ($property->parse($lines)) {
if (is_null($this->_map)) {
if ($property->name == 'BEGIN') {
$this->_map = array();
}
} else {
if ($property->name == 'END') {
break;
} else {
$this->_map[$property->name][] = $property;
}
}
// MDH: Create new property to prevent overwriting previous one
// (PHP5)
$property = new VCardProperty();
}
return $this->_map != null;
}
function parse(&$lines)
{
while (list(, $line) = each($lines)) {
$line = rtrim($line);
$tmp = split_quoted_string(":", $line, 2);
if (count($tmp) == 2) {
$this->value = $tmp[1];
$tmp = strtoupper($tmp[0]);
$tmp = split_quoted_string(";", $tmp);
$this->name = $tmp[0];
$this->params = array();
for ($i = 1; $i < count($tmp); $i++) {
$this->_parseParam($tmp[$i]);
}
if ($this->params['ENCODING'][0] == 'QUOTED-PRINTABLE') {
$this->_decodeQuotedPrintable($lines);
}
if ($this->params['CHARSET'][0] == 'UTF-8') {
$this->value = utf8_decode($this->value);
}
return true;
}
}
return false;
}
and
vbook.php
function parse_vcards(&$lines)
{
$cards = array();
$card = new VCard();
while ($card->parse($lines)) {
$property = $card->getProperty('N');
if (!$property) {
return "";
}
$n = $property->getComponents();
$tmp = array();
if ($n[3]) $tmp[] = $n[3]; // Mr.
if ($n[1]) $tmp[] = $n[1]; // John
if ($n[2]) $tmp[] = $n[2]; // Quinlan
if ($n[4]) $tmp[] = $n[4]; // Esq.
$ret = array();
if ($n[0]) $ret[] = $n[0];
$tmp = join(" ", $tmp);
if ($tmp) $ret[] = $tmp;
$key = join(", ", $ret);
$cards[$key] = $card;
// MDH: Create new VCard to prevent overwriting previous one (PHP5)
$card = new VCard();
}
ksort($cards);
return $cards;
}
function print_vcard($card, $hide)
{
$names = array('N', 'FN', 'TITLE', 'TEL', 'EMAIL', 'URL', 'ADR', 'NOTE');
$row = 0;
foreach ($names as $name) {
if (in_array_case($name, $hide)) {
continue;
}
$properties = $card->getProperties($name);
if ($properties) {
foreach ($properties as $property) {
$show = true;
$types = $property->params['TYPE'];
if ($types) {
foreach ($types as $type) {
if (in_array_case($type, $hide)) {
$show = false;
break;
}
}
}
if ($show) {
$class = ($row++ % 2 == 0) ? "property-even" : "property-odd";
print_vcard_property($property, $class, $hide);
}
}
}
}
}
function print_vcard_property($property, $class, $hide)
{
$name = $property->name;
$value = $property->value;
$types = $property->params['TYPE'];
if ($types) {
print_r(array_filter($types));
}
switch ($name) {
case 'N':
$name = $property->getComponents();
print_r(array_filter($name));
break;
case 'TEL':
$tel = $property->getComponents();
print_r(array_filter($tel));
break;
case 'FN':
$company = $property->getComponents();
print_r(array_filter($company));
break;
case 'ADR':
$adr = $property->getComponents();
print_r(array_filter($adr));
break;
case 'EMAIL':
$email = $property->getComponents();
print_r(array_filter($email));
break;
case 'URL':
$url = $property->getComponents();
print_r(array_filter($url));
break;
default:
$components = $property->getComponents();
$lines = array();
foreach ($components as $component) {
if ($component) {
$lines[] = $component;
}
}
$html = join("\n", $lines);
break;
}
echo "<br>";
echo "<br><br>";
}
It works quite well when I am using the sample vcard file, which looks like this:
BEGIN:VCARD
N:Smith;Jim;Alvin;Mr.
FN:Jim A. Smith
CATEGORIES:Family
BDAY:1977-01-27
ADR;WORK:;Suite 900;11 5th Street;Coco Beach;FL;32082
ADR;HOME:;;198 Elm Street;Coco Beach;FL;32082
TEL;WORK:904-555-9384;
TEL;HOME:904-873-0394
TEL;CELL:904-934-3429
END:VCARD
But my vcard from Apple Contacts looks a little bit different:
BEGIN:VCARD
N:Underwood;Frank;;;
FN:Frank Underwood
item1.EMAIL;type=INTERNET;type=pref:frank.underwood#hoc.com
item2.TEL;type=pref:+01 321 323123123
item2.X-ABLabel:Mobil
item3.ADR;type=pref:;;Richmondstreet 21;Washington D.C;;12312;
item4.URL;type=pref:http://www.frankunderwood.com/
item4.X-ABLabel:_$!<HomePage>!$_
CATEGORIES:Friends
END:VCARD
So in my example only the name of the person is printed, not the email, phone, address or url. So in the parsing I need to remove item1,item2 and so on. But I do not know how to do that.
Here is a solution how to export the Apple Contacts into a proper vCard file, that can be parsed correctly:
Go to Contacts/Settings/vCard and change the format to 2.1 and Unicode (UTF-8)

How to get both array key from value in 2 dimensional array (PHP)

$arr['animal'][0] = 'Dog';
$arr['animal'][1] = 'Cat';
From that array basically I need to create a function with the array value parameter and then it gives me the array keys.
For example:
find_index('Cat');
Output :
The result is animal, 1
You could probably do something like
function find_index($value) {
foreach ($arr as $index => $index2) {
$exists = array_search($value, $index2);
if ($exists !== false) {
echo "The result is {$index}, {$exists}";
return true;
}
}
return false;
}
Try this:
$arr['animal'][0] = 'Dog';
$arr['animal'][1] = 'Cat';
function find_index($searchVal, $arr){
return array_search($searchVal, $arr);
}
print_r(find_index('Cat', $arr['animal']));
Consider this Array,
$arr['animal'][] = 'Dog';
$arr['animal'][] = 'Cat';
$arr['insects'][] = 'Insect1';
$arr['insects'][] = 'Insect2';
Here is Iterator Method,
$search = 'InsectSub1';
$matches = [];
$arr_array = new RecursiveArrayIterator($arr);
$arr_array_iterator = new RecursiveIteratorIterator($arr_array);
foreach($arr_array_iterator as $key => $value)
{
if($value === $search)
{
$fill = [];
$fill['category'] = $arr_array->key();
$fill['key'] = $arr_array_iterator->key();
$fill['value'] = $value;
$matches[] = $fill;
}
}
if($matches)
{
// One or more Match(es) Found
}
else
{
// Not Found
}
$arr['animal'][] = 'Dog';
$arr['animal'][] = 'Cat';
$arr['insects'][] = 'Insect1';
$arr['insects'][] = 'Insect2';
$search_for = 'Cat';
$search_result = [];
while ($part = each($arr)) {
$found = array_search($search_for, $part['value']);
if(is_int($found)) {
$fill = [ 'key1' => $part['key'], 'key2' => $found ];
$search_result[] = $fill;
}
}
echo 'Found '.count($search_result).' result(s)';
print_r($search_result);

ChildNodes of DOMDocument lost order

I have a html string. I want to traverse it and extract some information. My code is as following:
$str = '<p>aaa</p><img src="http://stackoverflow.com/questions/ask"/><p>sss</p><img src="http://stackoverflow.com/"/>';
function parseContent($str) {
$contents = array();
$dom = new DOMDocument('1.0', 'UTF-8');
if (!$dom->loadHTML($str)) {
return $contents;
}
$stack = array($dom);
while (count($stack) > 0) {
$node = array_shift($stack);
foreach ($node->childNodes as $node) {
if ($node->hasChildNodes()) {
$stack[] = $node;
} else {
switch ($node->nodeType) {
case XML_ELEMENT_NODE:
if ('img' == $node->tagName) {
$contents[] = $node->attributes->getNamedItem('src')->nodeValue;
}
break;
case XML_TEXT_NODE:
$contents[] = $node->textContent;
break;
}
}
}
}
return $contents;
}
The problem is: When I dumped the return value of this function, it was something like this:
array(
'http://stackoverflow.com/questions/ask',
'http://stackoverflow.com/',
'aaa',
'sss',
)
Could someone point it out why the order was lost?
Extending from comment:
That's because each <p> also has child node (a text node), so they go into the first if ($node->hasChildNodes()) statement and are stacked one more time.
To avoid this, one way is to add one more condition:
/* ... */
if ($node->hasChildNodes()) {
if ($node->childNodes->length==1 && $node->childNodes->item(0)->nodeType==XML_TEXT_NODE) {
$contents[] = $node->childNodes->item(0)->textContent;
} else {
$stack[] = $node;
}
} else {
/* ... */

Create array out of XML data

I have a simple xml object like this as a result of Zend_Rest_Client_Result Object.
<userdetails>
<name value="jake"/>
<type value="user"/>
<attribute name="fname">
<value>Jake</value>
</attribute>
<attribute name="lname">
<value>Gordon</value>
</attribute>
<attribute name="phone">
<value>123-555-1234</value>
<value>999-888-7777</value>
</attribute>
</userdetails>
How can I create an array like this from the above XML object using PHP?
$userDetails = array();
$userDetails["name"] = "jake";
$userDetails["type"] = "user";
$userDetails["fname"] = "Jake";
$userDetails["lname"] = "Gordon";
$userDetails["phone"][0] = "123-555-1234";
$userDetails["phone"][1] = "123-555-1234";
Thanks,
$xml = '<userdetails><name value="jake"/><type value="user"/><attribute name="fname"><value>Jake</value></attribute><attribute name="lname"><value>Gordon</value></attribute><attribute name="phone"><value>123-555-1234</value><value>999-888-7777</value></attribute></userdetails>';
$simple = new SimpleXMLElement($xml);
$array = array();
if(isset($simple->name))
$array['name'] = (string) $simple->name->attributes()->value;
if(isset($simple->type))
$array['type'] = (string) $simple->type->attributes()->value;
if($foreach = $simple->xpath('//attribute[#name="fname"]/value'))
{
foreach($foreach as $node)
{
$array['fname'] = (string) $node;
}
}
if($foreach = $simple->xpath('//attribute[#name="lname"]/value'))
{
foreach($foreach as $node)
{
$array['lname'] = (string) $node;
}
}
if($foreach = $simple->xpath('//attribute[#name="phone"]/value'))
{
$array['phone'] = array();
foreach($simple->xpath('//attribute[#name="phone"]/value') as $node)
{
$array['phone'][] = (string) $node;
}
}
print_r($array);
function xmlstr_to_array($xmlstr) {
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
$output = array();
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
$child = $node->childNodes->item($i);
$v = domnode_to_array($child);
if(isset($child->tagName)) {
$t = $child->tagName;
if(!isset($output[$t])) {
$output[$t] = array();
}
$output[$t][] = $v;
}
elseif($v) {
$output = (string) $v;
}
}
if(is_array($output)) {
if($node->attributes->length) {
$a = array();
foreach($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string) $attrNode->value;
}
$output['#attributes'] = $a;
}
foreach ($output as $t => $v) {
if(is_array($v) && count($v)==1 && $t!='#attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
?>
Another quick and dirty method is:
<?php
$a = json_decode(json_encode((array) simplexml_load_string($s)),1);
?>
This is less robust and will create problem you code where to contain CDATA nodes as well.

compare object properties and show diff in PHP

I'm searching for a way to show me the different properties/values from given objects...
$obj1 = new StdClass; $obj1->prop = 1;
$obj2 = new StdClass; $obj2->prop = 2;
var_dump(array_diff((array)$obj1, (array)$obj2));
//output array(1) { ["prop"]=> int(1) }
This works very well as long the property is not a object or array.
$obj1 = new StdClass; $obj1->prop = array(1,2);
$obj2 = new StdClass; $obj2->prop = array(1,3);
var_dump(array_diff((array)$obj1, (array)$obj2))
// Output array(0) { }
// Expected output - array { ["prop"]=> array { [1]=> int(2) } }
Is there a way to get rid of this, even when the property is another object ?!
Something like the following, which iterates through and does a recursive diff is the item in the array is itself an array could work:
Des similar work to array_diff, but it does a check to see if it is an array first (is_array) and if so, sets the diff for that key to be the diff for that array. Repeats recursively.
function recursive_array_diff($a1, $a2) {
$r = array();
foreach ($a1 as $k => $v) {
if (array_key_exists($k, $a2)) {
if (is_array($v)) {
$rad = recursive_array_diff($v, $a2[$k]);
if (count($rad)) { $r[$k] = $rad; }
} else {
if ($v != $a2[$k]) {
$r[$k] = $v;
}
}
} else {
$r[$k] = $v;
}
}
return $r;
}
It then works like this:
$obj1 = new StdClass; $obj1->prop = array(1,2);
$obj2 = new StdClass; $obj2->prop = array(1,3);
print_r(recursive_array_diff((array)$obj1, (array)$obj2));
/* Output:
Array
(
[prop] => Array
(
[1] => 2
)
)
*/
My solution will recursively diff a stdClass and all it nested arrays and stdClass objects.
It is meant to be used for comparison of rest api responses.
function objDiff($obj1, $obj2):array {
$a1 = (array)$obj1;
$a2 = (array)$obj2;
return arrDiff($a1, $a2);
}
function arrDiff(array $a1, array $a2):array {
$r = array();
foreach ($a1 as $k => $v) {
if (array_key_exists($k, $a2)) {
if ($v instanceof stdClass) {
$rad = objDiff($v, $a2[$k]);
if (count($rad)) { $r[$k] = $rad; }
}else if (is_array($v)){
$rad = arrDiff($v, $a2[$k]);
if (count($rad)) { $r[$k] = $rad; }
// required to avoid rounding errors due to the
// conversion from string representation to double
} else if (is_double($v)){
if (abs($v - $a2[$k]) > 0.000000000001) {
$r[$k] = array($v, $a2[$k]);
}
} else {
if ($v != $a2[$k]) {
$r[$k] = array($v, $a2[$k]);
}
}
} else {
$r[$k] = array($v, null);
}
}
return $r;
}
Here is a comparison function that I built using the pattern:
function objEq(stdClass $obj1, stdClass $obj2):bool {
$a1 = (array)$obj1;
$a2 = (array)$obj2;
return arrEq($a1, $a2);
}
function arrEq(array $a1, array $a2):bool {
foreach ($a1 as $k => $v) {
if (array_key_exists($k, $a2)) {
if ($v instanceof stdClass) {
$r = objEq($v, $a2[$k]);
if ($r === false) return false;
}else if (is_array($v)){
$r = arrEq($v, $a2[$k]);
if ($r === false) return false;
} else if (is_double($v)){
// required to avoid rounding errors due to the
// conversion from string representation to double
if (abs($v - $a2[$k]) > 0.000000000001) {
return false;
}
} else {
if ($v != $a2[$k]) {
return false;
}
}
} else {
return false;
}
}
return true;
}
Usage:
$apiResponse = apiCall(GET, $objId);
$responseObj = json_decode($apiResponse);
// do stuff ...
if(!objEq($myObj, $responseObj) apiCall(PUT, $myObj, $objId);
Note that the apiCall function is just a mock to illustrate the concept.
Also this solution is incomplete because it does not take into account any key->value pairs that are unique to obj2. In my use case this is not required and could be neglected.
NB: I borrowed heavily from Peter Hamiltons contribution. If you like what I did then please upvote his solution. Thanks!

Categories