php [function.fopen]:无法打开流:HTTP 请求失败!HTTP/1.1 401 未经授权

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3334359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:22:58  来源:igfitidea点击:

[function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

phpfopen

提问by Hailwood

[function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized

[function.fopen]:无法打开流:HTTP 请求失败!HTTP/1.1 401 未经授权

I am periodically getting this error, i.e it is only happening at random times, any ideas what would cause this, i have checked my php.ini and allow_url_fopen is set to true.

我定期收到此错误,即它仅在随机时间发生,任何想法会导致此错误,我已检查我的 php.ini 并将 allow_url_fopen 设置为 true。

采纳答案by teemitzitrone

10.4.2 401 Unauthorized

The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].

10.4.2 401 未授权

该请求需要用户身份验证。响应必须包含一个 WWW-Authenticate 头域(第 14.47 节),其中包含适用于所请求资源的质询。客户端可以使用合适的 Authorization 头域(第 14.8 节)重复请求。如果请求已包含授权凭证,则 401 响应表明对这些凭证的授权已被拒绝。如果 401 响应包含与先前响应相同的质询,并且用户代理已经至少尝试过一次身份验证,那么应该向用户呈现响应中给出的实体,因为该实体可能包含相关的诊断信息。HTTP 访问身份验证在“HTTP 身份验证:

Status Code Definitions

状态代码定义

回答by Artefacto

If the site requires basic authentication, you can give your credentials this way:

如果站点需要基本身份验证,您可以通过以下方式提供您的凭据:

fopen("http://user:[email protected]/path/to/resource", "r");

If it uses digest authentication, you'll have to handle it manually by reading the headers of the failed response and sending a new one with the correct headers. See HTTP context optionsfor how to read and set headers and see how digest works in HTTP authentication with PHP.

如果它使用摘要式身份验证,则必须通过读取失败响应的标头并发送带有正确标头的新标头来手动处理它。有关如何读取和设置标头的信息,请参阅HTTP 上下文选项,并了解摘要在使用 PHP 的 HTTP 身份验证中的工作原理。

You can also use the cURL extensionor the HTTP PECL extension.

您还可以使用cURL 扩展HTTP PECL 扩展

回答by vtorhonen

You are trying to access a site that requires authentication, as maggie pointed out. fopen()does not support HTTP Basic Authentication, so you must use the Client URL Libraryto achieve such functionalities.

正如玛吉指出的那样,您正在尝试访问需要身份验证的站点。fopen()不支持HTTP 基本身份验证,因此您必须使用客户端 URL 库来实现此类功能。

This has been discussed before.

这个之前已经讨论过了。

回答by user3032727

task - fopen with basic http authentication.

任务 - fopen 具有基本的 http 身份验证。

$auth_header = 'Authorization: Basic '.base64_encode("$user:$password");

$f = fopen($url, $open_mode,false, stream_context_create(
    'http'=>array(
        'header' => array($auth_header,$some_other_header,$some_yet_header),
    ),
));