【问题标题】:400 Bad Request with Curl Php SSL Authentication带有 Curl Php SSL 身份验证的 400 错误请求
【发布时间】:2015-12-28 18:30:58
【问题描述】:

我快疯了!我阅读了一千篇关于 Curl PHP for SSL Auth 的 400 Bad Request 错误的帖子,但我从未找到详尽的答案。

这就是我的 sn-p:

ini_set('display_errors','On');
error_reporting(E_ALL);

$username=''; // To Set
$password=''; //To Set

// CODICE DAVIDE

 $ch = curl_init();//
 curl_setopt($ch, CURLOPT_URL, 'https://webstudenti.unica.it/esse3/Home.do');
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HEADER, true);
 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
 curl_setopt($ch, CURLOPT_VERBOSE, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 $response = curl_exec($ch);

 preg_match("/Set-cookie: (.*)\n/iU", $response, $matches);
 $cookie = trim(substr($matches[1], strpos($matches[1],':')));

 $url = "https://webstudenti.unica.it/esse3/auth/Logon.do";
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
 curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password);
 curl_setopt($ch, CURLOPT_HEADER, true);
 curl_setopt($ch, CURLOPT_COOKIE, $cookie);
 curl_setopt($ch, CURLOPT_COOKIESESSION, true);
 curl_setopt($ch, CURLOPT_VERBOSE, true);
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

 $response = curl_exec($ch);
 curl_close($ch);

 var_dump($response);

可能是什么?我真的找不到解决办法。我正在使用带有 Microsoft Azure 的 Windows Server,Php 版本:5.4。 谢谢你

【问题讨论】:

  • 你试过设置CURLOPT_USERAGENT吗?也许服务器需要知道它不是请求数据的机器人或爬虫?
  • 是的,我试过这个: curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/ 2.0.0.13');但在此之后,我收到 401 未授权(没有意义,用户名和密码是正确的)

标签: php authentication ssl curl


【解决方案1】:

我能够使用下面的 sn-p 成功连接。我为第二个请求添加了新变量$curl,而不是再次使用$ch

我收到HTTP/1.1 401 Unauthorized,因为未设置用户/密码。设置用户/密码试试这个,让我知道你发现了什么。

$curl = curl_init();
$url = "https://webstudenti.unica.it/esse3/auth/Logon.do";
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt($curl, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_VERBOSE, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // Might need this, but I was able to verify it works without
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');

$response = curl_exec($curl);
curl_close($curl);
var_dump($response);

【讨论】:

  • 完美!使用 curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);这行得通!我总是在标题上收到 401(我不知道为什么),但会话有效!在那之后,一个好奇心,如果我需要在页面之间导航,我需要编写什么代码以及我可以删除什么?
  • @AsoStrife 很高兴它有效,认为在您的情况下可能需要它,但不确定没有设置用户/密码。不要忘记为未来的访问者标记答案为正确。回复:其他问题......您应该能够在登录后使用额外的卷曲导航到页面。您需要正确处理 cookie,see this answer if needed
猜你喜欢
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2012-07-06
  • 2017-03-04
  • 2020-09-11
  • 2021-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多