Code Monkey home page Code Monkey logo

Comments (2)

esoodev avatar esoodev commented on June 2, 2024

I looked into the code more - I think it's worth mentioning that I wrapped the request to return a Promise on "end."

/**
 * AsyncHttpClient.php
 */
public function __construct()
{
    $this->event_loop = EventLoop::create();
    $this->client = new Client($this->event_loop);
}

public function post($options, $data = [])
{
    $resolver = Closure::bind(function ($resolve, $reject) use (&$options, &$data) {

        $res = '';

        $url = $this->_get_url_from_options($options);
        $headers = $this->_get_headers_from_options($options);

        /**
         * multipart/form-data processing
         */
        if (strcasecmp($headers['Content-Type'], 'multipart/form-data') === 0) {
            $boundary = 'gc0p4Jq0M2Yt08j34c0p';
            $headers['Content-Type'] .= "; boundary=${boundary}";
            $data = $this->_arr_to_multipart($data, $boundary);
        }

        /**
         * x-www-form-urlencoded processing
         */
        if (strcasecmp($headers['Content-Type'], 'x-www-form-urlencoded') === 0) {
            $data = http_build_query($data);
        }

        /**
         * application/json processing
         */
        if (strcasecmp($headers['Content-Type'], 'application/json') === 0) {
            $data = json_encode($data);
        }

        $request = $this->client->request('POST', $url, $headers);

        /**
         *  $response->on('data', function ($chunk) {});
         *  $response->on('end', function () {});
         */
        $request->on('response', function ($response) use (&$res, &$resolve) {
            $response->on('data', function ($chunk) use (&$res) {
                $res = $res . $chunk;
            });
            $response->on('end', function () use (&$res, &$resolve) {
                $resolve($res);
            });
        });

        /**
         *  \Exception $e passed into $fn_on_err($e)
         */
        $request->on('error', function (Exception $e) use (&$reject) {
            $reject($e);});

        $request->end($data);
    }, $this);

    $canceller = function () {
        throw new Exception('Async GET request cancelled');
    };

    return new Promise($resolver, $canceller);
}

Here is the above snippet in action :

$this->AsyncHttpClient->post([
    'url' => 'http://some.url',
    'headers' => [
        'X-CUSTOM-HEADER' => 'VAL',
        'Authorization' => $token,
        'Content-Type' => 'multipart/form-data',
    ]],
    [
        'data_key' => 'data_value',
    ]
)->then(function ($res) {echo ($res);}, function (Exception $e) {
    echo ($e->getMessage());
});
$this->AsyncHttpClient->run();

I checked that the host receives no data.

from http-client.

omar391 avatar omar391 commented on June 2, 2024

@esoodev How did you solve this?

from http-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.