读取multipart / form-data请求PHP7

时间:2018-06-10 20:50:53

标签: php multipartform-data

如何从multipart / form-data请求中获取原始HTTP请求?

请求未写入test.log ..仅保存Response:

客户端可以上传文件等,我需要检查用户是否gzip二进制数据部分..如果不是我需要抛出错误

请求

$boundary = 'jlasdifj439';
$field1 = json_encode(['test' => 123]);
$body = '--'.$boundary."\r\n"
    .'Content-Disposition: form-data; name="json"'."\r\n"
    .'Content-Type: application/json'."\r\n"
    .'Content-Length: '.strlen($field1)."\r\n\r\n"
    .$field1."\r\n"
    .'--'.$boundary.'--';

$socket = curl_init();
curl_setopt_array($socket, [
    CURLOPT_POST            => true,
    CURLOPT_RETURNTRANSFER  => true,
    CURLOPT_VERBOSE         => true,
    CURLOPT_ENCODING        => '',
    CURLOPT_HTTP_VERSION    => CURL_HTTP_VERSION_2_0,
]);
curl_setopt($socket, CURLOPT_URL, 'https://my-url');
curl_setopt($socket, CURLOPT_HTTPHEADER, [
    'Content-Type: multipart/form-data; boundary='.$boundary,
]);
curl_setopt($socket, CURLOPT_POSTFIELDS, $body);
if(!$response = curl_exec($socket)){
    echo "ERROR!";
}

echo $response;

响应

$body = file_get_contents('php://input');
file_put_contents('/var/www/test.log', "Response:\n".$body);

test.log中

Response:

curl verbose

* Hostname was NOT found in DNS cache
*   Trying xxx.xxx.xxx.xxx...
* Connected to test.com (xxx.xxx.xxx.xxx) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*    subject: CN=*xxxxx
*    start date: 2017-10-02 00:00:00 GMT
*    expire date: 2018-12-01 23:59:59 GMT
*    subjectAltName: test.com matched
*    issuer: C=US; O=thawte, Inc.; OU=Domain Validated SSL; CN=thawte DV SSL SHA256 CA
*    SSL certificate verify ok.
> POST / HTTP/1.1
Host: xxxxx
Accept: */*
Accept-Encoding: deflate, gzip
Content-Type: multipart/form-data; boundary=jlasdifj439
Content-Length: 143

* upload completely sent off: 143 out of 143 bytes
< HTTP/1.1 200 OK
* Server nginx is not blacklisted
< Server: nginx
< Date: Sun, 10 Jun 2018 20:49:43 GMT
< Content-Type: application/json
< Transfer-Encoding: chunked
< Connection: keep-alive
< content-encoding: gzip
< 
* Connection #0 to host xxxxx left intact

0 个答案:

没有答案