php curl能够登录到网站但登录后没有显示任何数据 [英] php curl Able to login into site but shows no data after login

查看:195
本文介绍了php curl能够登录到网站但登录后没有显示任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



我可以使用php和curl登录网站。但是当我尝试读取数据登录后显示字符串(0)如果CURLOPT_RETURNTRANSFER启用,当禁用它显示bool(true)。我不明白我在哪里出错...



任何帮助真的很感激



/ p>

  $ 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);
//现在不打印任何东西。
// 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);

任何线索我错了什么?

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

这可能是因为您的Cookie未正确储存/ 。



httpRequest Class 示例(短form static constructors):



  $ 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 //执行请求
//您可以通过$ loginAttempt-> responseRaw,
//或其他选项的主机获得返回的值,请参见__get()

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

您可以通过访问body属性轻松访问响应主体( $ loggedInPage-> body )。让我知道这是否适合您。



编辑:样本回应

 < 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, 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,家庭电器],

[3,家庭健康],

[14,家庭健康护理(新)],

[4,家庭健康护理每月特价],

[93,HOME HEALTH CARE TOP 40],

[36,HOMEOPATHIC],

[31 ,HOUSEHOLD GOODS],

[9,INJECTABLES],

[29,INSULIN],

[50,从不出卖商品],

[30,营养],

[56,OTC PRODUCTS CONTAINING PSE],

[46,个人护理用具],

[51,PET耗材],

[59,PHARMACY FIRST],

[40,PHARMACY SUPPLIES(MISC)],

[55,PRE- BOOK ITEMS],

[20,PREFERRED PLUS(KPP)NEW],

[2 ],

[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& DRYS],

[,]

];

var titleSearch ='';
$ b b 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> b $ b document.write(< a href = \javascript:parent.parent.frames.search.loadCategory(categories [+ x +] [0],categories [+ x +] [1]) \>,table [x] [1],< / a>< BR>);
}
document.write(<
}

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


HI,

I am able to login into the site using php and 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). 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?

解决方案

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.

httpRequest Class Example (short form static constructors):

$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. 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>

这篇关于php curl能够登录到网站但登录后没有显示任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