hollodotme / fast-cgi-client

A PHP fast CGI client for sending requests (a)synchronously to PHP-FPM
Other
551 stars 34 forks source link

/path/to/script.php seems to run twice #65

Closed fabs2011 closed 3 years ago

fabs2011 commented 3 years ago

Hi there, I'm testing this library with the sample scripts in the readme file. Everything works as described except when I try to echo the response, in which case the script seems to run twice. If I don't output the response it's fine but then I'd not have a way to check in my code whether the script was run successfully or not. It makes no difference if I use sendRequest or sendAsyncRequest.

With the $reponse->getBody(), in my browser I can see 3 arrays, which is correct image But in the database I have twice as much rows image

This is the constructor of my class: `function __construct() {

        $this->connection = new NetworkSocket(
            '127.0.0.1',    # Hostname
            9000,           # Port
            5000,           # Connect timeout in milliseconds (default: 5000)
            5000            # Read/write timeout in milliseconds (default: 5000)
        );

        $this->client = new Client();

} This is the function that calls the script: function process(array $payload) {

        $payload['db']['dsn']=\Base::instance()->get('dbdsn');
                    $payload['db']['uname']=\Base::instance()->get('dbusername');
                    $payload['db']['pwd']=\Base::instance()->get('dbpassword');
                $content = http_build_query($payload);
        $request = new PostRequest('scripts/async.php', $content);

        // $this->socketId[] = $this->client->sendAsyncRequest($this->connection,$request);
        $response = $this->client->sendRequest($this->connection,$request);
        **echo $response->getBody().'<br />';**

} And this is the script (async.php): <?php

try {
    $pdo = new \PDO($_REQUEST['db']['dsn'],$_REQUEST['db']['uname'],$_REQUEST['db'] 
                               ['pwd'],array(\PDO::ATTR_ERRMODE=>\PDO::ERRMODE_EXCEPTION));
    $sql = "INSERT INTO queue (task,payload) VALUES (?,?)";
    $stmt= $pdo->prepare($sql);
    unset($_REQUEST['db']);
    $stmt->execute(['async-'.date('Y-m-d h:i:s'), json_encode($_REQUEST)]);
} catch (\Exception $e) {
    die(json_encode($e->getMessage()));
}

echo json_encode($_REQUEST);`

If I remove the echo $response->getBody().'
';
from my function the number of rows added to the database is 3 (correct) instead of 6 (wrong). I'm using the latest version 3.1 with a php:8.0.8-fpm docker container, nginx latest and mysql latest.

Thank you for your work and hopefully your help with this. Fabio.

UPDATE I've ended up setting a header "Status" in my script async.php, and assign it to a property of my class, like this: $this->status=$response->getHeader('Status') I still have the correct number of records in my DB, so the issue seems to be only with echo ANYTHING (even echo $response->getHeader('Status') duplicates the number of calls to my script. I wonder what am I doing wrong....

fabs2011 commented 3 years ago

After further testing I'm closing this issue, it has nothing to do with the fast-cgi-client library.