I'm using file_get_contents('php://input')
to retrieve all the POST parameters/values from a particular web service, eg:
$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.
You can use parse_str
function to parse query string:
$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($data);
Edit 2 : If you already have code that uses $_POST
variable with respective indexes you can merge data with it:
$_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
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.