Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

gather data from a url

Moving parts start on line 50.  Outputs:
2007-01-01 01:01:01
1111 = John Smith
2222 = Apple

2007-02-02 02:02:02
1111 = Jane Smith
2222 = Banana




gather this data not from a code snippet
but from a url  (as in an api url)


so instead of


$obj = SimpleXML_Load_String($xml);

use url
https://www.formstack.com/api/forms?api_key=ABCDE&type=xml

which is
<response status="error"><error>A valid API key was not supplied</error></response>


<?php // RAY_temp_u08.php
error_reporting(E_ALL);
echo "<pre>";


// TEST DATA
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<response status="ok">
<submissions>
<submission>
<id>1001</id>
<timestamp>2007-01-01 01:01:01</timestamp>
<user_agent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)</user_agent>
<remote_addr>127.0.0.1</remote_addr>
<data>
<item>
<field>1111</field>
<value>John Smith</value>
</item>
<item>
<field>2222</field>
<value>Apple</value>
</item>
</data>
</submission>
<submission>
<id>1001</id>
<timestamp>2007-02-02 02:02:02</timestamp>
<user_agent>Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)</user_agent>
<remote_addr>127.0.0.1</remote_addr>
<data>
<item>
<field>1111</field>
<value>Jane Smith</value>
</item>
<item>
<field>2222</field>
<value>Banana</value>
</item>
</data>
</submission>
</submissions>
<total>2</total>
<pages>1</pages>
</response>
XML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);

// USE AN ITERATOR TO ACCESS THE PROPERTIES OF THE OBJECT
foreach ($obj->submissions->submission as $s)
{
    // SHOW THE TIMESTAMP
    $t = (string)$s->timestamp;
    echo PHP_EOL . $t;

    foreach ($s->data->item as $i)
    {
        // SHOW FIELD AND VALUE PROPERTIES
        $f = $i->field;
        $v = $i->value;
        echo PHP_EOL . "$f = $v";
    }
    echo PHP_EOL;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rgb192

ASKER

thanks