警告:输入变量超过1000 [英] Warning: Input variables exceeded 1000
问题描述
我正在PHP中构建类似RESTS的服务,该服务应接受大型JSON发布作为主要数据(我发送和读取数据的方式与此处讨论的非常相似:
I'm building a RESTS like service in PHP that should accept a large JSON post as main data (I send and read the data much like discussed here: http://forums.laravel.io/viewtopic.php?id=900)
问题是PHP给了我以下警告:
The problem is that PHP gives me the following Warning:
<b>Warning</b>: Unknown: Input variables exceeded 1000. To increase the limit change max_input_vars in php.ini. in <b>Unknown</b> on line <b>0</b><br />
有没有什么方法可以让PHP不计算输入变量(或者我应该停止启动警告)?
Is there any way to get PHP not count input variables (or should I just surpress startup warnings)?
推荐答案
我发现直接在PHP中(通过file_get_contents('php://input')
)直接处理json数据的正确方法是确保请求设置了正确的内容类型,即Content-type: application/json
在HTTP请求标头中.
I found out that the right way to handle json data directly in PHP (via file_get_contents('php://input')
) is to make sure the request sets the right content-type i.e. Content-type: application/json
in the HTTP request header.
在我的情况下,我要求使用curl结合以下代码从php请求页面:
In my case I'm requesting pages from php using curl with to this code:
function curl_post($url, array $post = NULL, array $options = array()) {
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 600
);
if(!is_null($post))
$defaults['CURLOPT_POSTFIELDS'] = http_build_query($post);
$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));
if(($result = curl_exec($ch)) === false) {
throw new Exception(curl_error($ch) . "\n $url");
}
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
throw new Exception("Curl error: ".
curl_getinfo($ch, CURLINFO_HTTP_CODE) ."\n".$result . "\n");
}
curl_close($ch);
return $result;
}
$curl_result = curl_post(URL, NULL,
array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => json_encode($out))
);
请注意CURLOPT_HTTPHEADER => array('Content-Type: application/json')
部分.
在接收方,我正在使用以下代码:
On the receiving side I'm using the following code:
$rawData = file_get_contents('php://input');
$postedJson = json_decode($rawData,true);
if(json_last_error() != JSON_ERROR_NONE) {
error_log('Last JSON error: '. json_last_error().
json_last_error_msg() . PHP_EOL. PHP_EOL,0);
}
请勿更改max_input_vars
变量..由于更改了设置正确标题的请求,所以我关于max_input_vars
的问题消失了.显然,PHP不会在设置某些Content-type
的情况下评估post变量.
Do not change the max_input_vars
variable. Since changing the request to set right headers my issue with max_input_vars
went away. Apparently does not PHP evaluate the post variables with certain Content-type
is set.
这篇关于警告:输入变量超过1000的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!