Wordpress 拦截响应 Location header 不可能,响应 headers 总是 null 和空的

Wordpress intercepting response Location header impossible, response headers are always null and empty

我正在为与第三方 HTTP 交互的 wordpress 网站开发插件 API。

对于一个特定的请求,我完全依赖响应 header。这是一个用于重定向的 Location header,我需要从中获取 url 和查询参数。

要求:

    return wp_remote_post($url, array(
        'body' => http_build_query(array(
            'token' => <omitted>,
            'email' => <omitted>,
            'password' => <omitted>
        )),
        'headers' => array(
            'Content-Type' => 'application/x-www-form-urlencoded'
        ),
        'redirection' => 0
    ));

在测试上面的请求时,我注意到 headers 响应字段是 null{}:

{
   "headers": {},
   "body": "",
   "response": {
      "code": 303,
      "message": "See Other"
   },
   "cookies": [
      {
         "name": "JSESSIONID",
         "value": "<omitted>",
         "expires": null,
         "path": "/<omitted>",
         "domain": "test.<omitted>.com",
         "host_only": true
      }
   ],
   "filename": null,
   "http_response": {
      "data": null,
      "headers": null,
      "status": null
   }
}

请求在 Postman 中运行良好,所有预期的响应 header 都清晰可见。我还针对此特定请求关闭了 Postman 中的以下重定向,以便能够在重定向发生之前拦截位置 header。

我试过:

  1. 另一个 wordpress 实例:同样的问题,没有 headers
  2. 本地 docker wordpress 实例:同样的问题,没有 headers
  3. 不同的 http 请求 (GET https://example.com):同样的问题,没有 headers
  4. 将重定向设置为 1 而不是 0:同样的问题,没有 headers,但是 重定向确实有效,这意味着 Location header 存在并且 wp_remote_post 拾取了它 - 这使得我更难理解为什么我看不到或检索任何 headers.

这是我 GET https://example.com:

时的响应
{
   "headers": {},
   "body": "<omitted>",
   "response": {
      "code": 200,
      "message": "OK"
   },
   "cookies":[],
   "filename": null,
   "http_response": {
      "data": null,
      "headers": null,
      "status": null
   }
}

我已经搜索了几个小时,但离解决这个问题还差一步。欢迎任何帮助。

编辑:有人向我要来自 Postman 的 cURL PHP 代码:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => '<omitted>/auth/login',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => 'token=<omitted>&email=<omitted>&password=<omitted>',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/x-www-form-urlencoded',
    'Cookie: JSESSIONID=<omitted>'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

我使用另一个 http 库 'Requests' 解决了这个问题。 Wordpress 通常默认包含这个库。

$url = "$this->base_url/$this->base_path/auth/login";
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');

$data = array(
    'token' => <omitted>,
    'email' => <omitted>,
    'password' => <omitted>
);

$options = array(
    'follow_redirects' => false
);

return Requests::post($url, $headers, $data, $options);

它没有解决 wp_remote 的问题,但可以很好地满足此特定要求。