Jump to content

Php: set textbox value, simulate a button click and read the data returned


Rob2012

Recommended Posts

Hi Sir,

 

I am trying to collect some data from a php site. However this particular php page has transformed $POST data(to a special timestamp data that I can't replicate) with its own function(setReport() in below code) and sent to its server.

So in order to get this data, enter a stock number in the textbox and press the button is the only way to do I guess.

 

 

Below is the snippets from the source of php site that I want to grab data from.

http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php

--------------------------------------------------------------------------------

<form name="search" method="post" action="st42.php">
<table width="736" border="0" cellpadding="0" cellspacing="0" summary="查詢">
  .......	 
         <td class="search-in02">股票代碼:

		<input id="input_stock_code" name="input_stock_code" class="input01" size="6" maxlength="6">

            <A HREF="#" onclick="ChoiceStkCode(document.getElementById('input_stock_code'));" onkeypress="ChoiceStkCode(document.getElementById('input_stock_code'));" class="page_table-text_over">代碼查詢</A>                

                <input type="button" class="input01" value="查詢" onclick="query()" onkeypress="query()"/>		 
  ........

</table>

</form>    			
function query(){				

	var code = document.getElementsByName("input_stock_code")[0].value;

	var param = 'ajax=true&input_stock_code='+code;

	setReport('result_st42.php',param );		

}

---------------------------------------------------------------------------------

 

I am thinking of writing a PHP code with following steps to get the data. But I don't know how to do Step 2. Is there everyone who can help on this? Or there is another way to do it? Thanks so much!!!

 

1. Use curl_init to read in the site.

2. Set textbox, "input_stock_code" with a value and simulate a button click.

3. Parse the results from curl_exec().

This is untested but try something like this:

$ch = curl_init();

$post = array('input_stock_code' => 'your value here');

curl_setopt($ch, CURLOPT_URL, 'http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);

Hi scootstah,

 

Thanks for reply. Actually I have checked with Firebug/Live HTTP. The php page sent to server is result_st42.php and the $POST data has been modified by setReport() to a series of number. setReport() is an invisible function. So I guess I still need to simulate step 2 somehow.

This is the setReport function:

function setReport(url) {     
    var element = document.getElementById("rpt_result"); //????????
    if (!element) {
        alert(id + "not found.");
        return;
    }
    
    var req = AJAXRequest();      
    if (req != null) {                    
        req.onreadystatechange = processRequest;//??request?onReadyStateChange??
        //url = url+"?timestamp="+(new Date().getTime());//?????????url???,??cache                                
        if (url.indexOf('http')>=0){
            url = "/php/result.php?url="+url;
        }else{
            var urlPath = document.URL.substring(0,document.URL.lastIndexOf('/')+1);        
            url = "/php/result.php?url="+urlPath+url;
        }
        req.open('GET', url, false);
        req.send(null);
        
    } else {
        element.innerHTML = "";
        return;
    }   
    
    //??request?onReadyStateChange??    
    function processRequest () {
      
      if (req.readyState == 4) {// readyState = 4 ????server?????????
        if (req.status == 200 || req.status == 0) {// status = 200 ??server????(HTTP code) 
            
            element.innerHTML = req.responseText;
          
        }else{
            
            element.innerHTML = "????";            
        }
      }
    }    
}

 

I just examined the post data being sent and it looks like all you need to do is spoof the AJAX request. Try something like:

$ch = curl_init();

$post = array('ajax' => 'true', 'input_stock_code' => 'your value here');

curl_setopt($ch, CURLOPT_URL, 'http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php');
curl_setopt($ch, CURLOPT_POST, count($post));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);

curl_close($ch);

 

 

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.