PHP通过接口查询车辆违章信息

/ 浏览:3216 次

某基早上找我帮他写一个查询的小功能,跟我说很简单,十分钟就可以写出来了。哎,确实抬举我了,写了一个多小时候才把逻辑写了出来。期间有几个小地方弄不懂的,咨询了下@程序员关关才明白,哎...还是要多多学习,所以还是写个日记记录下,怕以后用到然后忘记了。

代码由于时间问题,我只做了实现逻辑,其他的代码优化是完全不存在的,有路过的大神轻喷,权当是个人手札而已。

index.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="post.php" method="post">
    <textarea class="form-control" rows="4" placeholder="导入车牌号码,一个一行" id="number" name="number"></textarea><br/>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>

post.php

<?php 
$from = $_POST['number'];
file_put_contents('num.txt', $from."\r\n");
file_put_contents('num'. date("Y-m-d-H-i", time()) . 'txt', $from."\r\n");
echo '<p>写入成功,输入密钥开始查询</p>';
 ?>

 <!DOCTYPE html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <title>Document</title>
 </head>
 <body>
<form action="test.php" method="post">
  <p><input type="password" name="login"></p>
  <input type="submit" value="点击开始查询" />
</form>
 </body>
 </html>

test.php

<?php
/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set('Asia/Shanghai');
function send_post($url, $post_data)
{
    $postdata = http_build_query($post_data);
    $options = array('http' => array('method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60));
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}
function start()
{
    $txt = file_get_contents('./num.txt', '\\r');
    $array = explode("\r\n", $txt);
    $count_num = count($array);
    for ($i = 0; $i < $count_num; $i++) {
        $post_data = array('CarId' => '粤X' . $array[$i], 'Party' => 'XXXX集团有限公司');
        $mt = send_post('http://121.33.200.117:9090/Cases/CaseSummarySjgx/GetWaitTransactsForForm', $post_data);
        $jsons = str_replace(array('[', ']'), '', $mt);
        $json = "[{$jsons}]";
        $json = json_decode($json);
        $content = '';
        $num_file = 'list' . date("Y-m-d-H-i", time()) . '.txt';
        foreach ($json as $result) {
            if ($result->Tache == '调查办理') {
                $times = str_replace(array('/Date(', ')/'), '', $result->Inctime);
                $timedate = substr($times, 0, 10);
                $content .= '案件编号:' . $result->Code . "\r\n";
                $content .= '当事人:' . $result->Party . "\r\n";
                $content .= '当事人:' . $result->Name3 . "\r\n";
                $content .= '车牌号码:' . $result->License . "\r\n";
                $content .= '违法时间:' . date("Y年m月d日 H时i分", $timedate) . "\r\n";
                $content .= '违法地点:' . $result->Address . "\r\n";
                $content .= '案由:' . $result->Name . "\r\n";
                $content .= '案由:' . $result->BriefOfCase . "\r\n";
                $content .= '当前状态:' . $result->Tache . "\r\n";
                $content .= '执法机关:' . $result->Organization . "\r\n";
                $content .= '-----------------------------------------------' . "\r\n";
                file_put_contents($num_file, $content . "\r\n", FILE_APPEND);
            }
        }
    }
    $filename = './' . $num_file;
    header("Content-Type: application/force-download");
    header("Content-Disposition: attachment; filename=" . basename($filename));
    readfile($filename);
}
$login = empty($_POST['login']) ? 0 : $_POST['login'];
if ($login == 'admin999') {
    $put_txt = start();
} else {
    echo '你他妈没登录啊!';
}

如果你想转载,请注明来源或者出处