简体   繁体   English

我尝试使用PHP代码调用发布请求,但出现401未经授权错误。

[英]I tried to call a post request using PHP code, but I got 401 Unauthorized error.

I tried to send a post request using this PHP code but throw me 401 Unauthorized error: 我尝试使用此PHP代码发送发布请求,但抛出401未经授权错误:

$username = 'MyDomain\testuser';
$password = '123456';
$url = 'http://10.20.30.40:8080/TargetPage.aspx';

$data = array(
         'username' => $username,
         'password' => $password, 
         'postdata' => 'InputParameter1=Test1&InputParameter2=Test2'
);

$options = array(    
    'http' => array
            (
                'method'  => 'POST',
                'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
                'content' => http_build_query($data)   
            )           
);

$context  = stream_context_create($options);    
$result = file_get_contents($url, false, $context);

You're not doing proper basic auth. 您没有执行适当的基本身份验证。 You have to build the authenticate header yourself: 您必须自己构建authenticate标头:

$options = array(
    'http' => array(
        'header' => 'Authorization: Basic ' . base64_encode("$username:$password")
    )
);

You can't just shove a username/password elements into the array and expect it to work. 您不能只是将用户名/密码元素放入数组中并期望它能工作。 PHP doesn't know you're building a stream to HTTP basic auth... you have to provide EVERYTHING yourself. PHP不知道您正在构建到HTTP基本身份验证的流……您必须自己提供一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用php代码点击时使用gikom openfire REST API,错误显示为401未经授权 - Using gikom openfire REST API when hit using the php code , the error appears as 401 unauthorized 如何使用php curl正确调用API? 我收到401的http状态码 - How do i call an API using php curl correctly? I am getting http status code of 401 当我尝试使用Facebook ID登录网站时出现错误 - Got an error when i tried to login a website using facebook id ics php代码错误。 试图输入到phprunner - ics php code error. Tried to input this into phprunner ajax post请求401(未经授权)API Laravel - ajax post Request 401 (Unauthorized) API Laravel 为什么我在 Microsoft Graph API 调用中收到“401 Unauthorized”? - Why am I Getting "401 Unauthorized" for Microsoft Graph API Call? 将Sitefinity Web服务与php网站链接时,如何避免401 Unauthorized错误? - How do I avoid 401 Unauthorized error when linking a sitefinity web service with a php website? 使用PHP的GCM实现始终面临未授权错误401 - GCM Implementation using PHP always facing Unauthorized Error 401 当我尝试使用PHP访问JIRA REST API时未经授权(401) - Unauthorized (401) when I try to access JIRA REST API with PHP Laravel Ajax 表单发布显示 500 内部服务器错误。 我尝试了很多解决方案但仍然无法解决 - Laravel Ajax form post shows 500 internal server error. I tried solving with a lot of solution but still not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM