2013-05-02 84 views
1

我必須從頁面獲取考試結果http://cbseresults.nic.in/class1211/cbse122012.htm Sampleroll number is 4623447. 他們使用http post發佈表單數據。我寫了下面的代碼發佈data.But它不是提供所需的results.I我張貼必要的餅乾和後variables.But還是我沒有得到output.What改變我應該爲send_post功能,因此,這將是working.Here是我的代碼php抓取頁面使用http post

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; 
    } 

回答

2

您可以使用file_get_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); 
+1

什麼stream_context_create – 2013-05-02 08:05:27

+1

你可以用'的file_get_contents發送POST請求()',再加上它似乎你在這裏失蹤的餅乾罐的一部分。 – HamZa 2013-05-02 08:08:42

+0

餅乾罐子的一部分。你能解釋一下嗎? – 2013-05-02 08:12:29