gpt4 book ai didi

php - 使用 Curl Php SSL 身份验证的 400 错误请求

转载 作者:太空宇宙 更新时间:2023-11-03 13:37:38 25 4
gpt4 key购买 nike

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

所以这是我的片段:

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。谢谢

最佳答案

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

关于php - 使用 Curl Php SSL 身份验证的 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34498779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com