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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

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

Post 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
Attachments
untitled.JPG
untitled.JPG (138.33 KiB) Viewed 688 times
Last edited by hannnndy on Tue May 13, 2008 12:20 am, edited 1 time in total.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

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

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

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

Post 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 .
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

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

Post 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
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

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

Post 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.
User avatar
hannnndy
Forum Contributor
Posts: 131
Joined: Sat Jan 12, 2008 2:09 am
Location: Iran>Tehran
Contact:

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

Post 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
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

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

Post by Mordred »

*sigh*
I already told you that as well.
It's caused by magic :)
Post Reply