php 常见问题及解决方法
(1)请求要素是json字符串,后台如何获取
//this is a common php library by huangwei ,
//date:2014-07-03
//see http://blog.sina.com.cn/s/blog_4657e98e0100dyxp.html
//see http://www.cnblogs.com/fullhouse/archive/2012/04/24/2468870.html
if(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_data is empty";
$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对象转化为数组
//convert object to array
function object_to_array($obj){
if(is_array($obj)){
return $obj;
}
$_arr = is_object($obj)? get_object_vars($obj) :$obj;
foreach ($_arr as $key => $val){
$val=(is_array($val)) || is_object($val) ? object_to_array($val) :$val;
$arr[$key] = $val;
}
return $arr;
}
(4)获取php服务器操作系统类型
/***
* @return string : windows or linux
*/
function serverOS(){
$os_name=strtolower(php_uname('s'));
$os_pos=strpos($os_name,'linux');
if($os_pos === false) {
return "windows";
}
else {
return "linux";
}
}
应用:
$root_path_index;
//echo serverOS();
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
function strexists($a, $b)
{
return !(strpos($a, $b) === FALSE);
}
(6)递归创建文件夹
function mkdirs($dir)
{
return is_dir($dir) or (mkdirs(dirname($dir)) and mkdir($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
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com