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

use*_*124 8 php arrays

我正在使用file_get_contents('php://input')从特定Web服务检索所有POST参数/值,例如:

$postText = file_get_contents('php://input');
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

inReplyToId=MG1133&to=61477751386&body=test&from=61477751386&messageId=166594397&rateCode=
Run Code Online (Sandbox Code Playgroud)

然后我需要获取每个单独的键/值对,因为我需要将它们设置到新数据库记录中的字段中.例如,我想最终得到:

$inReplyToId = MG1133
$to = 61477751386
$body = test
Run Code Online (Sandbox Code Playgroud)

一旦我知道了单个值,我就可以在数据库中设置相应的字段.我通常会使用:

if(isset($_POST['inReplyToId']) && $_POST['inReplyToId'] !== '' ) {
    $request->setField('_kf_GatewayMessageID', $_POST['inReplyToId']);
}
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,这不起作用,因为它不是一个application/x-www-form-urlencoded提交的表格.

Ler*_*eri 8

您可以使用parse_str函数来解析查询字符串:

$queryString = 'inReplyToId=MG1133&to=61477751386&body=test&from=61477751386&messageId=166594397&rateCode=';
$data = array();
parse_str($queryString, $data);
var_dump($data);
Run Code Online (Sandbox Code Playgroud)

编辑:

例如,我想最终得到:

$inReplyToId = MG1133
$to = 61477751386
$body = test
Run Code Online (Sandbox Code Playgroud)

要将数组键作为变量,您可以使用extract:

extract($data);
Run Code Online (Sandbox Code Playgroud)

编辑2:如果您已经拥有使用$_POST带有相应索引的变量的代码,则可以将数据与其合并:

$_POST = array_merge($data, $_POST);
Run Code Online (Sandbox Code Playgroud)

但修改这些变量是不可取的.