php://input 解析,解析file_get_contents('php:// input')的結果

I'm using file_get_contents('php://input') to retrieve all the POST parameters/values from a particular web service, e.g.:

我正在使用file_get_contents('php:// input')從特定的Web服務中檢索所有POST參數/值,例如:

$postText = file_get_contents('php://input');

which results in something like this:

結果是這樣的:

inReplyToId=MG1133&to=61477751386&body=test&from=61477751386&messageId=166594397&rateCode=

I then need to get each of the individual key/value pairs as I need to set them into fields in a new database record. For example I would like to end up with:

然后我需要獲取每個單獨的鍵/值對,因為我需要將它們設置到新數據庫記錄中的字段中。例如,我想最終得到:

$inReplyToId = MG1133

$to = 61477751386

$body = test

Once I know the individual value I can set the corresponding field in the database. I would normally use:

一旦我知道了單個值,我就可以在數據庫中設置相應的字段。我通常會使用:

if(isset($_POST['inReplyToId']) && $_POST['inReplyToId'] !== '' ) {

$request->setField('_kf_GatewayMessageID', $_POST['inReplyToId']);

}

But that won't work in this case as it's not a application/x-www-form-urlencoded form that is being submitted.

但是在這種情況下這不起作用,因為它不是正在提交的application / x-www-form-urlencoded表單。

3 个解决方案

#1

6

You can use parse_str function to parse query string:

您可以使用parse_str函數來解析查詢字符串:

$queryString = 'inReplyToId=MG1133&to=61477751386&body=test&from=61477751386&messageId=166594397&rateCode=';

$data = array();

parse_str($queryString, $data);

var_dump($data);

Edit:

For example I would like to end up with:

例如,我想最終得到:

$inReplyToId = MG1133

$to = 61477751386

$body = test

To get array keys as variable you can use extract:

要將數組鍵作為變量,可以使用extract:

extract($data);

Edit 2: If you already have code that uses $_POST variable with respective indexes you can merge data with it:

編輯2:如果您已經擁有使用$ _POST變量和相應索引的代碼,則可以將數據與其合並:

$_POST = array_merge($data, $_POST);

But modifing these variables is not advisable.

但修改這些變量是不可取的。

#2

1

Take a look at the parse_str() function, which coverts a parameterized string, into an associative array.

看一下parse_str()函數,該函數將參數化字符串轉換為關聯數組。

#3

0

You can use this code! try!

你可以使用這段代碼!嘗試!

$postdata = file_get_contents("php://input");

//$postdata = '{"mail": "mistermuhittin@gmail.com", "number": 6969 }';

$obj = json_decode($postdata);

echo $obj->{'mail'}; // mistermuhittin@gmail.com

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值