php解析请求url并返回json数据

时间:2022-10-16 19:18:24
<?php                                                                                                                                        
paserRequest($_SERVER["QUERY_STRING"]);
function paserRequest($strReq)
{
parse_str($strReq);//解析请求参数
$cpIds = explode(',', $cp);//按逗号分隔
$cpArray = array();
for($index=0;$index<count($cpIds);$index++){
array_push($cpArray,array(
"cp" => (int)( $cpIds[$index] ),
"flag" => -1,
"fc" => 0,
"ta" => 1,
"cr" => $cpIds[$index])
);
} //构造数组
$jsonFinal = array("data" => $cpArray, "seq"=>$seq);
$json_string = json_encode($jsonFinal);//构造json对象
print_r($json_string);
}
?>


完全不懂服务器的东西,这点代码写了三个小时。。。

简单的记录一下要点,方便以后查阅:

  • 存储全局数据的$_SERVER变量
  • 解析字符串的函数
  • json编码、解码
  • 输出
  • 另外,nginx服务器需要配置php
        location ~ ^(.+\.php)(.*)$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9999;

include fastcgi_params;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}

fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SERVER_NAME $host;
server_name_in_redirect off;
}