400 Curl Php SSL身份验证的错误请求


400 Bad Request with Curl Php SSL Authentication

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

这是我的片段:

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);

它可能是什么?我真的找不到解决办法。我使用的是微软Azure的Windows服务器,Php版本:5.4。感谢

我能够使用下面的片段成功连接。我为第二个请求添加了新的变量$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);