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!
你可以使用这段代码!尝试!
<?php
$postdata = file_get_contents("php://input");
//$postdata = '{"mail": "mistermuhittin@gmail.com", "number": 6969 }';
$obj = json_decode($postdata);
echo $obj->{'mail'}; // mistermuhittin@gmail.com
?>
#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!
你可以使用这段代码!尝试!
<?php
$postdata = file_get_contents("php://input");
//$postdata = '{"mail": "mistermuhittin@gmail.com", "number": 6969 }';
$obj = json_decode($postdata);
echo $obj->{'mail'}; // mistermuhittin@gmail.com
?>