问题
I have the following code
protected function parse($header) {
$headerArr = http_parse_headers($header);
foreach ($headerArr as $key => $value) {
echo $key.'=>'.$value;
}
}
where $header is a http header. This code is in a PHP Class in a symfony2 project. I can execute it in a browser an get the expected results the keys and values of the parsed header like this
- Response Code=>200
- Response Status=>OK
- Date=>Sun, 13 Jul 2014 21:39:51 GMT
- Server=>Apache/2.4.7 (Ubuntu)
- Last-Modified=>Wed, 26 Mar 2014 22:51:46 GMT
- Etag=>502-4f58a50290480
- Accept-Ranges=>bytes
- Content-Length=>1282
- Content-Disposition=>attachment; filename= mvc.zip
- Keep-Alive=>timeout=5, max=100
- Connection=>Keep-Alive
- Content-Type=>application/zip
The problem is when i try to run a unit test for the function parse(), the PHPUnit can't find the http_parse_headers() function an throws "PHP Fatal error: Call to undefined function http_parse_headers()
How is this posible? How can I fix it?
回答1:
Doing some research I find a solution to my problem. Actually it has been already answered here in Stackoverflow. The php execution from the console use a different php.ini file than the execution of php from a web browser. All that is needed is to enable the pecl_http extension in the corresponding .ini file. To do this execute in command-line
php --ini
to check where is located the .ini file used by command/line php, then enable the extensions in the file
extension=http.so
To check if the extension is loaded run from the command-line
php -m
回答2:
A quick search of the PHP manual shows that http_parse_headers is part of a pecl extension: PECL pecl_http >= 0.10.0
Your server and your command line php versions must be different. The php manual has a work around but hopefully you can just update your command line version.
来源:https://stackoverflow.com/questions/25596949/phpunit-call-to-undefined-function-http-parse-headers-error