【问题标题】:PHP CURL GET request return 401 error (Auth fail)PHP CURL GET 请求返回 401 错误(验证失败)
【发布时间】:2020-01-26 12:21:31
【问题描述】:

首先我发送一个带有登录名和密码(管理员授权)的 POST 请求并获取一些字段。

接下来,我需要向 API 发送 GET 请求(检查客户端电子邮件是否存在),但我收到 CURL 错误:请求的 URL 返回错误:401

在 Postman GET 请求中,我做错了什么?

这是我的 GET 请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, "www.myurl.com/clients?email=a@a.a");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);

【问题讨论】:

  • 401 表示您未经授权。首先尝试找出您的授权服务器/资源服务器如何需要凭据?
  • @unclexo API 文档:“1)第一次连接必须使用登录名和密码完成” - 这是我的 POST 请求。 “2)如果身份验证cookies已经保存,你必须使用它们,60分钟后的会话” - 这个我不明白......我需要发送带有cookies选项的CURL请求吗?
  • 如果您有授权相关的cookies,请使用curl_setopt($ch, CURLOPT_COOKIE, "key1=value1;key2=value2");发送

标签: php curl


【解决方案1】:

您必须传递查询字符串以防 GET。 试试下面更新的代码。

$qry_str = "?email=a@a.a"
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'www.myurl.com/clients.php',$qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-21
    • 2017-07-16
    • 2014-04-07
    • 2019-07-24
    相关资源
    最近更新 更多