通过身份验证从URL中检索XML



我一直在互联网上找到解决我的特定问题的解决方案,但没有运气。

基本上,我有一个我登录的URL,看起来与此相似:https://some-website.university.edu.au:8781/elements/v4.9/users/

将返回带有所有用户的XML文本块。

我希望使用Curl或Simplexmlelement((或将XML带入我的PHP变量并输出的任何内容。

我觉得我拥有的最接近的是:

$username = 'usernameX';
$password = 'passwordX';
$URL = 'https://some-website.university.edu.au:8781/elements/v4.9/users/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
echo $result;

$url = 'https://usernameX:passwordX@some-website.university.edu.au:8781/elements/v4.9/users/';
echo $url."<br />";
$xml = new SimpleXMLElement($url);
print_r($xml);

我不确定要么接近还是卷发是否比单纯式((更好,或者一个或两个都永远无法使用。

我添加了屏幕截图以显示网站上返回的内容。登录屏幕只是浏览器默认一个。任何帮助都会很棒。谢谢!

XML在网页上返回

使用curl imo可能会更好:您可以尝试这样的事情以进行卷发:

$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);
}

从远程URL解析XML的另一种方法。(未测试(

<?php
$username = 'usernameX';
$password = 'passwordX';
$url = 'https://some-website.university.edu.au:8781/elements/v4.9/users/';
$context = stream_context_create(array(
    'http' => array(
        'header' => "Authorization: Basic " . base64_encode("$username:$password"),
        'header' => 'Accept: application/xml'
    )
));
$data = file_get_contents($url, false, $context);
$xml = simplexml_load_string($data);
print_r($xml);

php

  • stream_get_contents - 将流的其余部分读取到字符串
  • file_get_contents - 将整个文件读取到字符串
  • simplexml_load_string - 将XML字符串解释为对象

最新更新