請求要素是json字串時,php如何擷取原生請求體

來源:互聯網
上載者:User

標籤:常見問題   擷取請求要素   擷取json   請求提是json   擷取伺服器作業系統   php   

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.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_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 arrayfunction 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

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/57/91/wKioL1Sefb_wt5_wAAdpPX066mo693.jpg" title="2014-12-27_17-33-26.jpg" alt="wKioL1Sefb_wt5_wAAdpPX066mo693.jpg" />

本文出自 “whuang” 部落格,請務必保留此出處http://huangkunlun520.blog.51cto.com/2562772/1596591

請求要素是json字串時,php如何擷取原生請求體

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.