PHP基本身份验证file

编程入门 行业动态 更新时间:2024-10-20 05:38:05
本文介绍了PHP基本身份验证file_get_contents()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从网站解析一些XML数据. XML数据为原始格式,但是在我需要进行身份验证之前(基于基本网络服务器的身份验证,使用用户名和密码).

I need to parse some XML data from a website. The XML data is in raw format, but before I need to authentificate (basic webserver based auth, with username & password).

我尝试过:

$homepage = file_get_contents('user:password@IP:PORT/folder/file');

但出现以下错误:

无法打开流:HTTP请求失败! HTTP/1.0 401未经授权

failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized

PHP似乎在身份验证方面存在问题.知道如何解决这个问题吗?

PHP seems to have problems with the authentification. Any idea how to fix this?

推荐答案

您将需要添加流上下文以获取请求中的额外数据.尝试类似以下未经测试的代码.它基于file_get_contents()的PHP文档上的示例之一:

You will need to add a stream context to get the extra data in your request. Try something like the following untested code. It's based on one of the examples on the PHP documentation for file_get_contents():

$auth = base64_encode("username:password"); $context = stream_context_create([ "http" => [ "header" => "Authorization: Basic $auth" ] ]); $homepage = file_get_contents("example/file", false, $context );

更多推荐

PHP基本身份验证file

本文发布于:2023-11-01 01:37:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   PHP   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!