title: Http service meta:

  • name: description content: Easyswoole, how to get the client IP
  • name: keywords content: swoole|swoole extension|swoole framework|Easyswoole|Get client IP|cross domain processing

common problem

How to get $HTTP_RAW_POST_DATA

  1. $content = $this->request()->getBody()->__toString();
  2. $raw_array = json_decode($content, true);

How to get the client IP

For example, how to get the client IP in the controller

  1. //Real address
  2. $ip = ServerManager::getInstance()->getSwooleServer()->connection_info($this->request()->getSwooleRequest()->fd);
  3. var_dump($ip);
  4. //Header address, for example after nginx proxy
  5. $ip2 = $this->request()->getHeaders();
  6. var_dump($ip2);

HTTP status code is always 500

Since the swoole 1.10.x and 2.1.x versions, in the http server callback, if response->end() is not executed, all 500 status codes are returned.

How to setCookie

Set the cookie by calling the setCookie method of the response object.

  1. $this->response()->setCookie('name','value');

More operations can be seen [Response object] (response.md)

How to customize the App name

Just modify the namespace registration of composer.json.

  1. "autoload": {
  2. "psr-4": {
  3. "App\\": "Application/"
  4. }
  5. }