0

我正在尝试从下面的 url 获取数据但我没有成功。我已经阅读了很多关于 Viewstate 的内容,但是我没有成功地让它工作。每当我执行这个时,我都没有得到任何数据,但我没有收到任何错误。

http://agmarkweb.dacnet.nic.in/SA_Pri_MonthRep.aspx';

    $fields = array(
    'Year_list'=>urlencode("2013"),                     
    'Month_list'=>urlencode("2"),                       
    'Commodity_list'=>urlencode("40"),                  
    '__EVENTTARGET'=>(""),                          
    '__EVENTARGUMENT'=>(""),                        
    '__LASTFOCUS'=>(""),                            
    '__VIEWSTATE'=>(""));

    //url-ify the data for the POST

    foreach($fields as $key=>$value) { 
        $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string,'&');
        //open connection

        $ch = curl_init();
            //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,count($fields));
        curl_setopt($ch, CURLOPT_VERBOSE, false);
        curl_setopt($ch, CURLOPT_HEADER, CURLOPT_ENCODING);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U;Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        //execute post
        $result = curl_exec($ch);
        echo $result;
        //close connection
        curl_close($ch);

我阅读了很多关于视图状态的信息,但在这种情况下应该怎么做?我是否应该将 Viewstate 复制到我的代码中?

4

1 回答 1

1

尝试这个:

$fields = array(
    'Year_list'=>2013,                     
    'Month_list'=> 2,                       
    'Commodity_list'=>40,                  
    '__EVENTTARGET'=>'',                          
    '__EVENTARGUMENT'=>'',                        
    '__LASTFOCUS'=>'',                            
    '__VIEWSTATE'=>''
);

//open connection

$ch = curl_init();
$url = 'http://agmarkweb.dacnet.nic.in/SA_Pri_MonthRep.aspx';
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_HEADER, CURLOPT_ENCODING);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U;Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
于 2013-04-08T02:33:49.647 回答