可以使用php的socket函数,向另外一个服务器提交http请求

解决方案 »

  1.   

    有详细点的例子没有?关于php中socket的?
      

  2.   

    /*
    ************************************************************************
    * ?Sloppycode.net All rights reserved.
    *
    * This is a standard copyright header for all source code appearing
    * at sloppycode.net. This application/class/script may be redistributed,
    * as long as the above copyright remains intact. 
    * Comments to [email protected]
    ************************************************************************
    *//**
     * @title Html Source class - api for getting/posting to websites
     * @author C.Small
     * @version 1.0
     *
     * - HTTP 1.0 specs - http://www.w3.org/Protocols/rfc1945/rfc1945]
     * - Easier to read version at http://www.ics.uci.edu/pub/ietf/http/rfc1945.html
     */
    Class HtmlSource
    {
    /* All vars are public */

    // Common
    var $host;
    var $port = 80;
    var $page;
    var $request;
    var $httpversion;
    var $method = "GET";
    var $timeout = 30;

    var $striptags;
    var $showsource;
    var $strip_responseheader = true;

    // Cookie
    var $cookies = array();

    // GET
    var $getvars = array();

    // POST
    var $postvars = array();

    // Request fields
    var $accept;         //format: Accept: */*
    var $accept_encoding;//format: gzip,deflate 
    var $accept_language;//format: en-gb 
    var $authorization;  //format: username:password
    var $content_length; //format: 40 (for POST)
    var $content_type;   //format: application/x-www-form-urlencoded
    var $date;           //format: Date: Tue, 15 Nov 1994 08:12:31 GMT
    var $referer;        //format: Referer: http://www.domain.com
    var $useragent;      //format: User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

    function addPostVar($name,$value)
    {
    if (!empty($name) && !empty($value))
    {
    $this->postvars[] =$name."=".$value;
    }
    }
    function addGetVar($name,$value)
    {
    if (!empty($name) && !empty($value))
    {
    $this->getvars[] = $name."=".$value;
    }
    }
    function addCookie($name,$value)
    {
    if (!empty($name) && !empty($value))
    {
    $this->cookies[] = $name."=".$value;
    }
    }
    function getSource()
    {
    // Error check
    if (empty($this->httpversion))
    {
    $this->httpversion = "1.0";
    }
    if (empty($this->method))
    {
    $this->method = "GET";
    }

    // Make GET variables
    $vars   = "";
    $cookiehead = "";
    if (sizeof($this->getvars) >0 && $this->method == "GET")
    {
    $vars  = "?";
    $vars .= join($this->getvars,"&");
    // Knock last '&' off
    // Remove this..?
    if (sizeof($this->getvars) >1)
    {
    $vars = substr($vars,0,strlen($vars) -1);
    }
    }
    // Make POST variables
    if (sizeof($this->postvars) >0 && $this->method == "POST")
    {
    $vars  = "\r\n";
    $strpostvar = join($this->postvars,"&");
    $vars .= $strpostvar;
    $vars .= "\r\n";
    }

    // Make Cookies
    if (sizeof($this->cookies) >0)
    {
    $cookiehead  = "Cookie: ";
    $cookiehead .= join($this->cookies,"; ");
    $cookiehead .= "\r\n";
    }

    // Make up request. Host isn't strictly needed except IIS winges
    if ($this->method == "POST")
    {
    $this->content_length = strlen($strpostvar);
    $this->content_type = "application/x-www-form-urlencoded";

    $this->request  = $this->method." ".$this->page." HTTP/".$this->httpversion."\r\n";
    $this->request .= "Host: ".$this->host."\r\n";
    $this->request .= $cookiehead;
    $this->request .= $this->privateMakeRequest();
    $this->request .= $vars."\r\n";
    } else{
    $this->request  = $this->method." ".$this->page.$vars." HTTP/".$this->httpversion."\r\n";
    $this->request .= "Host: ".$this->host."\r\n";
    $this->request .= $cookiehead;
    $this->request .= $this->privateMakeRequest();
    $this->request .= "\r\n";
    } // Open socket to URL

    $sHnd = fsockopen ($this->host, $this->port, $errno, $errstr, $this->timeout);
    fputs ($sHnd, $this->request);

    // Get source
    while (!feof($sHnd))
    {
    $result .= fgets($sHnd,128);
    }

    // Strip header
    if ($this->strip_responseheader)
    {
    $result = $this->privateStripResponseHeader($result);
    }

    // Strip tags
    if ($this->striptags)
    {
    $result = strip_tags($result);
    }
    // Show the source only
    if ($this->showsource && !$this->striptags)
    {
    $result = htmlentities($result);
    $result = nl2br($result);
    }
    return $result;
    }

    // Make up headers
    function privateMakeRequest()
    {
    if (!empty($this->accept))
    {
    $result .= "Accept: ".$this->accept."\r\n";
    }
    if (!empty($this->accept_encoding))
    {
    $result .= "Accept-Encoding: ".$this->accept_encoding."\r\n";
    }
    if (!empty($this->accept_language))
    {
    $result .= "Accept-Language: ".$this->accept_language."\r\n";
    }
    if (!empty($this->authorization))
    {
    $result .= "Authorization: Basic ".base64_encode($this->authorization)."\r\n";
    }
    if (!empty($this->content_length))
    {
    $result .= "Content-length: ".$this->content_length."\r\n";
    }
    if (!empty($this->content_type))
    {
    $result .= "Content-type: ".$this->content_type."\r\n";
    }
    if (!empty($this->date))
    {
    $result .= "Date: ".$this->date."\r\n";
    }
    if (!empty($this->referer))
    {
    $result .= "Referer: ".$this->referer."\r\n";
    }
    if (!empty($this->useragent))
    {
    $result .= "User-Agent: ".$this->useragent."\r\n";
    }

    return $result;
    }
    function privateStripResponseHeader($source)
    {
    $headerend = strpos($source,"\r\n\r\n");
    if (is_bool($headerend))
    {
    $result = $source;
    } else{
    $result = substr($source,$headerend+4,strlen($source) - ($headerend+4));
    }
    return $result;
    }}