在XMLReader中指定凭据

时间:2018-02-26 18:33:38

标签: php xml simplexml credentials xmlreader

我正在尝试使用XMLReader访问XML提要(使用用户名和密码) 以前,我已将凭证整合到网址中(例如http://username:password@mysite.com);但是,这现在不起作用。

我在代码的最后检查中得到'XPath query failed for bio'

是否可以在XMLReader中指定用户名/密码?

感谢任何线索。

我的代码(编辑后包含我的卷曲代码):

<?php
$secondary_user_id = "jsmith";
$url_bio = "http://username:password@mysite.com";

//
$a_username='username';
$a_password='password';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_bio);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_USERPWD, $a_username . ':' . $a_password);
$result = curl_exec($ch);
$a_error = curl_error($ch);
echo '<br>'.$a_error.'<br>';
curl_close($ch);
//



$reader = new XMLReader();
//$reader->open($url_bio);
$reader->XML($result);
while ($reader->read()) 
{

if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'Users') 
    {
            //the code works to this point  
            echo 'success<br>';
            $node = $reader->expand();
            $doc = new DOMDocument('1.0','UTF-8');//
            $n = $doc->importNode($node,true);
            $doc->appendChild($n);
            //$xml_bio_report = simplexml_import_dom($doc->importNode($reader->expand(),true));//
            $xml_bio_report =  simplexml_import_dom($n);//

        $xml_bio_report->registerXPathNamespace('xlink','http://www.w3.org/1999/xlink');
        $xml_bio_report->registerXPathNamespace('dmu','http://www.digitalmeasures.com/schema/user-metadata');

          //echo $xml_bio_report->Users->User;


        $xml_bio_report_abbrev = $xml_bio_report->xpath('//User[@SecondaryID="'.$secondary_user_id.'"]');

        if ($xml_bio_report_abbrev){
            echo '<h1>'.$xml_bio_report_abbrev[0]['username'].'</h1>';
            echo '<h1>'.$xml_bio_report_abbrev[0]['SecondaryID'].'</h1>';

        } else {
            echo 'XPath query failed for bio';  
        }

}
}
?>

0 个答案:

没有答案