【问题标题】:How to send header in postman via Php如何通过Php在邮递员中发送标题
【发布时间】:2021-01-09 03:51:45
【问题描述】:

我正在创建 Web 服务,我想通过 Php 在邮递员中发送“标题”,我该怎么做?如何在 php 中使用邮递员发送和获取标头?我尝试使用以下代码,但我收到响应“请发送访问令牌”,我该如何解决这个问题?提前致谢。 这是我的代码

$tokens=apache_request_headers();
        $token=$tokens['access_token'];
        if(empty($token))
        {
                    $responseJSON = array("Status" => false,"Message" => "Please enter access token");
                    header("content-type:application/json");
                    $response = json_encode($responseJSON);
                    echo $response; 
        }
        else
        {
                //your code
        }

【问题讨论】:

    标签: php rest postman


    【解决方案1】:

    有一个名为标题的选项卡,您可以在其中添加标题。

    如果您使用的是 fastCGI,那么标题将以驼峰形式出现,因此您需要将其用作:

    $token=$tokens['Access_token'];
    

    【讨论】:

    • 它不工作,我仍然没有得到我在邮递员标题中传递的“Access_token”
    • print_r($tokens) 并查看结果。
    • 我收到以下响应数组([Content-Length] => 11 [Content-Type] => text/plain;charset=UTF-8 [Accept] => / i> [Accept-Encoding] => gzip, deflate [Accept-Language] => en-US,en;q=0.5 [Connection] => close [Cookie] => xxxxxxxxx [Host] => xxxxx.in [Origin ] => xxxxxxxx [User-Agent] => xxxxxxxxxxxx [X-Forwarded-For] => xxxxxxx )
    • 如您所见,数组中没有“access_token”
    • 唯一可能的原因是您没有正确传递它.. 否则 apache_request_headers() 将获得所有结果.. print_r 应该显示您的自定义标题..
    【解决方案2】:

    添加:

    header('Content-Type: application/json');
    

    在 web api 中 PHP 代码的顶部

    【讨论】:

      猜你喜欢
      • 2017-07-20
      • 2015-05-27
      • 2019-01-20
      • 2023-02-20
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多