简体   繁体   English

PHP cURL 请求返​​回 401,但适用于 Postman

[英]PHP cURL request return 401, but works with Postman

If I execute the Webservice call with Postman with these parameters:如果我使用以下参数使用 Postman 执行 Webservice 调用:

Content-Type: application/json
Cookie: xxxx
Body: json

The Webservice return successfully a JSON. Web 服务成功返回一个 JSON。

So, I generated the code with the Postman and paste it on PHP:因此,我使用 Postman 生成了代码并将其粘贴到 PHP 上:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "url",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "json",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "content-type: application/json",
    "cookie: xxxx",
    "postman-token: 7c771f8e-5c87-1d27-64f3-bdb92bba6a19"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

And the Webservice is returning an error 401. But it's working when I execute it on Postman.并且 Web 服务返回错误 401。但是当我在 Postman 上执行它时它正在工作。 What am I doing differently from Postman?我和 Postman 有什么不同?

401 error code is usually been because some authorize issues :- 401 错误代码通常是因为一些授权问题:-

401 Unauthorized 401 未授权

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.响应必须包含一个 WWW-Authenticate 头域(第 14.47 节),其中包含适用于所请求资源的质询。 The client MAY repeat the request with a suitable Authorization header field (section 14.8).客户端可以使用合适的 Authorization 头域(第 14.8 节)重复请求。 If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials.如果请求已包含授权凭证,则 401 响应表明对这些凭证的授权已被拒绝。 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.如果 401 响应包含与先前响应相同的质询,并且用户代理已经至少尝试过一次身份验证,那么应该向用户呈现响应中给出的实体,因为该实体可能包含相关的诊断信息。 HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" HTTP 访问认证在“HTTP 认证:基本和摘要式访问认证”中有解释

which is mean that you are facing problems in authentication your request这意味着您在验证您的请求时遇到问题

often this will be fixed by simulating the web request通常这将通过模拟Web 请求来解决

for example :-例如 :-

assume that you have some page which login is needed to access to it ,假设您有一些需要登录才能访问的页面,

then you will have two requests you should do那么你将有两个你应该做的请求

1- post some login authentication data 2- send the cookies which returned from the first request , to the second request header 1- 发布一些登录验证数据 2- 将从第一个请求返回的 cookie 发送到第二个请求标头

you will need to read more about cookies in cURL in php manual pages您需要在 php 手册页中阅读有关cURL中 cookie 的更多信息

specially CURLOPT_COOKIEJAR , CURLOPT_COOKIEJAR and CURLOPT_COOKIE options特别是CURLOPT_COOKIEJAR , CURLOPT_COOKIEJAR and CURLOPT_COOKIE选项

I had a similar issue.我有一个类似的问题。 In postman it was working but through curl it was giving 401 error.在邮递员中它正在工作,但通过 curl 它给出了401错误。

Now it is working for me with below code.现在它使用以下代码为我工作。 I have used base64_encode for username and password.我使用base64_encode作为用户名和密码。

Manual Page Ref: https://www.php.net/manual/en/function.curl-setopt.php手册页参考: https : //www.php.net/manual/en/function.curl-setopt.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Basic ".base64_encode($username.":".$password), ]);  
$result=curl_exec ($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
echo "<br/>http status code is ".$status_code;

Output:输出:

http status code is 200

Remove the following line from your .htaccess:从您的 .htaccess 中删除以下行:

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0

It worked for me!它对我有用!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM