You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地PHP服务端无法获取wp_remote_get请求参数及空响应问题

问题:WP远程GET请求参数无法获取且响应体为空

发起请求的代码

//request parameters
$request_args = array(
    'body' => [
        'action' => 'version',
        'type' => $this->product_type,
        'name' => $this->product_type == 'plugin' ? $this->plugin_slug : $this->theme,
    ],
    'timeout'     => '5',
    'redirection' => '5',
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => array(
        'content-type' => 'text/plain',
    ),
    'cookies'     => array(),
);

//get request
$response = wp_remote_get($this->server_url,$request_args);

服务端代码

<?php

require_once __DIR__.'/ProductInfo.php';
require_once __DIR__.'/VersionDetector.php';

use provider\ProductInfo;
use provider\VersionDetector;

defined( 'ABSPATH' ) || exit;

if(isset($_GET['action']))
{
    switch($_GET['action'])
    {
        case 'version':
            VersionDetector::version();
            break;
        case 'info':
            ProductInfo::info();
            break;
    }
}

问题现象

  • 请求返回200状态码,但服务端无法通过$_GET['action']获取参数
  • 服务端顶部添加echo或return语句后,响应体仍为空;尝试将参数拼接为URL查询字符串,结果依旧
  • 响应var_dump结果:
array(6) {
  ["headers"]=>
  object(WpOrg\Requests\Utility\CaseInsensitiveDictionary)#2460 (1) {
    ["data":protected]=>
    array(5) {
      ["date"]=>
      string(29) "Wed, 19 Apr 2023 07:48:39 GMT"
      ["server"]=>
      string(46) "Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.6"
      ["x-powered-by"]=>
      string(9) "PHP/8.1.6"
      ["content-length"]=>
      string(1) "0"
      ["content-type"]=>
      string(24) "text/html; charset=UTF-8"
    }
  }
  ["body"]=>
  string(0) ""
  ["response"]=>
  array(2) {
    ["code"]=>
    int(200)
    ["message"]=>
    string(2) "OK"
  }
  ["cookies"]=>
  array(0) {
  }
  ["filename"]=>
  NULL
  ["http_response"]=>
  object(WP_HTTP_Requests_Response)#2545 (5) {
    ["data"]=>
    NULL
    ["headers"]=>
    NULL
    ["status"]=>
    NULL
    ["response":protected]=>
    object(WpOrg\Requests\Response)#2467 (10) {
      ["body"]=>
      string(0) ""
      ["raw"]=>
      string(215) "HTTP/1.1 200 OK
Date: Wed, 19 Apr 2023 07:48:39 GMT
Server: Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.6
X-Powered-By: PHP/8.1.6
Content-Length: 0
Connection: close
Content-Type: text/html; charset=UTF-8

"
      ["headers"]=>
      object(WpOrg\Requests\Response\Headers)#2469 (1) {
        ["data":protected]=>
        array(5) {
          ["date"]=>
          array(1) {
            [0]=>
            string(29) "Wed, 19 Apr 2023 07:48:39 GMT"
          }
          ["server"]=>
          array(1) {
            [0]=>
            string(46) "Apache/2.4.53 (Win64) OpenSSL/1.1.1n PHP/8.1.6"
          }
          ["x-powered-by"]=>
          array(1) {
            [0]=>
            string(9) "PHP/8.1.6"
          }
          ["content-length"]=>
          array(1) {
            [0]=>
            string(1) "0"
          }
          ["content-type"]=>
          array(1) {
            [0]=>
            string(24) "text/html; charset=UTF-8"
          }
        }
      }
      ["status_code"]=>
      int(200)
      ["protocol_version"]=>
      float(1.1)
      ["success"]=>
      bool(true)
      ["redirects"]=>
      int(0)
      ["url"]=>
      string(44) "http://localhost/update%20server/updater.php"
      ["history"]=>
      array(0) {
      }
      ["cookies"]=>
      object(WpOrg\Requests\Cookie\Jar)#2480 (1) {
        ["cookies":protected]=>
        array(0) {
        }
      }
    }
    ["filename":protected]=>
    NULL
  }
}

解决建议

1. 修正GET请求参数传递方式

wp_remote_get的body参数不会自动拼接到URL查询字符串中,GET请求的参数需通过URL查询串传递,有两种实现方式:

// 方式一:直接拼接参数到URL
$server_url_with_params = add_query_arg([
    'action' => 'version',
    'type' => $this->product_type,
    'name' => $this->product_type == 'plugin' ? $this->plugin_slug : $this->theme,
], $this->server_url);
$response = wp_remote_get($server_url_with_params, [
    'timeout'     => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => [
        'content-type' => 'text/plain',
    ],
    'cookies'     => [],
]);

// 方式二:使用WP的query参数
$request_args = array(
    'query' => [ // 用query替代body
        'action' => 'version',
        'type' => $this->product_type,
        'name' => $this->product_type == 'plugin' ? $this->plugin_slug : $this->theme,
    ],
    'timeout'     => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => [
        'content-type' => 'text/plain',
    ],
    'cookies'     => [],
);
$response = wp_remote_get($this->server_url, $request_args);

2. 排查服务端输出阻断问题

  • 注释掉defined( 'ABSPATH' ) || exit;语句:如果服务端不在WordPress环境中,ABSPATH未定义会直接触发exit,导致无任何输出
  • 添加调试代码验证输出:在服务端最顶部插入以下代码,确认是否能正常返回内容
<?php
var_dump($_GET);
echo "Test response content";
exit;
// 暂时注释后续依赖引入和业务代码

3. 检查服务端依赖与方法输出

  • 确认ProductInfo.php和VersionDetector.php文件路径正确、文件存在
  • 检查VersionDetector::version()和ProductInfo::info()方法内部逻辑:确保方法中有echo或print输出内容,仅return不会向响应体输出任何内容

内容的提问来源于stack exchange,提问作者Navid Nasro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 10:29:57