屌丝就是null 2015-11-10 02:59 采纳率: 0%
浏览 829
已结题

php 用fsockopen来GET网页 感觉死循环了 最后30秒超时 感觉是fread的问题 求大神解答

<?php
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');

?>

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 MATLAB动图问题
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名