[英]how to submit and retrieve data with Curl?
我第一次嘗試在PHP中使用Curl,原因是我要從以下頁面抓取結果: http : //www.lldj.com/pastresult.php 。 該網站自2002年以來每周發布樂透結果,並具有簡單的提交表格(Date)。
提交按鈕:名稱=按鈕/值=提交選擇下拉列表:名稱=繪制和選項#(1-1097)//表示繪制編號
我可以手動檢查它,但是我想為什么不使用簡單的腳本並使它變得更簡單,因為我也對測試如何使用PHP / CURL提交數據和檢索結果感興趣。
我已經使用DOM PHP進行抓取,並且使用語法感到很舒服。 我想知道我應該同時使用Curl和DOM還是可以通過CURL實現。
到目前為止我有什么;
include'dom.php';
$post_data['draw'] = '1097';
$post_data['button'] = 'Submit';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://www.lldj.com/pastresult.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
提交數據后/刮擦
$t = $curl_connection->find('table',0); // ?? usualy referes to file_get_content Var
$data = $t->find('tr');
foreach($data as $n) {
$tds = $n->find('td');
$dataRows = array();
$dataRows['num'] = $tds[0]->find('img',0)->href;
var_dump($dataRows);
}
有人可以指出這是否正確嗎? 如何設置為自動增加提交值,然后重復該過程(例如,提交darw = 1然后繪制= 2等),謝謝
<?php
while(true){
for($i=1;$i<5000;$i++){
$post_data['draw'] = $i; // will change every time like 1,2,3,4
$post_data['button'] = 'Submit';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//create cURL connection
$curl_connection =
curl_init('http://www.lldj.com/pastresult.php');
//set options
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT,
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
//perform our request
$result = curl_exec($curl_connection);
//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' .
curl_error($curl_connection);
//開始報廢
$t = $curl_connection->find('table',0); // ?? usualy referes to file_get_content Var
$data = $t->find('tr');
foreach($data as $n) {
$tds = $n->find('td');
$dataRows = array();
$dataRows['num'] = $tds[0]->find('img',0)->href;
var_dump($dataRows);
}
} for loop end here
}?>
這里只是骨架,可以使用不斷變化的id連續使用curl,您可以按自己的方式進行設置。
還請確保在獲取數據后清除變量。
使用像
...
curl_close($ch);
unset($fields_string);
...
載入頁面
獲取遠程內容的首選方法是file_get_contents()
。 采用:
$html = file_get_contents('http://www.lldj.com/pastresult.php');
就是這樣。
從頁面獲取內容
要從頁面獲取內容,通常將使用DOMDocument
和DOMXPath
:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$selector = new DOMXpath($doc);
// xpath query
$result = $selector->query('YOUR QUERY');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.