0

I want to grab data from a website say www.example.com/stations with XML output:

<stations>
    <station>
        <name>Loppersum</name>
        <code>LP</code>
        <country>NL</country>
        <lat>53.334713</lat>
        <long>6.7472625</long>
        <alias>false</alias>
    </station>
    <station>
         <name>Ludinghausen</name>
         <code>ELDH</code>
         <country>D</country>
         <lat>51.76184</lat>
         <long>7.43165</long>
         <alias>true</alias>
    </station>
</stations>

But the url is protected by a password and username (I have that).

I thought that I can use the cURL function, but i never used it before. Can I store the data also as a object?

EDIT:
It is a HTTP Authorization and I use PHP

Thom
  • 167
  • 1
  • 1
  • 10
  • 1
    There's a similar answer about HTTP auth and cURL here, http://stackoverflow.com/a/2140445/209585 – lsouza May 09 '14 at 19:15

1 Answers1

0

You didn't specify what kind of login scheme is in use.

If you're up against HTTP authorization, you can simply use the -u argument with curl. See this answer: Using cURL with a username and password?

If you're up against cookie authorization, it gets a bit more complicated. You'll most likely need to act as a web browser and "login" to the website, and then perform your request. Both requests will need access to a cookie jar/file that you provide to curl.

Edit: The author indicated that this is HTTP authorization using PHP.

The solution would be to use PHP's SimpleXMLElement to get the XML object. You can use Curl to download the XML data and pass it into the constructor, or you can have SimpleXMLElement do it for you.

Try this:

$user = 'someuser';
$pass = 'somepass';
$url  = "http://$someuser:$somepass@example.com/stations";
$obj  = new SimpleXMLElement($url, NULL, TRUE);

echo $obj->movie[0]->title; // example

Hope that helps.

Community
  • 1
  • 1
Carl Bennett
  • 828
  • 6
  • 15
  • It is an HTTP authorization – Thom May 09 '14 at 19:21
  • Providing the `-u` argument should work, then. As for the object, I'm not quite sure. Is this being requested from the shell? – Carl Bennett May 09 '14 at 19:25
  • No, I do it with php, so not with the commandprompt – Thom May 09 '14 at 19:30
  • In PHP, you can use the `SimpleXMLElement` to get what you want. Once you get the data, pass it to the constructor, like this: `$obj = new SimpleXMLElement($response);`. You can also tell it that `$response` is a URL instead of XML data by setting the third parameter to `TRUE`. More info: http://php.net/simplexmlelement.construct.php – Carl Bennett May 09 '14 at 19:37