Guzzle http doesn't send json a request to server



  • Guzzlehttp 7.4

    Recovery:

    composer init composer require guzzlehttp/guzzle touch test.php

    test.php

    <?php    require 'vendor/autoload.php';
    use GuzzleHttp\Client;
    

    $client = new Client();

    $response = $client->post('http://localhost:8000/dump.php', [
    GuzzleHttp\RequestOptions::JSON => ['foo' => 'bar'] // or 'json' => [...]
    ]);

    touch dump.php

    dump.php

    <?php
    ob_start();
    var_dump($_GET,$_POST);
    $output = ob_get_clean();
    file_put_contents('log.txt', $output,FILE_APPEND);

    php -S localhost:8000

    php test.php

    Output expected (sighs) https://docs.guzzlephp.org/en/stable/request-options.html#json 😞

    Receive a log.txt json request by POST

    Output delivered:

    1. The request comes and the type of request is correct.

      [Sat Nov 13 19:08:33 2021] PHP 7.4.3 Development Server (http://localhost:8000) started
      [Sat Nov 13 19:09:16 2021] 127.0.0.1:51972 Accepted
      [Sat Nov 13 19:09:16 2021] 127.0.0.1:51972 [200]: POST /dump.php
      [Sat Nov 13 19:09:16 2021] 127.0.0.1:51972 Closing

    But cat log.txt

    array(0) {
    }
    array(0) {
    }

    Bullshit, I couldn't find a solution on the websites, all of the listed ones don't send json on the phone. Can you tell me what I'm doing wrong?



  • I think it's partly because

    https://www.php.net/manual/ru/features.commandline.webserver.php to assist in the development of annexes. It may also be useful for test purposes or for demonstrating an annex launched in a fully controlled environment. It does not serve as a full web server and should not be used in public networks.

    You can use it. https://www.php.net/manual/ru/wrappers.php.php to obtain the contents of the request in a built-in (and other) server.

    https://www.php.net/manual/ru/reserved.variables.post.php#125651 Says $_POST is used for this form, it cannot be transmitted to JSON/ https://www.php.net/manual/ru/reserved.variables.post.php#114598 β™ͺ I confirmed it by transmitting the data through the form_params in Guzzle as a mass-- the data appeared at $_POST. Use php://input to receive JSON.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2