1. 云栖社区>
  2. PHP教程>
  3. 正文

php编写接口返回json格式结果

作者:用户 来源:互联网 时间:2017-12-01 19:17:06

phpjson接口格式结果返回编写

php编写接口返回json格式结果 - 摘要: 本文讲的是php编写接口返回json格式结果,<?php  header("content-type:application/json; charset=utf-8");  require $_SERVER["DOCUMENT_ROOT"]."/common.inc.php"; 

<?php
  header("content-type:application/json; charset=utf-8");
  require $_SERVER["DOCUMENT_ROOT"]."/common.inc.php"; 
  //require $_SERVER["DOCUMENT_ROOT"]."/mcweb/function.php";
  $reportarr['haveTraffics'] = $row[HAVETRAFFICS];
  $reportarr['siteTrafficTop100'] = $row[SITETRAFFICTOP100];
  $reportarr['keywordTraffic'] = $row[KEYWORDTRAFFIC];
  $reportarr['keywordTrafficTop100'] = $row[KEYWORDTRAFFICTOP100];
  $reportarr['lottery'] = $row[LOTTERY];
  $reportarr['diRate'] = $row[DIRATE];
  $reportarr['baiduRate'] = $row[BAIDURATE];
  $reportarr['sogouRate'] = $row[SOGOURATE];
  $reportarr['bingRate'] = $row[BINGRATE];
  $reportarr['haosouRate'] = $row[HAOSOURATE];
  $reportarr['smRate'] = $row[SMRATE];
  $reportarr['Date_'] = $row[DATE_];
  
  define('PROJECTID', 0); 
  define('PROJECTNAME', 1);
  define('PROJECTPID', 2);
  define('MANAGER', 3);
  define('HAVETRAFFICS', 4);
  define('SITETRAFFICTOP100', 5);
  define('KEYWORDTRAFFIC', 6);
  define('KEYWORDTRAFFICTOP100', 7);
  define('LOTTERY', 8);
  define('DIRATE', 9);
  define('BAIDURATE', 10);
  define('SOGOURATE', 11);
  define('BINGRATE', 12);
  define('HAOSOURATE', 13);
  define('SMRATE', 14);
  define('DATE_', 15);
   
 $date = trim($_GET['date']); 
 $project = iconv('UTF-8','gb2312//IGNORE',trim($_GET['pro']));
 $type = $_GET['type'];
 
 
 function inject_check($Sql_Str) {//?
    $check=preg_match('/select|insert|update|delete|/'|//*|/*|/././/|/.//|union|into|load_file|outfile/i',$Sql_Str);
    if ($check) {
        echo '<script language="JavaScript">alert("ϵͳ¾¯¸棺/n/nȫ²»Ҫ³¢˔՚²ϊ�¬·Ƿ¨ؖ·�ɫ£¡");</script>';
        exit();
    }else{
        return $Sql_Str;
    }
  }
  
 //¼풩ˤɫ 
 $date = inject_check($date); 
 $project = inject_check($project);
 $type = inject_check($project);
 
 if($date == '')   $date = date("Y-m-d");
 
 if ((!isset($type))||(empty($type))) {
  $json=getAllMcReport($project, $date);
 }
 else
 {
  $json = getMcReport($project, $date);
 }
 
 echo $json;
 
 function getAllMcReport($project, $date) {
  /*
  $sql = 'select * from t_ccs_project a, t_ccs_project_hz b where a.PrjID=b.PrjID and a.PrjName='315ЮĿ' and b.Date_='2016-06-19'';
  $result = db_query($sql);
 
while ($row=mysql_fetch_array($result)) { 
$prj_arr[$row['PrjID']] = $row['PrjName'];

  }
  */
 
 }
 
 function getMcReport($project, $date) {
  $sql = "select a.PrjID, a.PrjName, a.pid, a.manager, b.Site_Flow as haveTraffics,
  b.s_top100 as siteTrafficTop100, b.key_flow as keywordTraffic,b.k_top100 as keywordTrafficTop100,
b.BKNums as lottery,b.Input_Rate as diRate,b.baidu_Rate as baiduRate, b.sogou_Rate as sogouRate,
b.bing_Rate as bingRate,b.haosou_Rate as haosouRate,b.sm_Rate as smRate,b.Date_ from t_ccs_project a, 
t_ccs_project_hz b where a.PrjID=b.PrjID and a.PrjName='$project' and b.Date_='$date'";
 


  $result = db_query($sql);
  $row=mysql_fetch_row($result);
 
  $reportarr = array();
  $reportarr['id'] = $row[PROJECTID];
  $reportarr['project'] = iconv('gb2312','utf-8',$row[PROJECTNAME]);
  $reportarr['pid'] = $row[PROJECTPID];
  $reportarr['manager'] = $row[MANAGER];
  $reportarr['haveTraffics'] = $row[HAVETRAFFICS];
 
  $stop100arr = array();
  $temparr = explode("/r/n", $row[SITETRAFFICTOP100]);
    $arrlength = count($temparr);
    $stop100arrline = array();
for($x = 0; $x < $arrlength; $x++) {
$stop100arrItem = explode("/t", $temparr[$x]);
   $stop100arrline['site']=$stop100arrItem[0];
   $stop100arrline['traffics']=$stop100arrItem[1];
   $stop100arrline['ip']=$stop100arrItem[2];
   $stop100arrline['crate']=$stop100arrItem[3];
   $stop100arr[]=$stop100arrline;
}
 
 
  $reportarr['siteTrafficTop100'] = $stop100arr;
  $reportarr['keywordTraffic'] = $row[KEYWORDTRAFFIC];
 
  $ktop100arr = array();
  $temparr = explode("/r/n", $row[KEYWORDTRAFFICTOP100]);
    $arrlength = count($temparr);
    $ktop100arrline = array();
for($x = 0; $x < $arrlength; $x++) {
$ktop100arrItem = explode("/t", $temparr[$x]);
   $ktop100arrline['keyword']=iconv('gb2312','utf-8',$ktop100arrItem[0]);
   $ktop100arrline['ktraffics']=$ktop100arrItem[1];
   $ktop100arr[] = $ktop100arrline; 
}
 
  $reportarr['keywordTrafficTop100'] = $ktop100arr;
  $reportarr['lottery'] = $row[LOTTERY];
  $reportarr['diRate'] = $row[DIRATE];
  $reportarr['baiduRate'] = $row[BAIDURATE];
  $reportarr['sogouRate'] = $row[SOGOURATE];
  $reportarr['bingRate'] = $row[BINGRATE];
  $reportarr['haosouRate'] = $row[HAOSOURATE];
  $reportarr['smRate'] = $row[SMRATE];
  $reportarr['Date_'] = $row[DATE_];
 
 
   
  return json_encode(array('mcreport'=>$reportarr));
  mysql_free_result($result);
  mysql_close();
 }
 /*
 $sql="select PrjID from myccs.t_ccs_project where `enable`=1 and PrjName='$project'"; 
 $prjID=db_first_value($sql);
 if ($prjID=='')
 {
  $json='{"mcreport":[]}';
 }
 else
 {
  $sql = "select * from t_ccs_project_hz where PrjID='$prjID' and Date_='$date'";
 }
 
 
  echo 'fsdfs';
 
 exit('test');
    
 $result = db_query($sql);
 while ($row=mysql_fetch_array($result)) { 
 $prj_arr[$row['PrjID']] = $row['PrjName'];
 }
$p = "'"; 
 if($date == '')   $date = date("Y-m-d");
 */
?>

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , json , 接口 , 格式 , 结果 , 返回 编写 ,以便于您获取更多的相关知识。