0

PHPサイトからデータを収集しようとしています。ただし、この特定の php ページは、独自の関数 (以下のコードでは setReport()) を使用して $POST データを (複製できない特別なタイムスタンプ データに) 変換し、そのサーバーに送信しました。したがって、このデータを取得するには、テキストボックスに在庫番号を入力してボタンを押すしかないと思います。

以下は、データを取得したい php サイトのソースからのスニペットです。

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>                
> 
>             &nbsp;&nbsp;&nbsp;&nbsp;<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 );        
> 
>   }

データを取得するために、次の手順で PHP コードを作成することを考えています。しかし、ステップ 2 の実行方法がわかりません。これを手伝ってくれる人はいますか? またはそれを行う別の方法はありますか?本当にありがとう!!!

  1. curl_init を使用してサイトを読み込みます。
  2. テキストボックス「input_stock_code」に値を設定し、ボタンのクリックをシミュレートします。
  3. curl_exec() からの結果を解析します。
4

1 に答える 1

0

サイトを読むことができないので、これを実際にテストすることはできませんが、ページを取得してフォームに入力し、結果を取得するのではなく、次の方法で結果ページを直接取得できます。

http://www.gretai.org.tw/ch/stock/statistics/monthly/result_st42.php?ajax=true&input_stock_code=<code>

setReport() が行うタイムスタンプ ビジネスは、ブラウザがキャッシュされた結果をロードするのを止めるだけなので、問題なく省略できます。

編集:修正、ajaxおよびinput_stock_code変数をPOSTする必要があります。CURL を使用して PHP でこれを行うことができます。

// Build URL complete with timestamp
$url = 'http://www.gretai.org.tw/ch/stock/statistics/monthly/result_st42.php?timestamp='.time().'156';

// POST body variables
$fields = array(
            'ajax'=>urlencode('true'),
            'input_stock_code'=>urlencode('1158')
        );

//url-ify the data for the POST
$fields_string = http_build_query($fields);

//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_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

// Pretend we are a browser that is looking at the site
curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120403211507 Firefox/12.0');
curl_setopt($ch,CURLOPT_REFERER, 'http://www.gretai.org.tw/ch/stock/statistics/monthly/st42.php');

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

echo $result;

このウェブサイトのスニペット投稿に基づいています。

于 2012-06-19T06:22:12.047 に答える