douxing8855 2017-04-21 07:23
浏览 49
已采纳

通过身份验证从URL检索XML

I have been all over the internet trying to find a solution to my specific problem but no luck.

Basically I have a URL that I log into that looks similar to this: https://some-website.university.edu.au:8781/elements/v4.9/users/

Which will return to the browsers an XML block of text with all of the users.

I am looking to use curl or SimpleXMLElement() or whatever it takes to bring that XML into my php variable and output it.

The closest I feel I have got is:

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

or

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

I'm not sure if either is close or whether curl is better than SimpleXMLElement() or if one or both just will never work.

I have added a screenshots to show the what is returned on the website. The login screen is just the browser default one. Any help would be amazing. Thanks!

XML Returned on web page

  • 写回答

2条回答 默认 最新

  • douhuibo5635 2017-04-28 07:24
    关注

    You might be better off using curl imo: You can try something like this for authentication with curl:

    $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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)