api – PHPUnit – getallheaders不起作用

前端之家收集整理的这篇文章主要介绍了api – PHPUnit – getallheaders不起作用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在测试我的代码,我的标题有问题.在每个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 function getallheaders() 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;
    }
}

猜你在找的PHP相关文章