请求要素是json字符串时,php如何获取原生请求体
php 常见问题及解决方法
(1)请求要素是json字符串,后台如何获取
//thisisacommonphplibrarybyhuangwei,//date:2014-07-03//seehttp://blog.sina.com.cn/s/blog_4657e98e0100dyxp.html//seehttp://www.cnblogs.com/fullhouse/archive/2012/04/24/2468870.htmlif(array_key_exists('HTTP_RAW_POST_DATA',$GLOBALS)){//判断是否有key-HTTP_RAW_POST_DATA$raw_data=$GLOBALS['HTTP_RAW_POST_DATA'];//always_populate_raw_post_data=On}if(empty($raw_data)){$raw_data=$_POST;}if(empty($raw_data)){//echo"raw_dataisempty";$raw_data=file_get_contents("php://input");}if(empty($raw_data)){$raw_data=$_GET;}if(empty($raw_data)){$raw_data=$_POST;}
(2)如何把接收到的json字符串转化为对象
$post_object=json_decode($raw_data);
(3)如何把json对象转化为数组
//convertobjecttoarrayfunctionobject_to_array($obj){if(is_array($obj)){return$obj;}$_arr=is_object($obj)?get_object_vars($obj):$obj;foreach($_arras$key=>$val){$val=(is_array($val))||is_object($val)?object_to_array($val):$val;$arr[$key]=$val;}return$arr;}
(4)获取php服务器操作系统类型
/****@returnstring:windowsorlinux*/functionserverOS(){$os_name=strtolower(php_uname('s'));$os_pos=strpos($os_name,'linux');if($os_pos===false){return"windows";}else{return"linux";}}
应用:
$root_path_index;//echoserverOS();if(serverOS()=='linux'){$root_path_index=-9;}else{$root_path_index=32;}$config['webroot']=substr(dirname(__FILE__),0,$root_path_index);///var/www/html/exchange
(5)字符串a是否包含字符串b
functionstrexists($a,$b){return!(strpos($a,$b)===FALSE);}
(6)递归创建文件夹
functionmkdirs($dir){returnis_dir($dir)or(mkdirs(dirname($dir))andmkdir($dir,0777));}
php学习网站
http://www.w3school.com.cn/php
http://www.php.net/manual/zh/function.json-decode.php
http://www.cnblogs.com/bananaplan/p/Sublime-Text-3-Powerful.html
推荐php IDE:http://pan.baidu.com/s/1kTA81E3
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。