檔案寫入-php 用fsockopen來GET網頁 感覺死迴圈了 最後30秒逾時 感覺是fread的問題 求大神解答

來源:互聯網
上載者:User
關鍵字 檔案寫入 php socket網路編程
檔案寫入phpsocket網路編程

class Http {

const CRLF = " ";

protected $fp = null;
protected $errno = -1;
protected $errstr = 'error';
protected $fenxi = array();
protected $method = '';
protected $out = '';
//我覺得不需要下面兩個
protected $lineone = array();
protected $linetwo = array();

public function __construct($url) {
$this->conn($url);
//$this->same($this->method);
}

public function __destruct() {
$this->close();
}

public function conn($url) {
$this->fenxi = parse_url($url);
if (empty($this->fenxi['port'])) {
$this->fenxi['port'] = 80;
}

$this->fp = fsockopen($this->fenxi['host'], $this->fenxi['port'], $this->errno, $this->errstr, 3);
if (!$this->fp) {
echo $this->errstr;
}

}

public function same($method) {
$this->lineone[0] = $method . ' ' . $this->fenxi['path'] . ' ' . 'HTTP/1.1';
$this->linetwo[0] = 'Host:' . ' ' . $this->fenxi['host'];
$arr = array_merge($this->lineone, $this->linetwo);
$string = implode(self::CRLF, $arr);

if ($method == 'GET') {
$this->get($string);
} elseif ($method == 'POST') {
$this->post($string);
} else {
echo "method error";
exit;
}
}

public function get($str) {
//如果還要寫別的東西 寫在下面拼接

//這裡直接處理$str
fwrite($this->fp, $str);

while (!feof($this->fp)) {
$this->out .= fread($this->fp, 1024);
echo "ok";exit;
}

$this->show();

}

public function post($str) {

}

public function close() {
fclose($this->fp);
}

public function show() {
echo $this->out;
}

}

$test = new Http('http://news.163.com/13/0613/09/9187CJ4C00014JB6.html');
$test->same('GET');

?>

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.