You are here

public function ProfileStatSender::sendData in Brainstorm profile 8

Makes http POST request to server.

Return value

numeric|null Returns response code from server or NULL if client isn't able to connect to the server.

Overrides ProfileStatSenderInterface::sendData

File

module/custom/profile_stat_sender/src/ProfileStatSender.php, line 42
This file contains ProfileStatSender service implementation.

Class

ProfileStatSender
Provides ProfileStatSender service implementation.

Namespace

Drupal\profile_stat_sender

Code

public function sendData() {
  $client = \Drupal::httpClient();
  $data = $this
    ->fetchData();
  $data = http_build_query($data);
  $options = [
    'headers' => [
      'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'body' => $data,
  ];
  $server_url = $this
    ->getServerUrl();
  try {
    $response = $client
      ->request('POST', $server_url, $options);
    return $response
      ->getStatusCode();
  } catch (\Exception $e) {
    return NULL;
  }
}