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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!