溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

請(qǐng)求要素是json字符串時(shí),php如何獲取原生請(qǐng)求體

發(fā)布時(shí)間:2020-07-13 17:49:40 來(lái)源:網(wǎng)絡(luò) 閱讀:993 作者:黃昆侖 欄目:web開(kāi)發(fā)

php 常見(jiàn)問(wèn)題及解決方法


(1)請(qǐng)求要素是json字符串,后臺(tái)如何獲取

//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字符串轉(zhuǎn)化為對(duì)象

$post_object = json_decode($raw_data);

(3)如何把json對(duì)象轉(zhuǎn)化為數(shù)組

 //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服務(wù)器操作系統(tǒng)類(lèi)型

/***
 * @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";
    }
}

應(yīng)用:

$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(chǎn)是否包含字符串b

function strexists($a, $b)
{
	return !(strpos($a, $b) === FALSE);
}

(6)遞歸創(chuàng)建文件夾

function mkdirs($dir)
{    
	return is_dir($dir) or (mkdirs(dirname($dir)) and mkdir($dir, 0777));
}

php學(xué)習(xí)網(wǎng)站

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

請(qǐng)求要素是json字符串時(shí),php如何獲取原生請(qǐng)求體

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI