简体   繁体   English

php curl可以登录网站,但登录后不显示任何数据

[英]php curl Able to login into site but shows no data after login

HI, HI,

I am able to login into the site using php and curl. 我可以使用php和curl登录该站点。 but when i try to read the data after logging in it shows string(0) "" if CURLOPT_RETURNTRANSFER enabled and when disabled it shows bool(true). 但是当我登录后尝试读取数据时,如果启用了CURLOPT_RETURNTRANSFER,则显示string(0)“”;而当禁用时,它显示bool(true)。 I dont understand where exactly i am going wrong... 我不明白我到底要去哪里...

any help is really appreciated 任何帮助都非常感谢

heres code 继承人代码

$cookie_file_path = "C:\\Apache2\\htdocs\\test.txt";


//$fp = fopen($cookie_file_path,'wb');   
//fclose($fp);

$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/3.6.0";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://kinray.com/redirector1/weblink.jsp");   
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_REFERER, "http://www.google.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Do not print anything now. 
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, "custId=XXXXX&passWord=YYYYY");

$html = curl_exec($ch);
var_dump(urlencode($html));

 curl_close();
 $ch1 = curl_init();

   curl_setopt($ch1, CURLOPT_URL,"https://kinray.com/weblink2/search/categories.do?scrollType=&code=&alwaysCustomerNumber=023987");   
   curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie_fie_path);
   curl_setopt($ch1, CURLOPT_COOKIEJAR, $cookie_file_path);
   curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch1,CURLOPT_HTTPGET,true);
   //curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 1);
   //curl_setopt($ch1, CURLOPT_HEADER, 1);
   curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
   curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
   $html2 = curl_exec($ch1);
   curl_close();
   echo "<hr>";
   var_dump( $html2);

Any clue where am i going wrong? 任何提示我要去哪里错了?

Your curl_close calls are broken. 您的curl_close通话中断。 Pass in the curl resource. 传递curl资源。 Eg: 例如:

curl_close($ch);

This is why you should enable and pay attention to warnings. 这就是为什么您应该启用并注意警告的原因。

php > curl_close();
PHP Warning:  curl_close() expects exactly 1 parameter, 0 given in php shell code on line 1

This in turn means your cookie jar won't work properly. 反过来,这意味着您的Cookie罐将无法正常运行。

Anytime you are having issues with curl, use fiddler to help you debug. 每当您遇到卷曲问题时,请使用提琴手帮助您进行调试。

Do the request using your browser, and inspect the traffic, then do it in curl and compare. 使用浏览器发出请求,并检查流量,然后卷曲并进行比较。 That will tell you what is different. 那会告诉你有什么不同。

Chances are good that there is a redirect that is happening upon verification of your credentials. 很有可能在验证您的凭据时发生重定向。 You can still use CURLOPT_FOLLOWLOCATION without having to actually print that to screen - is there a reason why you have it commented out? 您仍然可以使用CURLOPT_FOLLOWLOCATION而无需实际将其打印到屏幕上-是否有理由将其注释掉?

should these variables be different? 这些变量应该不同吗?

//                                              v
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch1, CURLOPT_COOKIEJAR,  $cookie_file_path);
//                                              ^

It may be that your cookies aren't being stored/retrieved properly. 可能是您的Cookie没有正确存储/检索。

httpRequest Class Example (short form static constructors): httpRequest类示例(简短形式的静态构造函数):

$loginAttempt = httpRequest::post(
        'https://kinray.com/redirector1/weblink.jsp',
        'custId=XXXXX&passWord=YYYYY'
    )
    ->ignoreSSL()
    ->setCookieJar($cookie_file_path)
    ->retrieveHeader()
    ->setManualOption(CURLOPT_REFERER, "http://www.google.com")
    ->splode(); // executes the request
// you can get to the returned value with $loginAttempt->responseRaw,
// or a host of other options, see __get()

$loggedInPage = httpRequest::get('https://kinray.com/weblink2/search/categories.do?scrollType=&code=&alwaysCustomerNumber=023987')
    ->ignoreSSL()
    ->setCookieJar($cookie_file_path)
    ->splode();

you can easily get to the response body by accessing the body property ( $loggedInPage->body , for example) from there. 您可以通过从那里访问body属性(例如$loggedInPage->body )轻松地到达响应正文。 Let me know if this works for you. 让我知道这是否适合您。

EDIT: Sample Response 编辑:样本响应

