-1

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.

basickarl
  • 37,187
  • 64
  • 214
  • 335
  • I have no idea what you want to do. – Tomer Aug 08 '12 at 09:04
  • I want to extract: http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=lund&inpPointTo=ystad - And display it in a table for the user. Exactly what XML is used for... – basickarl Aug 08 '12 at 09:11
  • `How do I get these two to collaborate` , we need to guess what that means, please explain clearly in the question what you want to do. – Tomer Aug 08 '12 at 09:13
  • You are very rude, i thought about helping you but you can forget about it. – Tomer Aug 08 '12 at 09:40
  • Sorry just having a bad day, theres absolutely no information on this subject on how to extract variables from the: "http://www.labs.skanetrafiken.se/v2.2/querypage.asp?inpPointFr=lund&inpPointTo=ystad". For example, I want it all in an PHP array or object. Wasn't directed at you directly. – basickarl Aug 08 '12 at 09:44
  • What you need to do is parse the XML. For that, goggle eg. `php parse xml` or if you want to interact with the service, maybe `php soap client`. Then you'd run through it in a loop, and render a table – Pekka Aug 08 '12 at 09:47
  • Pekka, I started up this topic: "http://stackoverflow.com/questions/11847124/php-xml-parse-from-website-url#comment15755909_11847124", still no help on the topic. I have tried the simplexml_load_file and SimpleXMLElement object. Still getting empty returns. – basickarl Aug 08 '12 at 09:55

1 Answers1

0

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.

basickarl
  • 37,187
  • 64
  • 214
  • 335