PHP fsockopen stopped working since 20/05/2022

78 Views Asked by At

In our stack we need websites to be able to fire and forget a request to central server. to do that we use a fsockopen. The code below has not been changed for years.

private function curl_post_async($url, $params = array()){
    $post_params = array();

    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']:80,
        $errno, $errstr, 30);

    $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);

As you can see we are not using the while loop with fgets as we dont care about the response. We just need that data sent to central server. We have not made any changes on any website or server in our network but as of Friday the 20th of May that function stopped working. We can get it to work by adding the while loop with fgets but that does not fit our requirements.

We have sites sitting on range of server and central server on Azure.

Interestingly enough the server it writes to has other apps on it. They also use this method and it works still.

Anyone got any input on this or seeing similar problem ?

1

There are 1 best solutions below

2
Sairam Tadepalli On

The PHP function fsockopen() may not work because of firewall restrictions on outgoing connections.

In such cases, the port required for the outgoing connection should be opened. You can request this by opening a ticket through your

Client Area > Help Menu > View Help Center > Contact Us > Other > Report Other Technical Issues.