【问题标题】:Why does fsockopen have performance problems and not fopen when sending a POST request in PHP?为什么在 PHP 中发送 POST 请求时 fsockopen 有性能问题而不是 fopen?
【发布时间】:2010-12-28 03:43:05
【问题描述】:

我尝试了两种不同的实现来模拟 POST 表单。一种使用fsockopen(此处示例:http://www.faqts.com/knowledge_base/view.phtml/aid/7962),另一种使用fopen(此处示例:http://netevil.org/blog/2006/nov/http-post-from-php-without-curl)。

我在使用fsockopen 时遇到了一些严重的性能问题 - 当我使用调试器单步执行时,一切似乎都运行良好,但是当我不附加调试器时,页面需要很长时间才能加载(可能更多超过 10 秒)。 fopen 完美运行(另外我不必解析响应标头)。有谁知道为什么fsockopen 会有这些性能问题?和超时设置有关系吗?

我在下面包含了我的代码。

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

【问题讨论】:

    标签: php performance post fopen fsockopen


    【解决方案1】:

    HTTP 1.1 连接默认为持久连接。这意味着连接不会自动关闭,feof() 在请求超时之前不会返回 true。要解决此问题,请发送 Connection: close 标头或使用不支持持久连接的 HTTP 1.0。

    【讨论】:

      【解决方案2】:

      有谁知道为什么 fsockopen 会 有这些性能问题吗?做 它与超时有关 设置?

      我过去在使用 fsockopen over ssl 时遇到过问题 - 但这不是你要问的。

      您是否尝试过改用 curl_* 系列函数,这可能会有所帮助?

      【讨论】:

      • 我确实尝试过,但我认为我将部署到的服务器不会使用 curl 支持编译 PHP(这不是默认设置)。我在这里没有真正的问题,因为 fopen 正在为我工​​作,但我绝对很好奇为什么会发生这种情况。
      猜你喜欢
      • 1970-01-01
      • 2015-08-06
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-26
      • 2014-04-16
      相关资源
      最近更新 更多