Page 1 of 1

[Solved] Socket Problem (Http Request - Unknown Chars ... )

Posted: Sun May 11, 2008 6:11 am
by hannnndy
:banghead:
I'm having a problem using php socket programming, the thing is somwhere (don't exactly know) random numbers are generated (well not realy random but i don't know what they mean) take a look at the site to see the damn numbers naniax

can anyone help me?

Code: Select all

 
<?php
class HttpRequest
{
    var $sHostAdd;
    var $sUri;
    var $iPort;
    
    var $sRequestHeader;
    
    var $sResponse;
    
    function HttpRequest($sUrl)
    {
        $sPatternUrlPart = '/http:\/\/([a-z-\.0-9]+)(:(\d+)){0,1}(.*)/i';
        $arMatchUrlPart = array();
        preg_match($sPatternUrlPart, $sUrl, $arMatchUrlPart);
        
        $this->sHostAdd = gethostbyname($arMatchUrlPart[1]);
        
        if (empty($arMatchUrlPart[4]))
        {
            $this->sUri = '/';
        }
        else
        {
            $this->sUri = $arMatchUrlPart[4];
        }
        if (empty($arMatchUrlPart[3]))
        {
            $this->iPort = 80;
        }
        else
        {
            $this->iPort = $arMatchUrlPart[3];
        }
        $this->addRequestHeader('Host: '.$arMatchUrlPart[1]);
        $this->addRequestHeader('Connection: Close');
    }
    
    function addRequestHeader($sHeader)
    {
        $this->sRequestHeader .= trim($sHeader)."\r\n";
    }
    
    function sendRequest($sMethod = 'GET', $sPostData = '')
    {
        $sRequest = $sMethod." ".$this->sUri." HTTP/1.1\r\n";
        $sRequest .= $this->sRequestHeader;
        if ($sMethod == 'POST')
        {
            $sRequest .= "Content-Type: application/x-www-form-urlencoded\r\n";
            $sRequest .= "Content-Length: ".strlen($sPostData)."\r\n";
            $sRequest .= "\r\n";
            $sRequest .= $sPostData."\r\n";
        }
        $sRequest .= "\r\n";
        
        $sockHttp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if (!$sockHttp)
        {
            die('socket_create() failed!');
        }
        
        $resSockHttp = socket_connect($sockHttp, $this->sHostAdd, $this->iPort);
        if (!$resSockHttp)
        {
            die('socket_connect() failed!');
        }
        //print($sRequest."<Br />");
        socket_write($sockHttp, $sRequest, strlen($sRequest));
        
        $this->sResponse = '';
        while ($sRead = socket_read($sockHttp, 4096))
        {
            $this->sResponse .= $sRead;
        }
        
        socket_close($sockHttp);
    }
    
    function getResponse()
    {
        return $this->sResponse;
    }
    
    function getResponseBody()
    {
        $sPatternSeperate = '/\r\n\r\n/';
        $arMatchResponsePart = preg_split($sPatternSeperate, $this->sResponse, 2);
        return $arMatchResponsePart[1];
    }
}
?>
 
it seems that this code removes some data too

:bow: Please leave me as soon as possible the soloution unfortunately our costumers are at the door

Re: Socket Problem (Http Request - Unknown Chars ... )

Posted: Sun May 11, 2008 6:15 pm
by Jade
It looks like some information being sent to the socket isn't being converted correctly or can't be displayed. I'd check the information you're sending through and see if you can't find the problem there.

Re: Socket Problem (Http Request - Unknown Chars ... )

Posted: Sun May 11, 2008 10:57 pm
by Mordred
You are receiving a multipart message, which you need to put back together correctly. The "random" numbers are the sizes of each chunk. While the best solution is to put the chunks together correctly, the fastest solution would be to switch to HTTP/1.0 .

Re: Socket Problem (Http Request - Unknown Chars ... )

Posted: Mon May 12, 2008 2:37 am
by hannnndy
hi and thanks for your reply

:cry: let me explain more about this problem

we are calling each module using the left socet http class and the return values are as you can see a string in our testing programs windows (os) clients we do not have any sort of problem it has been parsed correctely but on the linux server in the internet we do have such chars . :(

is there any diffrence?

and one thing more we have tried to delete some chars from the begining and the end of the strings that returned using string trim funcs but it does not work because we do not know how much chars shoul be deleted

Re: Socket Problem (Http Request - Unknown Chars ... )

Posted: Mon May 12, 2008 5:14 am
by Mordred
It would help greatly if you use at least basic punctuation, I can't parse any meaning of what you say in that long sentence.

Anyway, I already told you what to do - change HTTP/1.1 to HTTP/1.0 in the request.

Re:[SOLVED] Socket Problem (Http Request - Unknown Chars ...

Posted: Mon May 12, 2008 5:50 am
by hannnndy
:lol: 8O :lol: thank you for your reply :wink: :roll: :drunk: it has solved now the problem was recognized correctly
but we are still confuzed :crazy:

Great Thanks

Re: Socket Problem (Http Request - Unknown Chars ... )

Posted: Mon May 12, 2008 6:59 am
by Mordred
*sigh*
I already told you that as well.
It's caused by magic :)