【问题标题】:Wordpress intercepting response Location header impossible, response headers are always null and emptyWordpress 拦截响应位置标头不可能,响应标头始终为空且为空
【发布时间】:2020-12-30 22:25:38
【问题描述】:

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

对于一个特定的请求,我完全依赖于响应标头。这是一个用于重定向的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 中运行良好,所有预期的响应标头都清晰可见。我还关闭了 Postman 中的以下重定向,以便能够在重定向发生之前拦截 Location 标头。

我试过了:

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

这是我 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;

【问题讨论】:

  • postman 生成的 PHP cURL 代码是什么样的?
  • @HowardE 在帖子末尾添加了 PHP cURL sn-p - 这是一个奇怪的 sn-p,因为它与实际的 Postman 设置不同,例如为生成的 cURL 启用了跟随重定向。

标签: wordpress http null header


【解决方案1】:

我通过使用另一个 http 库“请求”解决了这个问题。 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 的问题,但很好地替换了这个特定的请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 2012-09-26
    • 2021-07-27
    • 2019-06-20
    相关资源
    最近更新 更多