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
?>