加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

api – PHPUnit – getallheaders不起作用

发布时间:2020-12-13 18:14:43 所属栏目:PHP教程 来源:网络整理
导读:我正在测试我的代码,我的标题有问题.在每个api我使用 $headers = getallheaders(); 为了得到它,当我使用app或crhome postman扩展测试时,这工作正常. 当我开始测试时,就像这样 $client = $this-createClient(); $client-request('GET','/api/shotcard',['qrco
我正在测试我的代码,我的标题有问题.在每个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 AppBackendBundleControllergetallheaders() 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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读