2009-01-19 43 views
4

我想讓一臺服務器向另一臺服務器發出http post請求。我可以用一個基本的PHP安裝(Dreamhost在我的情況下)嗎?如果是這樣,請指向正確的方向! 謝謝, -Morganphp服務器到服務器的帖子?

回答

6

我會use Curl,如果我是你。編號:it's supported on DH

+0

感謝奧利。我已經這樣傾向了。我會做的。 – morgancodes 2009-01-19 15:16:16

+0

我也推薦使用Curl,特別是你需要傳遞很多參數等。同樣值得一提的是,大多數PHP的文件函數都會很高興地打開URL以及本地文件。 – 2009-01-19 15:44:31

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); 

    }