我正在测试我的代码,我的标题有问题.在每个api我使用
$headers = getallheaders();
为了得到它,当我使用app或crhome postman扩展测试时,这工作正常.
当我开始测试时,就像这样
$client = $this->createClient(); $client->request('GET','/api/shotcard',['qrcode'=>'D0m1c173'],[],['HTTP_API_TOKEN' => 'abc123'] ); $this->assertEquals(200,$client->getResponse()->getStatusCode());
我尝试用带有该测试令牌的用户拍摄带有该qrcode的卡(不是我将在应用程序中使用的令牌),我在这里看到这样的调用:https://stackoverflow.com/a/11681422/5475228.
测试以这种方式失败:
PHP Fatal error: Call to undefined function AppBackendBundle\Controller\getallheaders() in /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.PHP on line 42
从
this文章:
If you use Nginx,PHP-FPM or any other FastCGI method of running PHP
you’ve probably noticed that the functiongetallheaders()
does not
exist. There are many creative workarounds in the wild,but PHP offers
two very nice features to ease your pain.
if (!function_exists('getallheaders')) { function getallheaders() { $headers = []; foreach ($_SERVER as $name => $value) { if (substr($name,5) == 'HTTP_') { $headers[str_replace(' ','-',ucwords(strtolower(str_replace('_',' ',substr($name,5)))))] = $value; } } return $headers; } }