php curl没有得到与fiddler相同的响应
这是我的PHP代码:php curl没有得到与fiddler相同的响应,php,curl,fiddler,Php,Curl,Fiddler,这是我的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' => $destinatio
$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
";
$source=$\u POST['source'];
$destination=$_POST['destination'];
$class=$_POST['class'];
$day=$_POST['day'];
$month=$_POST['month'];
$data=数组(
‘lccp_src_stncode_dis’=>$source,
“lccp_dstn_stncode”=>$destination,
“lccp_classopt”=>$class,
“lccp_日”=>美元日,
“lccp_月”=>$month
);
#创建连接
$url='根据原始请求的数据';
$ch=curl\u init($url);
echo$ch.“
”;
#表单数据字符串
$postString=http\u build\u query($data);
echo$postString;
$header=数组(
'主机'=>'根据原始请求的数据'
“连接”=>“保持活动状态”,
“内容长度”=>“180”,
“缓存控制”=>“最大年龄=0”,
'接受'=>'文本/html',
“来源”=>“根据原始请求的数据”,
'用户代理'=>'',
“内容类型”=>“应用程序/x-www-form-urlencoded”,
'参考':'根据原始请求的数据'
'接受编码'=>'',
“接受语言”=>“en US,en;q=0.8”
);
#设置选项
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postString);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
//#得到回应
curl_setopt($ch,CURLOPT_POSTFIELDS,
$response=curl\u exec($ch);
卷曲关闭($ch);
#打印响应
回声“
$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
POSThttp://www.indianrail.gov.in/cgi_bin/inet_srcdest_cgi_date.cgi HTTP/1.1
主持人:www.indianrail.gov.in
连接:保持活力
内容长度:91
缓存控制:最大年龄=0
接受:text/html
原点:*标题不能作为关联数组输入,这是错误的:
$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实际上不进行任何格式化。实际原始请求:post http/1.1主机:www.indianrail.gov.in连接:保持活动内容长度:91缓存控制:max age=0接受:text/html来源:用户代理:内容类型:application/x-www-form-urlencoded Referer:Accept编码:接受语言:en-US,en;q=0.8 lccp_src_stncode_dis=ndls&lccp_dstn_stncode=HYB&lccp_classopt=SL&lccp_day=26&lccp_month=6您确定用curl重新创建了整个http请求吗?我在那里没有看到任何Cookie头例如,您可以启用CURLOPT_VERBOSE for php curl来查看与curl-v类似的输出,以使调试更容易。不需要,我作为注释发布的原始请求会在fiddler中提供所需的输出。将发布详细输出您可以使用fiddler的比较会话
功能将curl请求与fiddler请求进行比较看看他们到底有什么不同。
$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'
);