为什么简单的PHP服务器不打印或接收POST数据?

yrefmtwq  于 2023-02-07  发布在  PHP
关注(0)|答案(1)|浏览(88)

我在命令行上使用php -S localhost:8000 index.php运行PHP服务器,服务器的内容如下

<?php

require 'vendor/autoload.php';
use Embed\Embed;
$embed = new Embed();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    flush();
    ob_flush();
    echo $_POST;
    print($_POST);
    var_dump($_POST);

  // Prepare the response
  $response = [
    'status' => 'success',
    'message' => 'why does this not reach you',
  ];

  // Return the response as JSON
  echo json_encode($response);
} else {
  // Return an error response if the request method is not POST
  $response = [
    'status' => 'error',
    'message' => 'Invalid request method. POST request expected.',
  ];

  echo json_encode($response);
}
?>

当请求进入时,命令行会做出如下React:

[Fri Feb  3 18:11:47 2023] PHP 8.2.1 Development Server (http://localhost:8000) started
[Fri Feb  3 18:11:48 2023] 127.0.0.1:54363 Accepted
[Fri Feb  3 18:11:48 2023] 127.0.0.1:54363 Closing

但在请求传入后,它几乎立即显示“Closing”,而且我无法使用echoprintvar_dump获取打印请求的内容。客户端确实收到了响应,但它看起来像垃圾:

{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "A4EEE117-0E46-46B3-9BA9-519F6B27357B", "name": "Unknown", "offset": 0, "size": 0, "type": "text/html"}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "A4EEE117-0E46-46B3-9BA9-519F6B27357B", "name": "Unknown", "offset": 0, "size": 0, "type": "text/html"}}, "bodyUsed": false, "headers": {"map": {"connection": "close", "content-type": "text/html; charset=UTF-8", "date": "Sat, 04 Feb 2023 02:07:35 GMT", "host": "localhost:8000", "x-powered-by": "PHP/8.2.1"}}, "ok": true, "status": 200, "statusText": "", "type": "default", "url": "http://localhost:8000/"}

我用React Native发送请求:

9 function getThumbnail(url){                                                    
  8   fetch('http://localhost:8000/', {                                            
  7   method: 'POST',                                                              
  6   headers: {                                                                   
  5     'Accept': 'application/json',                                              
  4     'Content-Type': 'application/json',                                        
  3     },                                                                         
  2     body: JSON.stringify({                                                     
  1       data: url                                                                
  0     })                                                                         
  1   })                                                                           
  2                                       
  3   .then((response) => {                                                        
  4     console.log(response);                                                     
  5   })                                                                           
  6   .catch((error) => {                                                          
  7     console.error("got an error");                                             
  8     console.error(error);                                                      
  9   });                                                                          
 10 }

我不知道发生了什么事,因为服务器收到请求,但似乎没有从它那里得到任何数据,立即关闭连接,并发送回一个看起来像垃圾的响应。也没有做太多的PHP之前,所以很抱歉,如果我错过了一些超级基本的东西。
我尝试更改服务器以打印带有print(implode($_GET))的GET请求的内容,并在浏览器中导航到localhost:8000,但$_GET的内容也是空的:

Received GET request:
roejwanj

roejwanj1#

看起来您正在发送原始JSON数据
试试这个

$body = json_decode(file_get_contents('php://input'), true);
print_r($body );

相关问题