我正在测试我的代码,并且标头有一些问题。在我使用的每个 api 中
$headers = getallheaders();
为了实现这一点,当我使用应用程序或 chrome postman 扩展进行测试时,效果很好。
当我启动测试时,像这样
$client = $this->createClient();
$client->request('GET', '/api/shotcard',
['qrcode'=>'D0m1c173'], [],
['HTTP_API_TOKEN' => 'abc123']
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
当我尝试使用具有该测试令牌(不是我将在应用程序中使用的令牌)的用户使用该二维码拍摄卡片时,我在这里看到这样的调用:https://stackoverflow.com/a/11681422/5475228 https://stackoverflow.com/a/11681422/5475228。
测试失败是这样的:
PHP 致命错误:在第 42 行调用 /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php 中未定义的函数 AppBackendBundle\Controller\getallheaders()
From this https://www.popmartian.com/tipsntricks/2015/07/14/howto-use-php-getallheaders-under-fastcgi-php-fpm-nginx-etc/文章:
如果您使用 Nginx、PHP-FPM 或任何其他运行 PHP 的 FastCGI 方法
你可能已经注意到这个函数getallheaders()
才不是
存在。有许多创造性的解决方法,但 PHP 提供了
两个非常好的功能可以减轻您的痛苦。
来自用户贡献的评论getallheaders()
PHP 手册上的函数Joyview 在 gmail dot com https://www.php.net/manual/en/function.getallheaders.php#84262
if (!function_exists('getallheaders')) {
function getallheaders() {
$headers = [];
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) == 'HTTP_') {
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
return $headers;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)