[英]Extracting data from protect TXT file and turning it into a table via PHP
我有一些在文本文檔中格式化並用逗號分隔的選舉數據,我正在嘗試使用 PHP 為我們的網站重新格式化。
文本文件受用戶名和密碼保護,因此我試圖弄清楚如何將數據提取到數組中進行操作。 這是鏈接(https://go.elections.hawaii.gov/media-results/files/summary.txt ),但我在此處提取了數據樣本以供參考( http://lilly-digital.com/test /summary.txt )。
使用下面的代碼,我能夠生成此表,但我需要重新排序並排除一些數據: https://lilly-digital.com/test/parse.php關於如何執行此操作的任何建議?
<?php
$filename = 'https://lilly-digital.com/test/summary.txt';
$the_big_array = [];
if (($h = fopen("{$filename}", "r")) !== FALSE)
{
while (($data = fgetcsv($h, 1000, ",")) !== FALSE)
{
$the_big_array[] = $data;
}
// Close the file
fclose($h);
}
$build = '<table><tbody>';
foreach($the_big_array as $row)
{
$build .= '<tr>';
foreach($row as $item)
{
$build .= "<td>{$item}</td>";
}
$build .= '</tr>';
}
$build .= '</tbody></table>';
echo $build;
?>
總之,知道如何使用用戶名和密碼提取數據並重新格式化數據嗎? 我對此比較陌生。
提前致謝。
服務器使用HTTP 基本身份驗證進行保護,因此您需要在請求中傳遞用戶名和密碼。 您可以使用stream_context_create做到這一點
$username='Roland';
$password='Orzabal';
$auth = base64_encode("$username:$password");
$header = array("Authorization: Basic $auth");
$opts = array( 'http' => array ('method'=>'GET', 'header'=>$header));
$ctx = stream_context_create($opts);
//stream context is ready
$filename = 'https://lilly-digital.com/test/summary.txt';
$the_big_array = [];
stream 上下文是fopen中的最后一個可選參數
if (($h = fopen("{$filename}", "r", false, $context)) !== FALSE)
{
//stuff
}
//other stuff
您可以嘗試以下代碼:
$https_user = "user_name";
$https_password = "password";
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);
$https_server = 'https://lilly-digital.com/test/summary.txt';
$context = stream_context_create($opts);
$url = $https_server;
$result = file_get_contents($url, false, $context);
$line_array = explode(PHP_EOL, $result);
$final_result = array_map(function($val) {
return explode(',', $val);
}, $line_array);
$final_result
將代表一個多維數組。 根據需要處理此數組
驗證碼使用來自Ariful Islam的回答
$https_user = "user_name";
$https_password = "password";
$opts = array('http' =>
array(
'method' => 'GET',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);
一旦通過身份驗證,您應該嘗試array_combine function 它將創建一個數組,其中的鍵包含字段名,值包含Mysql表的字段數據。 代碼將如下所示:
$fileUrl = 'https://lilly-digital.com/test/summary.txt';
$delimiter=',';
$header = NULL;
$data = array();
if (($handle = fopen($fileUrl, 'r')) !== FALSE){
while (($row = fgetcsv($handle,0, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
output 將類似於以下
Array
(
[0] => Array
(
[Contest ID] => 2
[Contest Title] => U.S. Representative, Dist I - D
[Contest Seq Nbr] => 2
[Contest Type] => OF
[Contest Party] =>
[Absentee Mail Blank Votes] => 0
[Absentee Walk-in Blank Votes] => 0
[Election Blank Votes] => 0
[Absentee Mail Over Votes] => 0
[Absentee Walk-in Over Votes] => 0
[Election Over Votes] => 0
[Registered Voters] => 795248
[Total Precincts] => 115
[Counted Precincts] => 0
[Candidate ID] => 9
[Candidate Name] => CASE, Ed
[Candidate Seq Nbr] => 1
[Candidate Party] =>
[Absentee Mail Votes] => 0
[Absentee Walk-in Votes] => 0
[Election Votes] => 0
[Total Votes] => 0
)
[1] => Array ... so on
要插入數據庫,需要使用以下代碼
$fields = implode("','",$header);
$fields = "'".$fields."'";
foreach($data as $key => $value) {
$value = implode("','",$value);
$value = "'".$value."'";
$sql = "INSERT INTO TABLENAME ($fields) VALUES ($value)";
}
注意:強烈建議使用Mysql Prepared statement 。 您可以在實現時使用上述邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.