gpt4 book ai didi

php - 未生成 Cookie 文件 PHP 和 cURL 从远程站点登录

转载 作者:搜寻专家 更新时间:2023-10-31 21:00:16 25 4
gpt4 key购买 nike

根据下面的代码,我没有收到 $loginUrl 的成功登录(即没有在与文件相同的目录中生成 cookie.txt 文件),因此我无法从 $url 加载 HTML 数据(即 echoes 做了不加载)。当我查看 loginUrl 的 curl_exec 时,它似乎没有将用户名和密码提交到表单,尽管我有 $store = curl_exec($ch),因为显示的是表单而不是成功登录。

function parseDOM($data)
{
global $projectID, $sRedirect, $database;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if(!$dom->loadHTML($data))
{
echo "did not load";
}
}

$ch = @curl_init();
if($ch)
{
$username = 'username';
$password = 'password';
//$url = 'https://global-factiva-com.libproxy.lib.unc.edu/ha/default.aspx#./!?&_suid=14977301633480007720669669887936';
//trying different URL
$url = 'https://global.factiva.com.libproxy.lib.unc.edu/redir/default.aspx?P=sa&NS=16&AID=9UNI011500&f=g&an=j000000020010807dw8b00lc2&cat=a';
//loginUrl is the same as the URL for the form post action
$loginUrl = 'https://sso.unc.edu/idp/profile/SAML2/POST/SSO;jsessionid=A2C0B6480084BED37E1104E903B07AA9?execution=e1s1';

//Set the URL to work with
curl_setopt($ch, CURLOPT_URL, $loginUrl);
// ENABLE HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
//Set the post parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'j_username='.$username.'&j_password='.$password);
//Handle cookies for the login
$cookie=dirname(__FILE__)."\\cookie.txt";
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
//execute the request (the login)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$store = curl_exec($ch);

//now access the URL that requires login
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$content=curl_exec($ch);
$headers = curl_getinfo($ch);

curl_close($ch);
parseDOM($content);

}

最佳答案

这是我会使用的方法。首先,使用谷歌浏览器并打开网络检查器。如果您随后手动登录,那么您将能够看到所有发送的请求 header 、表单字段等。

有了这些信息,您就可以构造一个 curl 请求并指定所有自定义 header 。之前我曾使用过拒绝没有合法引荐来源网址或用户代理的请求的系统。

所以,例如..

<?php

$username = 'hello';
$password = 'letmein';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://sso.unc.edu/idp/profile/SAML2/POST/SSO?execution=e1s1");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,'j_username:='.$username.'&j_password:='.$password.'&_eventId_proceed:=');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$headers = [
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding:gzip, deflate, br',
'Accept-Language:en-US,en;q=0.8,es;q=0.6',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'Content-Length:57',
'Content-Type:application/x-www-form-urlencoded',
'Host:sso.unc.edu',
'Origin:https://sso.unc.edu',
'Referer:https://sso.unc.edu/idp/profile/SAML2/POST/SSO?execution=e1s1',
'Upgrade-Insecure-Requests:1',
'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
];

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$output = curl_exec ($ch);

curl_close ($ch);

echo $output;

?>

一旦你运行它,那么希望你已经登录并设置了 cookie。然后,您可以使用新的 curl_init() 向第二个 URL 发出第二个请求,并包含 CURLOPT_COOKIEFILECURLOPT_COOKIEJAR 参数。

希望这能为您提供一些帮助。祝你好运。

关于php - 未生成 Cookie 文件 PHP 和 cURL 从远程站点登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44610671/

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