【发布时间】:2014-01-01 23:18:58
【问题描述】:
我正在尝试向网页发送 POST 查询,但不幸的是它不起作用。以下是我的代码:
<?php
$url_ref = "http://www.wunschgrundstueck.de/immobilien/wohnung-suchen.html?sp_was=wohnungen_m&lses=1";
$url = "http://www.wunschgrundstueck.de/immobilien/suchergebnis/wohnungen.html";
$fields = array(
'id_land' => 'DEU',
'id_bundesland' => '7',
'id_landkreis'=> '186',
'id_gemeinde'=>'5234',
'su_anb' => '0',
'sp_was' => 'wohnungen_m',
'sp_wo' => 'Frankfurt am Main',
'sp_ort_land' => 'DEU',
'sp_ort_bula' => '7',
'sp_ort_lakr' => '186',
'sp_ort_geme' => '5234',
'sp_ort_teil' => '0',
'sp_ort_umkreis' => '0',
'miete' => 'on',
'su_wohn_preis_miete_art' => '1',
);
$fields_string;
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
echo $fields_string;
//open connection
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_REFERER, $url_ref);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
curl_setopt($curl,CURLOPT_POST,count($fields));
curl_setopt($curl,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($curl);
?>
不幸的是,我似乎做错了什么,因为返回的结果不是我所期望的。令我困惑的是,如果我使用 LiveHTTP 标头插件为 firefox 发送相同的请求,那么我会得到正确的结果。然后我尝试使用 WFetch 发送请求,然后收到 301 Moved Permanent 错误。这是我使用来自 firefox 的 HTTPLive 测试的 HTTP Post 标头/字段,它可以正常工作,而使用 WFetch,我得到 301 错误。
Host: www.wunschgrundstueck.de\r\n
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0\r\n
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n
Accept-Encoding: gzip, deflate\r\n
Referer: http://www.wunschgrundstueck.de/immobilien/landkreis_frankfurt_am_main/wohnungen.html?s=1\r\n
Connection: keep-alive\r\n
Content-Type: application/x-www-form-urlencoded\r\n
\r\n
id_land=DEU&id_bundesland=7&id_landkreis=186&id_gemeinde=&id_ortsteil=&gemeid_bereich=&su_suchart=erweitert&su_anb=0&sp_was=wohnungen&sp_wo=&sp_ort_land=DEU&sp_ort_bula=7&sp_ort_lakr=186&sp_ort_geme=0&sp_ort_teil=0&sp_wo_ausl=&miete=on&su_wohnfl_wohn_von=&su_wohnfl_wohn_bis=&su_wohn_preis_von_kauf=&su_wohn_preis_bis_kauf=&su_wohn_preis_miete_art=1&su_wohn_preis_von_miete=&su_wohn_preis_bis_miete=&su_zimmer_wohn_anz_von=&su_zimmer_wohn_anz_bis=&su_filter_wohntyp=&su_wohn_etage_von=&su_wohn_etage_bis=&su_wohn_filter_anzbad=&su_wohn_filter_anzschlaf=&su_wohn_filter_baf_det=&su_wohn_filter_moebliert=&submit=Ergebnisse+zeigen+%BB\r\n
\r\n
【问题讨论】: