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

1

1 Answer 1

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:[email protected]/stations";
$obj  = new SimpleXMLElement($url, NULL, TRUE);

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

Hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

It is an HTTP authorization
Providing the -u argument should work, then. As for the object, I'm not quite sure. Is this being requested from the shell?
No, I do it with php, so not with the commandprompt
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: php.net/simplexmlelement.construct.php

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.