这是我的PHP代码:
$source = $_POST['source'];
$destination = $_POST['destination'];
$class = $_POST['class'];
$day = $_POST['day'];
$month = $_POST['month'];
$data = array(
'lccp_src_stncode_dis' => $source,
'lccp_dstn_stncode' => $destination,
'lccp_classopt' => $class,
'lccp_day' => $day,
'lccp_month' => $month
);
# Create a connection
$url = 'data as per raw req ';
$ch = curl_init($url);
echo $ch." <br>";
# Form data string
$postString = http_build_query($data);
echo $postString;
$header = array (
'Host' => 'data as per raw req '
'Connection'=> 'keep-alive',
'Content-Length'=> '180',
'Cache-Control'=> 'max-age=0',
'Accept'=> 'text/html',
'Origin'=> 'data as per raw req ',
'User-Agent' => '',
'Content-Type' => 'application/x-www-form-urlencoded',
'Referer': 'data as per raw req '
'Accept-Encoding'=> '',
'Accept-Language' => 'en-US,en;q=0.8'
);
# Setting options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//# Get the response
curl_setopt($ch, CURLOPT_POSTFIELDS,
$response = curl_exec($ch);
curl_close($ch);
#print response
echo "
$response
";
从以下地址接收表格数据:
<html>
<form method="post" action="poster.php">
<table>
<tr><td>source:</td><td><input type="text" name="source"></td></tr>
<tr><td>destination:</td><td><input type="text" name="destination"></td></tr>
<tr><td>day:</td><td><input type="text" name="day"></td></tr>
<tr><td>month:</td><td><input type="text" name="month"></td></tr>
<tr><td>class:</td><td><input type="text" name="class"></td></tr>
<tr><td><input type="submit" name="submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Reset"></td></tr>
</table>
</form>
</html>
这是原始请求
POST http://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_date.cgi HTTP/1.1
Host: www.indianrail.gov.in
Connection: keep-alive
Content-Length: 91
Cache-Control: max-age=0
Accept: text/html
Origin: **<---modified since I can't post more than 2 links
User-Agent:
Content-Type: application/x-www-form-urlencoded
Referer: ** <---modified since I can't post more than 2 links
Accept-Encoding:
Accept-Language: en-US,en;q=0.8
lccp_src_stncode_dis=ndls&lccp_dstn_stncode=HYB&lccp_classopt=SL&lccp_day=26&lccp_month=6
Fiddler正在给出来自cgi的相应响应,而php脚本只是显示了html容器,我已经被困了一个多小时!请帮忙!
编辑:详细输出:
* Adding handle: conn: 0x2cab910
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 3 (0x2cab910) send_pipe: 1, recv_pipe: 0
* About to connect() to www.indianrail.gov.in port 80 (#3)
* Trying 203.176.113.78...
* Connected to www.indianrail.gov.in (203.176.113.78) port 80 (#3)
> POST /cgi_bin/inet_srcdest_cgi_date.cgi HTTP/1.1
Host: www.indianrail.gov.in
Accept: */*
http://www.indianrail.gov.in
http://www.indianrail.gov.in/know_Station_Code.html
Content-Length: 89
Content-Type: application/x-www-form-urlencoded
* upload completely sent off: 89 out of 89 bytes
< HTTP/1.1 200 OK
< Date: Fri, 27 Jun 2014 05:38:17 GMT
* Server Apache/2.2.15 (Red Hat) is not blacklisted
< Server: Apache/2.2.15 (Red Hat)
< Connection: close
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=UTF-8
<
* Closing connection 3
^关闭话题:这次为什么只让我发布链接,应该被认为是链接?
另外,我会比较fiddler中的会话,但是fiddler会在本地主机上向我自己的应用程序显示请求,而不是从我的应用程序到印度铁路网站。
截图:
NEVERMIND,没有足够的回复
编辑:
如果有人能告诉我如何使用http_request2发出这个请求,我会很高兴。我已经发布了确切的原始请求,在评论中给我提供了fiddler所需的输出,所以不,我不是缺少任何身份验证和Cookie标头等
答案 0 :(得分:0)
标题不能作为关联数组输入,这是错误的:
$header = array (
'Host' => 'data as per raw req '
'Connection'=> 'keep-alive',
'Content-Length'=> '180',
'Cache-Control'=> 'max-age=0',
'Accept'=> 'text/html',
'Origin'=> 'data as per raw req ',
'User-Agent' => '',
'Content-Type' => 'application/x-www-form-urlencoded',
'Referer': 'data as per raw req '
'Accept-Encoding'=> '',
'Accept-Language' => 'en-US,en;q=0.8'
);
正确的方法:
$header = array (
'Host:*data as per raw req* '
'Connection:keep-alive',
'Content-Length:180',
'Cache-Control:max-age=0',
'Accept:text/html',
'Origin:*data as per raw req* ',
'User-Agent:',
'Content-Type:application/x-www-form-urlencoded',
'Referer:*data as per raw req* '
'Accept-Encoding:',
'Accept-Language:en-US,en;q=0.8'
);
即使在我使用的关联数组中输入的数据也被格式化为url编码的post字符串,显然所有内容都必须输入,因为它在原始http请求中。 cURL并没有真正做任何格式化。