使用http post的php获取页面
我得从页面上取考试结果 取样器编号为4623447。 他们正在使用http post发布表单数据。我编写了以下代码来发布数据。但它没有提供所需的结果。我正在发布所需的cookie和post变量。但我仍然没有得到输出。我应该对send_post函数做什么更改,以便它能够正常工作。这是我的代码使用http post的php获取页面,php,post,file-get-contents,Php,Post,File Get Contents,我得从页面上取考试结果 取样器编号为4623447。 他们正在使用http post发布表单数据。我编写了以下代码来发布数据。但它没有提供所需的结果。我正在发布所需的cookie和post变量。但我仍然没有得到输出。我应该对send_post函数做什么更改,以便它能够正常工作。这是我的代码 echo cbse12_data_extractor(4623447); function cbse12_data_extractor($regNo) { $source_url = 'http://
echo cbse12_data_extractor(4623447);
function cbse12_data_extractor($regNo) {
$source_url = 'http://cbseresults.nic.in/class1211/cbse122012.asp';
$post_vars = array('regno'=>$regNo);
$cookies = array('_tb_pingSent'=>1);
// $extraHeaders = array('Host'=>'http://cbseresults.nic.in');
return send_post($source_url,$post_vars,$cookies);
}
function send_post( $url, $data ,$cookies='',$extraHeaders = '') //sends data array(param=>val,...) to the page $url in post method and returns the reply string
{
$post = http_build_query( $data );
$header = "Accept-language: en\r\n".
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen( $post ) .
"\r\nUser-agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n";
if($extraHeaders) {
foreach($extraHeaders as $headerN => $val) {
$header = $header.$headerN.': '.$val."\r\n";
}
}
if($cookies) {
$cookieArr = array();
foreach($cookies as $cookie => $value) {
array_push($cookieArr,$cookie.'='.$value);
}
$cookieStr = "Cookie: ".implode('; ',$cookieArr)."\r\n";
$header = $header.$cookieStr;
}
$context = stream_context_create( array(
"http" => array(
"method" => "POST",
"header" => $header,
"content" => $post
)
) );
//echo $header;
$page = file_get_contents( $url, false, $context );
return $page;
}
您可以使用
file\u get\u contents
发送POST
数据。使用CURL
执行此任务
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,your_parameters);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
那么stream\u context\u created呢?您可以使用
文件\u get\u contents()
发送POST请求,而且这里似乎缺少cookie jar部分。cookie jar部分。你能解释一下吗?