PHP Curl-Cookies问题

PHP Curl-Cookies问题,第1张

PHP Curl-Cookies问题

我写了这段代码,它对我很好,在上一个var_dump中我看到了我的账户信息之类的。如果你不删除cookies,你可以使用登录名对受保护的页面发出后续curl请求。

希望这能帮助你学会如何去做。很多时候您需要访问登录页才能设置cookies的站点,以及通常在需要提交的表单上有csrf令牌

当然,如果amazon稍微更改了表单或url,就会有

去适应一些,但希望他们不要经常这样做。

<?php$email    = 'you@yoursite.com';$password = 'password';// initial login page which redirects to correct sign in page, sets some cookies$URL = 'https://affiliate-program.amazon.com/gp/associates/join/landing/main.html';$ch  = curl_init();curl_setopt($ch, CURLOPT_URL, $URL);curl_setopt($ch, CURLOPT_cookieJAR, 'amazoncookie.txt');curl_setopt($ch, CURLOPT_cookieFILE, 'amazoncookie.txt');curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0');curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HEADER, 1);//curl_setopt($ch, CURLOPT_VERBOSE, true);curl_setopt($ch, CURLOPT_STDERR,  fopen('php://stdout', 'w'));curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);$page = curl_exec($ch);//var_dump($page);exit;// try to find the actual login formif (!preg_match('/<form name="sign_in".*?</form>/is', $page, $form)) {    die('Failed to find log in form!');}$form = $form[0];// find the action of the login formif (!preg_match('/action=(?:'|")?([^s'">]+)/i', $form, $action)) {    die('Failed to find login form url');}$URL2 = $action[1]; // this is our new post url// find all hidden fields which we need to send with our login, this includes security tokens$count = preg_match_all('/<input type="hidden"s*name="([^"]*)"s*value="([^"]*)"/i', $form, $hiddenFields);$postFields = array();// turn the hidden fields into an arrayfor ($i = 0; $i < $count; ++$i) {    $postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i];}// add our login values$postFields['username'] = $email;$postFields['password'] = $password;$post = '';// convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlenpredforeach($postFields as $key => $value) {    $post .= $key . '=' . urlenpre($value) . '&';}$post = substr($post, 0, -1);// set additional curl options using our previous optionscurl_setopt($ch, CURLOPT_URL, $URL2);curl_setopt($ch, CURLOPT_REFERER, $URL);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post);$page = curl_exec($ch); // make requestvar_dump($page); // should be logged in


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5441574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存