cURL - 在浏览器中工作,而不是在cURL中 [英] cURL - works in browser, not in cURL

查看:205
本文介绍了cURL - 在浏览器中工作,而不是在cURL中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我开始:

我已经在这个任务上努力了几乎三天了,我想我缺少一些基本的cURL技能。 >

在IE的F12中,我看到第一页上的2个POST:(我注意到第一个是得到一个302,这应该是一个重定向,并且与cURL我只得到200)



填写验证码:



在第二页(验证码后):



traffic:



这是我的代码(我不能继续使用它,因为它不能在早期阶段工作):



我建立一个特殊的表单,提交到我自己的页面 GET (与cURL) Turn访问网站:

  $ id = $ _ GET ['id']; // getting the biznumber 
$ humanCode = $ _ GET ['nobot'];

$ curl = curl_init();
curl_setopt($ curl,CURLOPT_URL,https://www.*******.******.***);


//设置一些https以便能够从本地计算机访问该网站。
curl_setopt($ curl,CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($ curl,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ curl,CURLOPT_CAINFO,c:/xampp/htdocs/CAcerts/curl-ca-bundle.crt);

//我知道ASPX变量的值,如__EVENTTARGET,__EVENTARGUMENT,__VIEWSTATE现在是任意的。我需要照顾,但我还不知道如何。

$ postarr =阵列(
__EVENTTARGET=>中,
__VIEWSTATE ==>中%2FwEPDwULLTEzMzI2OTg4NDYPZBYCZg9kFgQCBA8PZBYCHgdvbmNsaWNrBQxnb1RvTWl2emFrKClkAgYPD2QWAh8ABQxnb1RvTWl2emFrKClkZM6iZZ0Qaf2CpfXoJJdZ0IqaWsDO,
__EVENTARGUMENT = =>,
__EVENTVALIDATION=>%2FwEWBQKgysLGCwL2r7SGDQLh4ri%2BAwLWws7NDwLWwpLPD%2F1HuCAFYzs2seaziWbYEXjDfigP,
hidUrlFileIshurim=>https%3A%2F,
cod >3322
);

$ fields_string ='';
foreach($ postarr为$ key => $ value){$ domains_string。 '='。$ value。'&';}
rtrim($ fields_string,'&');

curl_setopt($ curl,CURLOPT_POST,1);
curl_setopt($ curl,CURLOPT_POSTFIELDS,$ fields_string);

curl_setopt($ curl,CURLOPT_TIMEOUT,10);

curl_setopt($ curl,CURLOPT_USERAGENT,User-Agent Mozilla / 5.0(兼容; MSIE 9.0; Windows NT 6.1; WOW64; Trident / 5.0; MAAU));

//我做了一个cookie文件,似乎工作
$ cookiefile =d:/cookie.txt;
curl_setopt($ curl,CURLOPT_COOKIEJAR,$ cookelfile);
curl_setopt($ curl,CURLOPT_COOKIEFILE,$ cookelfile);

curl_setopt($ curl,CURLOPT_FRESH_CONNECT,1);
curl_setopt($ curl,CURLOPT_FOLLOWLOCATION,1);

curl_setopt($ curl,CURLOPT_HEADER,1); //不返回HTTP头
curl_setopt($ curl,CURLOPT_RETURNTRANSFER,1);

$ temp = curl_exec($ curl);
$ info = curl_getinfo($ curl);

$ html = mb_convert_encoding($ temp,'HTML-ENTITIES','utf-8');
echoERRCODE:.curl_error($ curl);
echo'< br />< br />';
echoINFO:;
print_r($ info);
echo'< br />< br />';
$ httpcode = curl_getinfo($ curl,CURLINFO_HTTP_CODE);
echoCODE:。$ httpcode;
echo'< br />< br />';
echoCODE:。$ httpcode;
echo'< br />< br />';
echoVARS:。$ vars;
echo'< br />< br />';
// echo $ html;

curl_setopt($ curl,CURLOPT_URL,https://www.*******.******.***);
curl_setopt($ curl,CURLOPT_FRESH_CONNECT,0);

echo< br />< br />< b>第二< / b>< br />< br /
$ temp = curl_exec($ curl);
$ info = curl_getinfo($ curl);

$ html = mb_convert_encoding($ temp,'HTML-ENTITIES','utf-8');
echoERRCODE:.curl_error($ curl);
echo'< br />< br />';
echoINFO:;
print_r($ info);
echo'< br />< br />';
echo $ html;

无法让它开始工作。它开始返回一个200 OK,而不是302,有时我也得到一个500.



我知道ASPX vars可能实际上是至关重要的,但如果我的浏览器可以使这些vars并将它们发送到服务器,不能cURL做同样的事情?



感谢任何帮助!!

解决方案

问题解决。

这是使用正确标头的问题。
根据浏览器的报告,我完成了所有步骤,结果显示出来。



我使用以下代码完成每个步骤:

  curl_init 
curl_setopt()
..
curl_setopt()
curl_exec b curl_close()

这样,我必须手动设置每个请求,它使代码更长,但更容易理解。



我对网站使用一些javascript特殊代码,使网站工作,所以我困扰了所有额外的JavaScript代码,这是不必要的。



这是关于更有条理和遵循正确的标题设置。



ASPX 网站,我必须阅读并记住 VIEWSTATE VALIDATION 每个迭代中的最后一页。这是第一个,也是为什么我一直得到所有的内部错误服务器消息的原因。



我使用Firebug和LiveHttpHeaders来完成每一步。


I have been struggling for almost three days now on this task, and I guess I am missing on some basic cURL skills.

I start with:

In the F12 of IE I see 2 POSTs on the first page: (I notice the first one is getting a 302 which is supposed to be a redirect, and with cURL I only get 200)

Filling up the captcha:

on the second page (after captcha):

traffic:

This is my code (and I cannot move on with it because it doesn't work for the early stages):

I Built a special form that submits to my own page with GET (with the cURL) which in turn is accessing the website:

$id=$_GET['id']; // getting the biznumber
$humanCode=$_GET['nobot'];

$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "https://www.*******.******.***");


// setting some https to be able to access the website from my local computer.
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, "c:/xampp/htdocs/CAcerts/curl-ca-bundle.crt");

// I know the values for the ASPX vars like __EVENTTARGET, __EVENTARGUMENT, __VIEWSTATE are arbitrary now. I need to take care of that but I don't yet know how.

$postarr= array (
                "__EVENTTARGET"=>"",
                "__VIEWSTATE=" =>"%2FwEPDwULLTEzMzI2OTg4NDYPZBYCZg9kFgQCBA8PZBYCHgdvbmNsaWNrBQxnb1RvTWl2emFrKClkAgYPD2QWAh8ABQxnb1RvTWl2emFrKClkZM6iZZ0Qaf2CpfXoJJdZ0IqaWsDO",
                "__EVENTARGUMENT=" =>"",
                "__EVENTVALIDATION" =>"%2FwEWBQKgysLGCwL2r7SGDQLh4ri%2BAwLWws7NDwLWwpLPD%2F1HuCAFYzs2seaziWbYEXjDfigP",
                "hidUrlFileIshurim"=>"https%3A%2F,
"cod"=>"3322"
                    );

$fields_string='';
foreach($postarr as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

curl_setopt($curl, CURLOPT_POST      ,1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);

curl_setopt($curl, CURLOPT_TIMEOUT, 10);

curl_setopt ($curl, CURLOPT_USERAGENT, "User-Agent  Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; MAAU)");

// I made a cookie file and it seems to work
$cookiefile = "d:/cookie.txt";
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile); 
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);

curl_setopt($curl, CURLOPT_FRESH_CONNECT , 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION  ,1);

curl_setopt($curl, CURLOPT_HEADER      ,1);  // DO NOT RETURN HTTP HEADERS
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$temp=curl_exec($curl);
$info = curl_getinfo($curl);

$html = mb_convert_encoding($temp, 'HTML-ENTITIES', 'utf-8'); 
echo "ERRCODE: ".curl_error($curl);
echo '<br /><br />';
echo "INFO : ";
print_r($info);
echo '<br /><br />';
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo "CODE: ".$httpcode;
echo '<br /><br />';
echo "CODE: ".$httpcode;
echo '<br /><br />';
echo "VARS: ".$vars;
echo '<br /><br />';
//echo $html;

curl_setopt ($curl, CURLOPT_URL, "https://www.*******.******.***");
curl_setopt($curl, CURLOPT_FRESH_CONNECT , 0);

echo "<br /><br /><b>2nd</b><br /><br />";
$temp=curl_exec($curl);
$info = curl_getinfo($curl);

$html = mb_convert_encoding($temp, 'HTML-ENTITIES', 'utf-8'); 
echo "ERRCODE: ".curl_error($curl);
echo '<br /><br />';
echo "INFO : ";
print_r($info);
echo '<br /><br />';
echo $html;

Can't get that to even start to work. It starts with returning me a 200 OK, instead of 302, and sometimes I also get a 500.

I know the ASPX vars might actually be crucial, but if my browser can make these vars and send them to the server, can't cURL do the same ?

Thanks for any help !!

解决方案

Problem solved.
It was a matter of using the correct headers. Following the reports from the browser, I went through all steps and the result showed up.

I went through each step by using:

curl_init
curl_setopt()
..
curl_setopt()
curl_exec()
curl_close()

This way I had to manually set each request and go through the settings. It made the code longer, but much easier to understand.

I had thoughts about the site using some javascript special code to make the site work, so I was troubled a lot by all the extra, javascript code, which turned out unnecessary.

It was all about being alot more organized and following the correct header settings.

Moreover, since this was an ASPX site, I had to read and memorize the VIEWSTATE and VALIDATION of the last page in each iteration. That is the first and very reason for the 500 internal error server message I used to get all the time.

I used Firebug and LiveHttpHeaders to concolude each step.

这篇关于cURL - 在浏览器中工作,而不是在cURL中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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