PHP Curl GET&POST
我正在使用以下代码在当前卷曲URL,这可以很好地与连接到URL结尾或POST数据的get一起使用。但不是和get和post。PHP Curl GET&POST
但是,当我使用高级休息客户端(添加谷歌浏览器)它工作得很好。尽管如此,我看不到它发送的模仿它的请求。
继承人我打电话给它。
$fields = array(
'searchPaginationResultsPerPage'=>500 );
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.microgenerationcertification.org/mcs-consumer/installer-search.php?searchPaginationPage=1');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_POST,count($fields));
curl_setopt($curl,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 80);
$str = curl_exec($curl);
curl_close($curl);
只是使用这个作为一个测试比其他任何东西,但似乎无法得到它的工作。我能拿到第500个结果所有的时间,但不能下500
这工作
$fields = array (
'searchPaginationResultsPerPage' => 500,
'searchPaginationPage' => 1
);
$headers = array (
"Connection: keep-alive",
"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding: gzip,deflate,sdch",
"Accept-Language: en-US,en;q=0.8",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3"
);
$fields_string = http_build_query ($fields);
$cookie = 'cf6c650fc5361e46b4e6b7d5918692cd=49d369a493e3088837720400c8dba3fa; __utma=148531883.862638000.1335434431.1335434431.1335434431.1; __utmc=148531883; __utmz=148531883.1335434431.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); mcs=698afe33a415257006ed24d33c7d467d; style=default';
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://www.microgenerationcertification.org/mcs-consumer/installer-search.php?searchPaginationPage=1&searchPaginationResultsPerPage=500');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 80);
curl_setopt ($ch, CURLOPT_COOKIE, $cookie);
curl_setopt ($ch, CURLOPT_HTTPHEADER, $headers);
$str = curl_exec ($ch);
curl_close ($ch);
echo $str;
您需要的cookie信息,并确保卷曲使用GET无法发布
见演示:http://codepad.viper-7.com/gTThxX(我希望cokkies不befor过期Ë您观看)
不知道为什么失败了,看起来不错。当你跳过卷曲,去为PHP流的方法会发生什么:
$postdata = http_build_query(
array(
'searchPaginationResultsPerPage' => 500
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://www.microgenerationcertification.org/mcs-consumer/installer-search.php?searchPaginationPage=1', false, $context);
$ POSTDATA = http_build_query( 阵列( 'searchPaginationResultsPerPage'=> '500' ) ); $ opts = array('http'=> array( 'method'=>'POST', 'header'=>'Content-type:application/x-www-form-urlencoded', 'content' => $ postdata ) ); $ context = stream_context_create($ opts); $ result = file_get_contents('http://www.microgenerationcertification.org/mcs-consumer/installer-search.php?searchPaginationPage=1',false,$ context); $ html = new simple_html_dom(); $ html-> load($ result); echo $ html; – David 2012-04-26 10:09:10
尝试此并获得相同的结果。 – David 2012-04-26 10:09:27
我看看你刮的页面,发现如下:
- 当您更改每页结果它张贴您的搜索再次
- 他们似乎是使用会话来存储您的搜索参数
使用CURL时,您不保留会话ID(并且这样做可能比您想要的复杂一些),因此这不会与网站上的行为相同。
但我注意到,但是如果您将searchPaginationResultsPerPage参数追加到URL,它的工作正常。就像这样:
这意味着你可以实际使用的file_get_contents,而不是担心卷曲的东西。
我试着用你建议的方法获得第二个500页,它不工作,只有第一个500. – David 2012-04-26 10:15:50
然后你需要保留会话ID。他们的代码中可能发生的情况是,如果您没有在会话中存储搜索关键字,则默认情况下会获得第一个页面。如果您不保留CURL请求之间的会话ID,则每次都像新会话一样。在这里阅读CURL和cookies:http://coderscult.com/php/php-curl/2008/05/20/php-curl-cookies-example/ – RolandLovelock 2012-04-26 10:27:19
我确实担心这可能是问题所在。奇怪的是,它在两个REST客户端都能正常工作。 – David 2012-04-26 10:38:00
在提示明显之前,您是否尝试过这种方法? – David 2012-04-26 10:11:58
我想可能不会,因为你会发现你的代码给出了我在这个主题的正文中提出的同样的问题。 – David 2012-04-26 10:13:26
'我可以一直得到前500的结果,但不是下一个500'测试它....有什么你认为错过了? – Baba 2012-04-26 10:22:07