<script>document.oncontextmenu = function(){return false;};</script>
<!--categories.jsp-->
<html>
<head>
<title></title>
<LINK rel=stylesheet type=text/css href="/weblink2/styles.css">
<script>



















top.frames.search.setPaging('up'  , true);
top.frames.search.setPaging('down', true);


   var categories  = [ 

   ["27","BABY DIAPERS                                      "],

   ["28","BABY FORMULA                                      "],

   ["66","BAG STUFFER (PG)                                  "],

   ["90","BAG STUFFER FLYER DECEMBER                        "],

   ["89","BAG STUFFER FLYER NOVEMBER                        "],

   ["78","BAG STUFFER WINDOW SIGNS DECEMBER                 "],

   ["77","BAG STUFFER WINDOW SIGNS NOVEMBER                 "],

   ["35","BATTERIES                                         "],

   ["17","BONUS , VALUE PACKS                               "],

   ["22","CANDY AND GUM                                     "],

   ["49","CANDY AND GUM - SUGARFREE                         "],

   ["25","COSMETICS                                         "],

   ["52","COUGH &amp; COLD LIQUIDS                              "],

   ["21","DIABETIC NEEDS                                    "],

   ["42","DIAGNOSTICS                                       "],

   ["16","DISPLAYS, FLOORSTANDS                             "],

   ["33","ELECTRONICS                                       "],

   ["43","ETHNIC                                            "],

   ["26","FAMILY PLANNING                                   "],

   ["44","FILM, CAMERAS, &amp; VIDEO                            "],

   ["32","FOOD ITEMS                                        "],

   ["5","FRAGRANCES MEN                                    "],

   ["45","FRAGRANCES WOMEN                                  "],

   ["54","GENERIC ANTIBIOTICS                               "],

   ["13","GENERIC SPECIALS                                  "],

   ["53","GENERIC TOPICALS                                  "],

   ["8","GENERICS NEW                                      "],

   ["7","HBA\&#039;S NEW                                         "],

   ["41","HOME APPLIANCES                                   "],

   ["3","HOME HEALTH CARE                                  "],

   ["14","HOME HEALTH CARE (NEW)                            "],

   ["4","HOME HEALTH CARE MONTHLY SPECIALS                 "],

   ["93","HOME HEALTH CARE TOP 40                           "],

   ["36","HOMEOPATHIC                                       "],

   ["31","HOUSEHOLD GOODS                                   "],

   ["9","INJECTABLES                                       "],

   ["29","INSULIN                                           "],

   ["50","NEVER OUT OF STOCK ITEMS                          "],

   ["30","NUTRITIONALS                                      "],

   ["56","OTC PRODUCTS CONTAINING PSE                       "],

   ["46","PERSONAL CARE APPLIANCES                          "],

   ["51","PET SUPPLIES                                      "],

   ["59","PHARMACY FIRST                                    "],

   ["40","PHARMACY SUPPLIES (MISC)                          "],

   ["55","PRE-BOOK ITEMS                                    "],

   ["20","PREFERRED PLUS (KPP) NEW                          "],

   ["2","PREFERRED PLUS (P/L) KPP                          "],

   ["10","RX BAGS (PHARMACY SUPPLIES)                       "],

   ["12","RX SPECIALS                                       "],

   ["6","RX\&#039;S NEW                                          "],

   ["24","SCHOOL SUPPLIES                                   "],

   ["15","SUMMER                                            "],

   ["60","SUNGLASSES/EYEGLASSES                             "],

   ["47","SYRINGES, NEEDLES, ACCESSORIES                    "],

   ["34","TOYS &amp; GAMES                                      "],

   ["18","TRIAL &amp; TRAVEL SIZES                              "],

   ["39","VIALS &amp; BOTTLES (PHARMACY SUPPLIES)               "],

   ["23","VITAMINS                                          "],

   ["37","WETS &amp; DRYS                                       "],

   ["",""]

   ];

   var titleSearch='';

   function DisplayCategories (table) {
      document.write("<TABLE class=searchTable border =0  cellPadding=0 cellSpacing=0 >");
      var k=0;
      for(var x=0; x<=(table.length-1); x++) {
         if(x%20==0)
            document.write("<TD>");
         document.write("<a href =\"javascript:parent.parent.frames.search.loadCategory(categories["+x+"][0], categories["+x+"][1])\">",table[x][1],"</a><BR>");
      }
      document.write("</TABLE>");
   }

   DisplayCategories(categories);
   top.setKeyHandlers(document);
</script>
</head>
<body>
</BODY>
</HTML>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM