Skip to content

Instantly share code, notes, and snippets.

@beanyoung
Created July 10, 2014 07:00
Show Gist options
  • Save beanyoung/41917cbf70690d4c4b2d to your computer and use it in GitHub Desktop.
Save beanyoung/41917cbf70690d4c4b2d to your computer and use it in GitHub Desktop.
<?php
$app_key = 'demo_key';
$app_secret = 'demo_secret';
$rfc_1123_date = gmdate('D, d M Y H:i:s T', time());
file_put_contents('./log.txt',json_encode($_POST),FILE_APPEND);
// $_POST['job_id'] ='577c460f-140a-4112-8898-a943ea020ccc';
if(isset($_POST['job_id'])){
file_put_contents('./log.txt',json_encode($_POST),FILE_APPEND);
$job_id = $_POST['job_id'];
$ci = curl_init();
$url = 'http://api.buding.cn/v1/job/'.$job_id;
$signature_str = 'GET&/v1/job/'.$job_id.'&'.$rfc_1123_date.'&0&'.md5($app_secret);
$signature = md5($signature_str);
$header = array("Authorization:{$app_key}:{$signature}","Date:{$rfc_1123_date}");
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);// 连接超时
curl_setopt($ci, CURLOPT_TIMEOUT, 30);// 执行超时
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);//文件流的形式返回,而不是直接输出
curl_setopt($ci, CURLOPT_ENCODING, "");
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ci);
$response = json_decode($response,true);
if(isset($response['code'])){
$data = $response;
}else{
$no = count($response['violations']); //违章数量
$totalpoint = 0; //总扣分
$totalfine = 0; //总罚款
$html = '';
$weekarray=array("日","一","二","三","四","五","六");
$datalist = $response['violations'];
if($no>0){
$datalist = array_sort($datalist,'time','desc'); //按照时间倒序
foreach($datalist as $v){
if($v['point']=='-1'){
$v['point'] = '未知';
}else{
$v['point'] = $v['point'].'分';
$totalpoint += $v['point'];
}
$totalfine += $v['fine'];
$timestamp = strtotime($v['time']);
$day = date('Y.m.d',$timestamp);
$time = date('H:i',$timestamp);
$week = '星期'.$weekarray[date('w',$timestamp)];
$html.="<div class='oneres'><div class='fit-width resault'><p class='violation_type'>$v[violation_type]</p><p class='time'>$day $week $time</p><p class='address'>$v[address]</p><p><span class='kf'>扣分</span><b class='point'>$v[point]</b><span class='fk'>罚款</span><b class='fine'>$v[fine]元</b></p></div></div>";
}
}
$data = array(
'job_id' => $response['job_id'],
'job_status' => $response['job_status'],
'no' => $no,
'totalpoint' => $totalpoint,
'totalfine' => $totalfine,
'html' => $html,
'time' => date("m.d H:i")
);
}
echo json_encode($data);
}else{
$carnumber = strtoupper($_POST['license_plate_num']);
// $license_plate_num = $_POST['car_province'].strtoupper($_POST['carnumber']);
// $engine_num = strtoupper($_POST['engine_num']);
// $body_num = strtoupper('选填');
// $city_pinyin = 'beijing';
$license_plate_num = $_POST['car_province'].$carnumber;
$engine_num = strtoupper($_POST['engine_num']);
$body_num = strtoupper($_POST['body_num']);
$city_pinyin = $_POST['city_pinyin'];
$callback_url = "";
$no_wait = 'true';
$url = 'http://api.buding.cn/v1/jobs';
$postfields = "license_plate_num=$license_plate_num&engine_num=$engine_num&body_num=$body_num&city_pinyin=$city_pinyin&callback_url=$callback_url&no_wait=$no_wait";
$signature_str = 'POST&/v1/jobs&'.$rfc_1123_date.'&'.strlen($postfields).'&'.md5($app_secret);
$signature = md5($signature_str);
$header = array("Authorization:{$app_key}:{$signature}","Date:{$rfc_1123_date}");
$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);// 连接超时
curl_setopt($ci, CURLOPT_TIMEOUT, 30);// 执行超时
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);//文件流的形式返回,而不是直接输出
curl_setopt($ci, CURLOPT_ENCODING, "");
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLOPT_POST, true);// post
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);// post数据 可为数组、连接字串
curl_setopt($ci, CURLOPT_HTTPHEADER, $header);
echo $response = curl_exec($ci);
file_put_contents('./log.txt',json_encode(curl_error($ci)),FILE_APPEND);
}
function array_sort($arr,$keys,$type='asc'){
$keysvalue = $new_array = array();
foreach ($arr as $k=>$v){
$keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
asort($keysvalue);
}else{
arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k=>$v){
$new_array[$k] = $arr[$k];
}
return $new_array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment