4

我想让一台服务器向另一台服务器发出 http post 请求。我可以通过基本的 php 安装(在我的情况下为 Dreamhost)来做到这一点吗?如果是这样,请指出我正确的方向!谢谢,-摩根

4

2 回答 2

6

如果我是你,我会使用 Curl 。

编辑:看起来它在 DH 上受支持

于 2009-01-19T15:08:03.857 回答
0

http://blog.brezovsky.net/en-text-3.html

    function httpSocketConnection($host, $method, $path, $data)
    {
        global $Db;


        $method = strtoupper($method);        

        if ($method == "GET")
        {
            $path.= '?'.$data;
        }    

        $filePointer = fsockopen($host, 80, $errorNumber, $errorString);

        if (!$filePointer) 
        {
            throw new Exception("Chyba spojeni $errorNumber $errorString");
        }

        $requestHeader = $method." ".$path."  HTTP/1.1\r\n";
        $requestHeader.= "Host: ".$host."\r\n";
        $requestHeader.= "User-Agent:      Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n";
        $requestHeader.= "Content-Type: application/x-www-form-urlencoded\r\n";

        if ($method == "POST")
        {
            $requestHeader.= "Content-Length: ".strlen($data)."\r\n";
        }

        $requestHeader.= "Connection: close\r\n\r\n";

        if ($method == "POST")
        {
            $requestHeader.= $data;
        }            

        fwrite($filePointer, $requestHeader);

        $responseHeader = '';
        $responseContent = '';

        do 
        {
            $responseHeader.= fread($filePointer, 1); 
        }
        while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader));


        if (!strstr($responseHeader, "Transfer-Encoding: chunked"))
        {
            while (!feof($filePointer))
            {
                $responseContent.= fgets($filePointer, 128);
            }
        }
        else 
        {

            while ($chunk_length = hexdec(fgets($filePointer))) 
            {
                $responseContentChunk = '';


                $read_length = 0;

                while ($read_length < $chunk_length) 
                {
                    $responseContentChunk .= fread($filePointer, $chunk_length - $read_length);
                    $read_length = strlen($responseContentChunk);
                }

                $responseContent.= $responseContentChunk;

                fgets($filePointer);

            }

        }




        return chop($responseContent);

    }
于 2009-01-19T15:09:00.703 回答