[英]Parsing result from 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, eg: 我正在使用
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
表单。
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. 但修改这些变量是不可取的。
看一下parse_str()函数,该函数将参数化字符串转换为关联数组。
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
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.