Re: Errors with urlencode and gzcompress

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I found a solution and figured I would share with the everyone.

I was able to switch my request method over to POST (instead of GET). I
am now doing testing to make sure that this will meet my needs, but so
far it seems to work well.

This function accepts the data to post and then returns the resulting
page. the response page does not have to be html, but I have only tested
with text. I don't know how data would work. Data to be posted should be
in name=value&name1=value1 format.

//###################################################################
// post_and_retrieve function
//###################################################################
function post_and_retrieve($host, $path, $data_to_send) {
        $errno = 0;
        $errstr = '';
        $conn_timeout = 30;
        $headers = '';
        $content = '';
        $flag = false;
        $lc = 0;
        $content_len = 0;
        
        $fp = fsockopen($host,80, $errno, $errstr, $conn_timeout);
        if (!$fp || $errno != 0 || $errstr != ''){
                return "FAIL errno: $errno, errstr: $errstr";
        }
        fputs($fp, "POST $path HTTP/1.1\n" );
        fputs($fp, "Host: $host\n" );
        fputs($fp, "Content-type: application/x-www-form-urlencoded
\n" );
        fputs($fp, "Content-length: ".strlen($data_to_send)."\n" );
        fputs($fp, "Connection: close\n\n" );
        fputs($fp, $data_to_send);
        // remote script will now process the post and return a result
        while (!feof($fp)) {
                $res = fgets($fp,8192); //read 8k/line from remote host
                if ($flag) {
                        if ($lc == 0) {
                                // first line contains the content
length in hex
                                $content_len = hexdec(trim ($res));
                        } else {
                                $content .= $res;
                        }
                        $lc++;
                } else {
                        $headers .= $res;
                        if (strlen(trim($res)) == 0) {
                        $flag = true;
                        }
                }

        }
        // trim our content to content_len
        $content = substr ($content, 0, $content_len);
        if ($headers === FALSE || strlen ($headers) < 1) {
                return "FAIL could not read response content: $content";
        }
        fclose($fp);
        //$return = array('headers' => $headers, 'content' => $content);
        $return = $content;
        return $return;
}

Thanks,
James



On Tue, 2007-12-04 at 14:14 -0500, James Crow wrote:
> Hello all,
> 
>   I am having some trouble figuring out how to pass data to a remote PHP
> script. 
> 
> Here is the scenario: 
> I have several Windows boxes running Apache 2 and PHP 4. PHP5 is not an
> option because of some Zend Encoded scripts that we purchased. I need to
> pass data from one script to a remote script. The local script has an
> array that is serialized. Then the serialized string is then double
> urlencoded. A single urlencode does not work correctly on PHP4/Win32.
> The serialized and urlencoded string is then passed in the request to a
> remote script: http://192.168.1.2?data=<serialized string>
> This process works fine for small amounts of data. If the URL is below a
> certain limit (maybe 1024 characters, have not yet found the exact size
> limit) this works fine. 
> 
> I now have a need to pass more data to the remote script. I have tried
> to do
> $temp = array ('lots of data...');
> $temp1 = serialize($temp);
> $temp2 = gzcompress($temp1); //compress our string first to save space
> $temp3 = urlencode($temp2);
> $data = urlencode($temp3);
> http://192.168.1.1/?data=$data
> 
> Sometimes this works and sometimes not. I have not yet been able to
> figure out why this sometimes fails.
> 
> Can anyone tell me why this works sometimes and others not? Or can
> someone suggest a better way of passing data to a remote script.
> 
> Thanks,
> James
> 

-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux