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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?