Jump to content

Asynchronous PHP calls


Funce

Recommended Posts

I'm running into a few issues regarding my web application written in PHP.

I'm wanting to post some data (already collected) without waiting for a response. I know that the page this posts to will take forever and a half to get through because it has to send multiple emails through a server that takes forever to do one. So I definitely do not want to block this page whilst the script is running.

 I looked around and I found this function, but I'm having some issues with it.

<?php
function post_without_wait($url, $params)
{

  foreach ($params as $key => &$val) {
    if (is_array($val)) $val = implode(',', $val);
    $post_params[] = $key . '=' . urlencode($val);
  }
  $post_string = implode('&', $post_params);
  $parts = parse_url($url);
  $fp = fsockopen(
    $parts['host'],
    isset($parts['port']) ? $parts['port'] : 443,
    $errno,
    $errstr,
    30
  );
  if (!$fp) {
    throw new Exception($errno . ' - ' . $errstr);
  }

  $out = "POST " . $parts['path'] . " HTTP/1.1\r\n";
  $out .= "Host: " . $parts['host'] . "\r\n";
  $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out .= "Content-Length: " . strlen($post_string) . "\r\n";
  $out .= "Connection: Close\r\n\r\n";
  if (isset($post_string)) $out .= $post_string;
  fwrite($fp, $out);

  fclose($fp);
}
?>

Whenever I use this, using my URL and the whole $_POST array,  I get

HTTP/1.1 400 Bad Request Date: Fri, 31 Aug 2018 01:04:56 GMT Server: Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.5 Vary: accept-language,accept-charset Accept-Ranges: bytes Connection: close Content-Type: text/html; charset=utf-8 Content-Language: en Expires: Fri, 31 Aug 2018 01:04:56 GMT

 

I can confirm that the page it is posting to never even runs, as I've set up an exception that is not being caught by my debugging tools.

I'm using port 443 as it is my SSL port I'm running on. Port 80 gives me a 301 permanent redirect (I only want HTTPS, this is sensitive anyway).

 

Does anything look amiss with the creation of this request?

Edited by Funce
Link to comment
Share on other sites

That looks fine in general, I guess you could print all of the headers just to verify.  The problem might be SSL negotiation, I'm not sure that just opening a socket to 443 will result in the entire SSL/TLS handshake process automatically.  Also, I don't think that's going to run asynchronously.

You might want to fork a child process and then just use something like cURL, which will do all of the SSL/TLS negotiation.

Link to comment
Share on other sites

Yeap! You would be correct. I've now managed to get this to work. I'm instead using stream_socket_client in place of fsockopen. My local server doesn't have a proper certificate installed, so I've just currently made it ignore SSL verification.

 

Thank you for the insight.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...