1

Currently, I am using exec / wget to callback a URL without it blocking the PHP script:

$newURL = /* URL we need to callback */
$newURL = escapeshellarg($newURL);
exec("curl -k $newURL > /dev/null &");

I would like to get the response of the callback URL and test to see if it responds with exactly a specified string. However, it must not block the rest of the script from executing.

How can I achieve this?

apscience
  • 7,033
  • 11
  • 55
  • 89

1 Answers1

0

This post has a solution for sending aysnc requests by using forked curl requests:

private function request($url, $payload) {

  $cmd = "curl -X POST -H 'Content-Type: application/json'";
  $cmd.= " -d '" . $payload . "' " . "'" . $url . "'";

  if (!$this->debug()) {
    $cmd .= " > /dev/null 2>&1 &";
  }

  exec($cmd, $output, $exit);
  return $exit == 0;
}

It also has a solution where you write to a socket before immediately closing it (before you get a response):

private function request($body) {

    $protocol = "ssl";
    $host = "api.segment.io";
    $port = 443;
    $path = "/v1/" . $body;
    $timeout = $this->options['timeout'];

    try {
      # Open our socket to the API Server.
      $socket = fsockopen($protocol . "://" . $host, $port,
                          $errno, $errstr, $timeout);

      # Create the request body, and make the request.
      $req = $this->create_body($host, $path, $content);
      fwrite($socket, $req);
      # ...
    } catch (Exception $e) {
      # ...
    }
}
Wayne Whitty
  • 19,513
  • 7
  • 44
  • 66