HI,
我可以使用php和curl登录该网站。但是当我在登录后尝试读取数据时,如果启用CURLOPT_RETURNTRANSFER则显示字符串(0)“”,禁用时显示bool(true)。我不明白我到底哪里错了......
非常感谢任何帮助
继承人代码
$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);
任何线索,我哪里出错?
答案 0 :(得分:2)
您的curl_close
来电被破坏了。传入curl资源。 E.g:
curl_close($ch);
这就是为什么你应该启用并注意警告。
php > curl_close();
PHP Warning: curl_close() expects exactly 1 parameter, 0 given in php shell code on line 1
这反过来意味着你的cookie罐无法正常工作。
答案 1 :(得分:1)
如果您遇到卷曲问题,请使用fiddler来帮助您进行调试。
使用您的浏览器执行请求,并检查流量,然后进行卷曲和比较。这将告诉你有什么不同。
答案 2 :(得分:1)
在验证您的凭据时,可能会发生重定向。您仍然可以使用CURLOPT_FOLLOWLOCATION
而无需将其实际打印到屏幕上 - 是否有理由将其注释掉?
答案 3 :(得分:1)
这些变量应该不同吗?
// v
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie_fie_path);
curl_setopt($ch1, CURLOPT_COOKIEJAR, $cookie_file_path);
// ^
可能是您的Cookie未正确存储/检索。
$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();
您可以通过从那里访问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 & COLD LIQUIDS "],
["21","DIABETIC NEEDS "],
["42","DIAGNOSTICS "],
["16","DISPLAYS, FLOORSTANDS "],
["33","ELECTRONICS "],
["43","ETHNIC "],
["26","FAMILY PLANNING "],
["44","FILM, CAMERAS, & VIDEO "],
["32","FOOD ITEMS "],
["5","FRAGRANCES MEN "],
["45","FRAGRANCES WOMEN "],
["54","GENERIC ANTIBIOTICS "],
["13","GENERIC SPECIALS "],
["53","GENERIC TOPICALS "],
["8","GENERICS NEW "],
["7","HBA\'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\'S NEW "],
["24","SCHOOL SUPPLIES "],
["15","SUMMER "],
["60","SUNGLASSES/EYEGLASSES "],
["47","SYRINGES, NEEDLES, ACCESSORIES "],
["34","TOYS & GAMES "],
["18","TRIAL & TRAVEL SIZES "],
["39","VIALS & BOTTLES (PHARMACY SUPPLIES) "],
["23","VITAMINS "],
["37","WETS & 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>