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

React中Axios POST请求PHP后端时php://input为空但前端可接收响应的问题求助

React中Axios POST请求PHP后端时php://input为空但前端可接收响应的问题求助

我最近碰到一个百思不得其解的问题:在React里用Axios发送POST请求给PHP的Curl后端,前端确实能收到看起来正常的响应,但PHP里的$request_body和解析后的$data居然都是空的!已经卡了好几天了,有没有大佬能指点一下?

前端Axios请求代码

useEffect(() => {
  const getWeather = async () => {
    try {
      const response = await axios.post(
        "http://localhost/eliMullan/project1/backend/src/getWikipedia.php",
        {
          selectedCountryName: selectedCountry.properties.name.replace(/\s/g,'%20'),
          GEONAMES_USER: process.env.REACT_APP_GEONAMES_USER,
          withCredentials: true
        }
      );
      const wikiData = response.data.data;
      setWikiData(wikiData);
    } catch (error) {
      console.log(error);
    }
  };
  getWeather();
}, [selectedCountry]);

PHP后端代码

<?php
// Get country general info
header("Access-Control-Allow-Origin: http://localhost:3000");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

// remove for production
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$executionStartTime = microtime(true);

$request_body = file_get_contents('php://input');
var_dump($request_body);

$data = json_decode($request_body, true);
$selectedCountryName = $data["selectedCountryName"];
$geonamesUser = $data['GEONAMES_USER'];

$url = 'http://api.geonames.org/wikipediaSearchJSON?q=' . $selectedCountryName. '&maxRows=20&username=' . $geonamesUser;

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);

$result = curl_exec($ch);
curl_close($ch);

$decode = json_decode($result, true);

$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['status']['returnedIn'] = intval((microtime(true) - $executionStartTime) * 1000) . " ms";
$output['data'] = $decode["geonames"];

header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);
?>

错误信息与输出

前端console.log(response)能拿到后端返回的JSON,但PHP服务器的输出显示:

string(0) ""

<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>C:\xampp2\htdocs\eliMullan\project1\backend\src\getWikipedia.php</b> on line <b>18</b><br />
<br />
<b>Warning</b>:  Trying to access array offset on value of type null in <b>C:\xampp2\htdocs\eliMullan\project1\backend\src\getWikipedia.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  Undefined array key "geonames" in <b>C:\xampp2\htdocs\eliMullan\project1\backend\src\getWikipedia.php</b> on line <b>38</b><br />
{"status":{"code":"200","name":"ok","description":"success","returnedIn":"89 ms"},"data":null}

真心求帮忙,实在搞不懂为什么请求体传不到PHP里😭

备注:内容来源于stack exchange,提问作者Eli Mullan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 15:27:46