PHP Get XML from Remote URL with HTTP Authentication

后端 未结 5 1847
太阳男子
太阳男子 2021-01-14 02:21

I have a URL which gives an xml output. It requires a username and password which I can access through a browser using the format:

http://username:pa

相关标签:
5条回答
  • 2021-01-14 03:05

    http://pear.php.net/package/HTTP_Request2 allows you to do a

    $basicAuthRequest = new HTTP_Request2('http://user:password@url.com');
    

    There's also an example of using curl to set the CURL_USERPWD of the request here

    0 讨论(0)
  • 2021-01-14 03:11

    but it loads only integers. not loaded strings in xml content. refer the below set of result.

     [2] => SimpleXMLElement Object
                (
                    [id] => 145894
                    [name] => SimpleXMLElement Object
                        (
                        )
    
                    [description] => SimpleXMLElement Object
                        (
                        )
    
                    [start_date] => SimpleXMLElement Object
                        (
                        )
    
                    [end_date] => SimpleXMLElement Object
                        (
                        )
    
                    [allow_deep_link] => 1
                    [program_id] => 6981
                    [program_name] => SimpleXMLElement Object
                        (
                        )
    
                    [category_name] => SimpleXMLElement Object
                        (
                        )
    
                    [code] => SimpleXMLElement Object
                        (
                        )
    
                    [tracking_url] => SimpleXMLElement Object
                        (
                        )
    
                )
    
    0 讨论(0)
  • 2021-01-14 03:13

    For XML with basic auth URL try this

    $username = 'admin';
    $password = 'mypass';
    $server = 'myserver.com';
    
    $context = stream_context_create(array(
            'http' => array(
                'header'  => "Authorization: Basic " . base64_encode("$username:$password")
            )
        )
    );
    $data = file_get_contents("http://$server/", false, $context);
    $xml=simplexml_load_string($data);
    if ($xml === false) {
        echo "Failed loading XML: ";
        foreach(libxml_get_errors() as $error) {
            echo "<br>", $error->message;
        }
    } else {
        print_r($xml);
    }
    
    0 讨论(0)
  • 2021-01-14 03:19

    The format -- http://username:password@url.com -- is a convention the browser understands; but if you're making an HTTP request programmatically, you'll need to set the HTTP Headers for basic authentication. I don't think *simplexml_load_file* supports HTTP headers, but you could try using for example:

    fopen("http://$username:$password@url.com");
    
    0 讨论(0)
  • 2021-01-14 03:20

    You should try something like this :

    $url = "http://username:password@url.com";
    $xml = file_get_contents($url);
    $data = new SimpleXMLElement($xml);
    
    0 讨论(0)
提交回复
热议问题