0

I have a background script running via socket as follows

   if (strstr($_SERVER['PHP_SELF'], "/")) { 
        $location = array(); 
        $location = explode("/", $_SERVER['PHP_SELF']); 
        $folder = $location[count($location) - 2]; 
    } 
    else { 
        $folder = $_SERVER['PHP_SELF']; 
    }

    //script, runs in background
    $host = HOST;
    $remote_house = 'https://'.$host.'/'.$folder.'/controllers/background';

    $socketcon = fsockopen($host, 80);

    if($socketcon) {   
        $socketdata = "GET $remote_house/".$scriptName." HTTPS 1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n"; 
        fwrite($socketcon, $socketdata); 
        fclose($socketcon);
    }

This is working properly on localhost and online with HTTP but it fails to work on HTTPS with this error.

HTTP/1.0 400 Bad request Cache-Control: no-cache Connection: close Content-Type: text/html 400 Bad request Your browser sent an invalid request.

Any idea?

1 Answer 1

1
$socketdata = "GET $remote_house/".$scriptName." HTTPS 1.1\r\n...

This is both an invalid HTTP and an invalid HTTPS request. First, it should be HTTP/1.1 not HTTPS 1.1. And for HTTPS you need to first create a SSL/TLS connection to the peer and then you can send your HTTP request over this connection.

$remote_house = 'https://'.$host.'/'.$folder.'/controllers/background';

Moreover, the path component in the request should not contain the full URL but only the path, i.e. '/'.$folder.'/controllers/background'.

$socketcon = fsockopen($host, 80);

And for HTTPS you need to connect to port 443 not 80 and then do a SSL/TLS handshake there before sending the HTTP request.

Sign up to request clarification or add additional context in comments.

2 Comments

I see. How would I go about doing the handshake?
@user5349142: see the documentation to fsockopen which describes how to create a TLS socket. In short: fsockopen('tls://'.$host,...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.