Lex
Lex

Reputation: 355

PHP fsockopen client doesn't receive sent data

I have the following (stripped-down) piece of code:

function curl_request_async($url, $params)
{
    foreach ($params as $key => $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);
    fwrite($fp, "$type ".$parts['path']." HTTP/1.1\r\n");
    fwrite($fp, "Host: ".$parts['host']."\r\n");
    fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    fwrite($fp, "Content-Length: ".strlen($post_string)."\r\n");
    fwrite($fp, "Connection: Close\r\n\r\n");

    $bytes_written = fwrite($fp, $post_string);
    var_dump($bytes_written, strlen($post_string));
    // fread($fp, 1);
    // fflush($fp);
    fclose($fp);
}

The problem with this code is that I found no evidence the request reached the server called. The line var_dump($bytes_written, strlen($post_string)); outputted int(493) int(493), so it should have received all data, yet it didn't.

If I uncomment fread($fp, 1); it works without a problem. That could be working solution, but it doesn't seem to make sense. There has to be a better way!

My question then is two-fold: why does fread($fp, 1); fix my problem and is there a better solution?

Upvotes: 0

Views: 876

Answers (2)

hanshenrik
hanshenrik

Reputation: 21665

your problem is probably that you wrote the server code in PHP, and you dont have ignore_user_abort=true by default (see http://php.net/manual/en/misc.configuration.php#ini.ignore-user-abort ), so when you close the connection, your server stop executing your php code, thus fread(fp,1) fix your problem - connection dont close before php start writing a response

you can use this code to make a server to test if its actually connecting or not -

<?php 
error_reporting(E_ALL);
ini_set('display_errors',1);
$sck=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($sck===FALSE){
    die('socket_create failed!');
}
if(!socket_set_block($sck)){
    die("socket_set_block failed!");
}
if(!socket_bind($sck, '0.0.0.0',1337)){
    die("FAILED to bind to port 1337");
}
if(!socket_listen($sck,0)){
    die("socket_listen failed!");
}
$fullFile='';
while((print('listening for connections!'.PHP_EOL)) && false!==($conn=socket_accept($sck))){
    echo "new connection!".PHP_EOL;
    echo "generating crypto iv..";

    while(false!==($buffi=socket_recv($conn,$buff,1024,MSG_WAITALL))){
        if($buffi===0){
            break;//socket_recv's way of
            //saying that the connection closed,
            //apparently. the docs say it should return
            // false, but it doesn't, it just infinitely returns int(0).
            // at least on windows 7 x64 sp1.
        }
        $fullFile.=$buff;
        echo "recieved ".strlen($fullFile)." bytes...".PHP_EOL;
        $buff='';//do i need to clear it? or wiill recv do it for me?
    }
    echo "all bytes recieved (i guess, todo, socket_last_error confirm).";
    echo PHP_EOL;var_dump($fullFule);
    echo "done!".PHP_EOL;
}

die("should never reach this code...");

it will make a netcat-style server on http://127.0.0.1:1337

Upvotes: 1

Fin
Fin

Reputation: 386

fread needs two parameters: a resource and a length number of bytes to read. Right now you are only reading 1 byte. fread($fp, 1);

If you want to read the complete result, loop it until readed completely:

while(!feof($fp)){
    echo fread($fp, 128);
}

Upvotes: 0

Related Questions