Rob2012 Posted June 18, 2012 Share Posted June 18, 2012 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(). Link to comment https://forums.phpfreaks.com/topic/264389-php-set-textbox-value-simulate-a-button-click-and-read-the-data-returned/ Share on other sites More sharing options...
scootstah Posted June 18, 2012 Share Posted June 18, 2012 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); Link to comment https://forums.phpfreaks.com/topic/264389-php-set-textbox-value-simulate-a-button-click-and-read-the-data-returned/#findComment-1354997 Share on other sites More sharing options...
Rob2012 Posted June 20, 2012 Author Share Posted June 20, 2012 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. Link to comment https://forums.phpfreaks.com/topic/264389-php-set-textbox-value-simulate-a-button-click-and-read-the-data-returned/#findComment-1355505 Share on other sites More sharing options...
scootstah Posted June 20, 2012 Share Posted June 20, 2012 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); Link to comment https://forums.phpfreaks.com/topic/264389-php-set-textbox-value-simulate-a-button-click-and-read-the-data-returned/#findComment-1355516 Share on other sites More sharing options...
Rob2012 Posted July 12, 2012 Author Share Posted July 12, 2012 Thanks so much scootstah. It is working. I can get the data I want. By the way, how do you find out what setReport() is ? What kind of tools are you using? Best regards. Link to comment https://forums.phpfreaks.com/topic/264389-php-set-textbox-value-simulate-a-button-click-and-read-the-data-returned/#findComment-1361085 Share on other sites More sharing options...
scootstah Posted July 12, 2012 Share Posted July 12, 2012 I used Firebug, but you can just view the page source. Link to comment https://forums.phpfreaks.com/topic/264389-php-set-textbox-value-simulate-a-button-click-and-read-the-data-returned/#findComment-1361106 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.