从 php 中的 http 请求中提取 base64 编码的字符串
Pulling a base64 encoded string from http request in php
背景
我写了一个小测试来确保我可以从我的移动应用程序发出的 POST 请求中提取数据。效果很好,
$rawJsonObj = file_get_contents('php://input');
$json = json_decode( $rawJsonObj, true );
$decodedData = base64_decode($json['data']);
file_put_contents('student-data/'.$json['username'].'.txt', $decodedData);
然后在我的 Slim Framework
应用程序中尝试相同的逻辑,但我用来创建文件的实际数据为空。但我仍然可以访问请求中发送的用户名和密码。
$app->post('/api/v1/endpoint', function ($request, $response, $args) {
$rawJsonObj = $request->getParams();
$json = json_decode( $rawJsonObj, true );
$decodedData = base64_decode($json['data']);
file_put_contents('test.txt', $decodedData);
return $response;
}
用户名和密码显示在请求中,当我将请求中的数据写入文件时可以看到它们。
$data = $request->getParams();
file_put_contents('2.txt', $data);
写入文件的数据,
","username":"myuname","password":"myPword"}
但是缺少base64编码的字符串,
例子
在 Swift 中,我正在创建这样的字典,然后 POST 它是 json,
let dict: [String: Any] = [
"username": named,
"password": password,
"data": data.base64EncodedString()
]
if let json = try? JSONSerialization.data(withJSONObject: dict, options: []) {
request.httpBody = json
}
问题
如何从 Slim Framework 3
中的请求正文访问实际的 "data": data.base64EncodedString()
?
我认为这与 $rawJsonObj = file_get_contents('php://input');
与 Slim 相比实际处理数据的方式有关?
Request::getParams() is a custom method by Slim (not part of PSR-7) that collects all input data; in other words, it's a rough equivalent of PHP's $_REQUEST
superglobal. Internally, it grabs the request body with Request::getParsedBody()(这个,PSR-7 的一部分)。这是与 $_REQUEST
或 $_POST
的主要区别所在:
If the request Content-Type
is either
application/x-www-form-urlencoded
or multipart/form-data
, and the
request method is POST
, this method MUST return the contents of
$_POST
.
Otherwise, this method may return any results of deserializing the
request body content; as parsing returns structured content, the
potential types MUST be arrays or objects only. A null
value
indicates the absence of body content.
虽然 $_POST
只解码标准形式的编码(因此在您的测试代码中您需要手动获取和解析数据)getParsedBody()
尝试解码其他编码,但是,就像 $_POST
,它需要一个适当的 Content-Type
才能这样做。如果您通过移动应用发送一个:
Content-Type: application/json
...它会按预期工作,因为 Slim 有一个内置的 JSON 解码器。
如果您无法发送编码声明,则需要手动解码内容。在这种情况下,获取原始请求主体的 PSR-7 方法是 Message::getBody(),即 returns 一个 stream(更具体地说,一个 Stream
实现 StreamInterface
).
的对象
背景
我写了一个小测试来确保我可以从我的移动应用程序发出的 POST 请求中提取数据。效果很好,
$rawJsonObj = file_get_contents('php://input');
$json = json_decode( $rawJsonObj, true );
$decodedData = base64_decode($json['data']);
file_put_contents('student-data/'.$json['username'].'.txt', $decodedData);
然后在我的 Slim Framework
应用程序中尝试相同的逻辑,但我用来创建文件的实际数据为空。但我仍然可以访问请求中发送的用户名和密码。
$app->post('/api/v1/endpoint', function ($request, $response, $args) {
$rawJsonObj = $request->getParams();
$json = json_decode( $rawJsonObj, true );
$decodedData = base64_decode($json['data']);
file_put_contents('test.txt', $decodedData);
return $response;
}
用户名和密码显示在请求中,当我将请求中的数据写入文件时可以看到它们。
$data = $request->getParams();
file_put_contents('2.txt', $data);
写入文件的数据,
","username":"myuname","password":"myPword"}
但是缺少base64编码的字符串,
例子
在 Swift 中,我正在创建这样的字典,然后 POST 它是 json,
let dict: [String: Any] = [
"username": named,
"password": password,
"data": data.base64EncodedString()
]
if let json = try? JSONSerialization.data(withJSONObject: dict, options: []) {
request.httpBody = json
}
问题
如何从 Slim Framework 3
中的请求正文访问实际的 "data": data.base64EncodedString()
?
我认为这与 $rawJsonObj = file_get_contents('php://input');
与 Slim 相比实际处理数据的方式有关?
Request::getParams() is a custom method by Slim (not part of PSR-7) that collects all input data; in other words, it's a rough equivalent of PHP's $_REQUEST
superglobal. Internally, it grabs the request body with Request::getParsedBody()(这个,PSR-7 的一部分)。这是与 $_REQUEST
或 $_POST
的主要区别所在:
If the request
Content-Type
is eitherapplication/x-www-form-urlencoded
ormultipart/form-data
, and the request method isPOST
, this method MUST return the contents of$_POST
.Otherwise, this method may return any results of deserializing the request body content; as parsing returns structured content, the potential types MUST be arrays or objects only. A
null
value indicates the absence of body content.
虽然 $_POST
只解码标准形式的编码(因此在您的测试代码中您需要手动获取和解析数据)getParsedBody()
尝试解码其他编码,但是,就像 $_POST
,它需要一个适当的 Content-Type
才能这样做。如果您通过移动应用发送一个:
Content-Type: application/json
...它会按预期工作,因为 Slim 有一个内置的 JSON 解码器。
如果您无法发送编码声明,则需要手动解码内容。在这种情况下,获取原始请求主体的 PSR-7 方法是 Message::getBody(),即 returns 一个 stream(更具体地说,一个 Stream
实现 StreamInterface
).