Perché fsockopen hanno problemi di prestazioni e non fopen quando si invia una richiesta POST in PHP?

StackOverflow https://stackoverflow.com/questions/1930074

Domanda

Ho provato due diverse implementazioni per simulare la pubblicazione di un modulo. Uno usa fsockopen (ad esempio qui: http://www.faqts.com/knowledge_base /view.phtml/aid/7962 ) e l'altro utilizza fopen (ad esempio qui: http://netevil.org/blog/2006/nov/http-post-from-php-without-curl ).

Mi sono imbattuto in alcuni gravi problemi di prestazioni con fsockopen - quando faccio un passo attraverso di essa con un debugger, tutto sembra funzionare bene, ma quando non connettere il debugger pagina richiede un tempo luuungo per caricare (probabilmente più di 10 secondi). fopen funziona perfettamente (più io non devo analizzare le intestazioni di risposta). Qualcuno sa perché fsockopen avrebbe questi problemi di prestazioni? Ha a che fare con l'impostazione di timeout?

Ho incluso il mio codice qui sotto.


//fsockopen implementation
/**
 * The class that posts form data to a specified URL.
 * @package Core
 * @subpackage Classes
 */
class SU_form_poster_class
{
    /**
     * The file handle which is created when a form is posted.
     * @var resource
     */
    protected $file_handle;

    protected $port;

    protected $timeout;


    /**
     * Creates a new instance of this class.
     * @param int $timeout the timeout (in seconds) to wait for the request
     * @param int $port the port to make the request on
     */
    public function __construct($timeout = 30, $port = 80)
    {
        $this->timeout = $timeout;
        $this->port = $port;
    }

    /**
     * Sends a POST request to a specified page on a specified host with the
     * specified data.
     * @param string $path the part of the URL that specifies the page (including
     * the query string)
     * @param string $host the host part of the URL to post to
     * @param string $request_data the data to be posted in "query string" format,
     * e.g. name1=value1&name2=value2&name3=value3
     */
    public function do_post($path, $host, $request_data)
    {
        $err_num = 0;
        $err_str = '';
        $this->file_handle = fsockopen($host, $this->port, $err_num, $err_str, $this->timeout);
        if (!$this->file_handle)
        {
            throw new RuntimeException($err_str, $err_num);
        }
        else
        {
            $request = 'POST '.$path." HTTP/1.1\r\n".
                'Host: '.$host."\r\n".
                "Content-type: application/x-www-form-urlencoded\r\n".
                'Content-length: '.strlen($request_data)."\r\n\r\n".
                $request_data;

            fputs($this->file_handle, $request);
        }
    }

    /**
     * Retrieves data from the most recent request.
     * @return string the response
     */
    public function get_last_response()
    {
        if (!$this->file_handle)
        {
            throw new RuntimeException('A valid request must be made first.');
        }
        else
        {
            $response = '';
            $linenum = 0;
            while (!feof($this->file_handle))
            {
                $line = fgets($this->file_handle, 1024);
                if ($linenum > 6)
                {
                    $response .= $line;
                }
                ++$linenum;
            }
            fclose($this->file_handle);
            return $response;
        }
    }
}


/**
 * The class that posts form data to a specified URL.
 * @package Core
 * @subpackage Classes
 */
class SU_form_poster_class
{
    /**
     * The file handle which is created when a form is posted.
     * @var resource
     */
    protected $stream;

    /**
     * Sends a POST request to a specified page on a specified host with the
     * specified data.
     * @param string $url
     * @param string $request_data the data to be posted in "query string" format,
     * e.g. name1=value1&name2=value2&name3=value3
     */
    public function do_post($url, $request_data)
    {
        $params = array(
            'http' => array(
                'method' => 'POST', 'content' => $request_data
            )
        );

        $context = stream_context_create($params);
        $this->stream = fopen($url, 'rb', false, $context);
        if (!$this->stream)
        {
            throw new RuntimeException('Stream was not created correctly');
        }
    }

    /**
     * Retrieves data from the most recent request.
     * @return string the response
     */
    public function get_last_response()
    {
        if (!$this->stream)
        {
            throw new RuntimeException('A valid request must be made first.');
        }
        else
        {
            return stream_get_contents($this->stream);
        }
    }
}

È stato utile?

Soluzione

HTTP 1.1 connessioni di default persistente. Questo significa che la connessione non ottiene automaticamente chiuso e feof() non restituisce vero fino a quando i tempi di richiesta fuori. Per aggirare il problema inviare l'intestazione Connection: close o utilizzo HTTP 1.0 che non supporta le connessioni persistenti.

Altri suggerimenti

  

Qualcuno sa perché fsockopen sarebbe   avere questi problemi di prestazioni? fa   ha a che fare con il timeout   impostazione?

Ho avuto problemi in passato con fsockopen su SSL -. Ma non è questo ciò che il vostro richiesto

Hai provato a usare la curl_ * famiglia di funzioni, invece, che potrebbe aiutare?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top