我想从 https://test24.highrisehq.com/tasks/upcoming.xml 访问一个 xml 文件使用 PHP。 以下是示例代码:
$xml = simplexml_load_file("https://test24.highrisehq.com/tasks/upcoming.xml");
由于连接是安全的,我收到一个错误:
Warning: simplexml_load_file(https://test24.highrisehq.com/tasks/upcoming.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in D:\wamp\www\xml\index.php on line 3
在访问文件之前,我需要在代码中的某处输入用户名和密码。谁能告诉他我怎样才能做到这一点?
问候 乌梅尔
最佳答案
cURL,你求cURL
! (说真的,cURL 很棒。向下滚动该链接,您会在下面找到 HTTP 身份验证示例)
// HTTP authentication
$url = "https://www.example.com/protected/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //see link below
$result = curl_exec($ch);
curl_close($ch);
echo $result;
您可以找到有关使用 CURLOPT_SSL_VERIFYPEER
的信息 here .
关于php - 如何验证https站点的用户名/密码以访问php中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6604892/