如何使用Curl和SSL和cookielogin

我一直在试图用curllogin到barnesandnoble.com手机网站,迄今为止还没有运气。 我没有任何错误地返回页面,并且将我的电子邮件默认再次login到login页面的电子邮件input表单框(从print $ result返回的表单中)。

相同的代码实际上可以让我正确地进入易趣通过更改LOGINURL指向易趣的login

唯一的区别是,barnesandnobles是https://和ebaylogin是http://

另外,我相信barnes网站是asp / aspx,所以我不知道如何处理cookie和_state的不同

任何帮助将不胜感激,因为我一直在试图debugging过去16小时

另外,我的cookie.txt是可写和工作的

<?php $cookie_file_path = "C:/test/cookie.txt"; $LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)"; $ch = curl_init(); $headers[] = "Accept: */*"; $headers[] = "Connection: Keep-Alive"; $headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_URL, $LOGINURL); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); $content = curl_exec($ch); curl_close($ch); unset($ch); // NAME="path_state" value="6657403"> if(stristr($content,"path_state")){ $array1=explode('path_state" value="',$content); $content1=$array1[1]; $array2=explode('">',$content1); $content2=$array2[0]; } $LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $POSTFIELDS = "d_hidPageStamp=V_3_17&hidViewMode=opSignIn&stage=signIn&previousStage=mainStage&path_state=" . $content2 . "&emailAddress=YOUREMAILHERE@gmail.com&acctPassword=YOURPASSWORD"; $reffer = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $ch = curl_init(); $headers[] = "Accept: */*"; $headers[] = "Connection: Keep-Alive"; $headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_URL, $LOGINURL); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_REFERER, $reffer); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); $result = curl_exec($ch); print $result; ?> 

这是我从你的代码创build的一个工作示例。 这使用了一个函数getFormFields ,我写了一个类似的问题(在这篇文章的底部的第一个参考)login到Android市场。

我想在你的脚本中可能有一些阻止login工作的东西。 首先,你应该在后面的string中对URL参数(如电子邮件和密码)进行urlen编码 (cURL不会为你做这个)。 其次,我认为可能需要用作loginURL一部分的x值。

这是一个成功login的解决scheme。 请注意,我重新使用了原始的cURL句柄。 这不是必需的,但是如果指定保持活动状态,它实际上将重新使用相同的连接,并且也使您无需反复指定相同的选项。

一旦你有了cookie,你可以创build一个新的cURL对象并指定COOKIEFILE和COOKIEJAR,你将会在不执行第一步的情况下login。

 <?php // options $EMAIL = 'you@yoursite.com'; $PASSWORD = 'yourpassword'; $cookie_file_path = "/tmp/cookies.txt"; $LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)"; // begin script $ch = curl_init(); // extra headers $headers[] = "Accept: */*"; $headers[] = "Connection: Keep-Alive"; // basic curl options for all requests curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path); // set first URL curl_setopt($ch, CURLOPT_URL, $LOGINURL); // execute session to get cookies and required form inputs $content = curl_exec($ch); // grab the hidden inputs from the form required to login $fields = getFormFields($content); $fields['emailAddress'] = $EMAIL; $fields['acctPassword'] = $PASSWORD; // get x value that is used in the login url $x = ''; if (preg_match('/op\.asp\?x=(\d+)/i', $content, $match)) { $x = $match[1]; } //$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn"; $LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?x=$x"; // set postfields using what we extracted from the form $POSTFIELDS = http_build_query($fields); // change URL to login URL curl_setopt($ch, CURLOPT_URL, $LOGINURL); // set post options curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS); // perform login $result = curl_exec($ch); print $result; function getFormFields($data) { if (preg_match('/(<form action="op.*?<\/form>)/is', $data, $matches)) { $inputs = getInputs($matches[1]); return $inputs; } else { die('didnt find login form'); } } function getInputs($form) { $inputs = array(); $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); if ($elements > 0) { for($i = 0; $i < $elements; $i++) { $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { $name = $name[1]; $value = ''; if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { $value = $value[1]; } $inputs[$name] = $value; } } } return $inputs; } 

这对我有用,希望能帮助你走。

下面是我可以帮助学习的其他一些cURL答案:

  • 用curl检索Android电子市场mylibrary
  • 多curl的行动
  • 通过curl发送xml和头文件
  • 使用PHP和CurlloginGoogle,Cookieclosures了吗?
  • PinterestloginPHP和curl不起作用