没事的时候自己封装了一个socket类

功能非常easy和curl功能是一样的

class socketClass{
private $host;
private $url;
private $error_no = 0;
public $error_str = '';
private $port = 80;
private $timeout = 5;
private $method;
private $param;
private $fp;
private $content = '';
private $header = array();
public function __construct($host, $port, $url, $param, $method='GET', $timeout=5) {
$this->host = $host;
$this->port = $port;
$this->url = $url;
$this->timeout = $timeout;
$this->method = $method;
if(is_array($param)){
$this->param = http_build_query($param);
}else{
$this->param = $param;
}
}
private function connect(){
$this->fp = fsockopen($this->host, $this->port, $this->error_no, $this->error_str, $this->timeout);
if(!$this->fp){
$this->error_str = 'socket connect failed';
return false;
}
return true;
}
private function send(){
$this->fp = '';
if(!$this->connect()){
$this->error_str = 'connect socket failed';
return false;
}
if($this->method == 'POST'){
$header = "POST $this->url HTTP/1.1\r\n";
}else{
if(!empty($this->param)){
$header = "GET $this->url?$this->param HTTP/1.1\r\n";
}else{
$header = "GET $this->url HTTP/1.1\r\n";
}
}
$header .= "Content-Length:" . strlen($this->param) . "\r\n";
$header .= "Host:$this->host:$this->port\r\n";
$header .= "Content-Type:application/x-www-form-urlencode\r\n";
$header .= "Connection:close\r\n\r\n";
if($this->method == 'POST'){
$header .= "$this->param\r\n\r\n";
}
if(!empty($this->header)){
foreach ($this->header as $key=>$val){
$tmep = urlencode($val);
 $header .= "$key:$temp\r\n";
}
}
fwrite($this->fp, $header);
while(!feof($this->fp)){
$this->content .= fread($this->fp, 1024);
}
fclose($this->fp);
return true;
}
public function getContent(){
if(!$this->send()){
return false;
}
$pos = strpos($this->content, "\r\n\r\n");
$this->content = substr($this->content, $pos);
return $this->content;
}
public function setHeader($key, $val){
$this->header[$key] = $val;
}
public function getErrorStr(){
return $this->error_str;
}
}
$host = 'localhost';
$port = 81;
$url = 'http://localhost/phpdemo/php/socket_accept.php';
$param = array('username'=>'beggar', 'passwd'=>'20060400');
$method = 'GET';
$timeout = 10;
$http = new socketClass($host, $port, $url, $param, $method, $timeout);
$http->setHeader('referer', $url);
$http->setHeader('Accept', '*/*');
$header = $http->getContent();
echo $header;

PHP socket类的更多相关文章

  1. ServerSocket与Socket类

    ServerSocket与Socket类 TCP套接字协议: TCP最主要的特征就是能够建立长时间的连接,而且能够保证数据安全的送达,但是速度比较慢.使用TCP进行连接的时候会有三次握手,之后才建立起 ...

  2. Socket类 以及 ServerSocket类 讲解

    Socket类 套接字是网络连接的端点,套接字使应用可以从网络中读取数据,可以向网络中写入数据.不同计算机上的两个应用程序可以通过连接发送或接收字节流,以此达到相互通信的目的. 为了从一个应用程序向另 ...

  3. socket系列之客户端socket——Socket类

    假设TCP套接字服务器端已经建立好并正在监听客户端的连接了,那么客户端就可以通过Socket类来发起连接.客户端发起一个连接请求后,就被动地在等待服务器的响应.这个类同样位于java.net包中,包含 ...

  4. Socket 类

    构造函数       名称 说明 Socket(AddressFamily, SocketType, ProtocolType) 新实例初始化 Socket 类使用指定的地址族. 套接字类型和协议. ...

  5. java网络编程ServerSocket类 和Socket类的常用构造方法及其方法

    Socket类Socket(InetAddress address, int port) 创建一个流套接字并将其连接到指定 IP 地址的指定端口号.Socket(String host, int po ...

  6. 详解 ServerSocket与Socket类

    (请观看本人博文 -- <详解 网络编程>) 目录 ServerSocket与Socket ServerSocket 类: Socket类: ServerSocket与Socket 首先, ...

  7. 【Android编程】Java利用Socket类编写Metasploit安卓载荷辅助模块

    /作者:Kali_MG1937 CSDN博客:ALDYS4 QQ:3496925334/ 注意!此文章被作者标记到 黑历史 专栏中,这意味着本篇文章可能存在 质量低下,流水账文,笔法低质 的问题 为了 ...

  8. 编写Java程序,使用 Socket类模拟用户加入 QQ 群时,QQ 小冰发送欢迎消息的场景(用户充当客户端,QQ 小冰充当服务端)

    查看本章节 查看作业目录 需求说明: 小冰是微软公司研发的人工智能机器人,被腾讯公司加入 QQ 群后,立即受到千万网友的喜爱.现在使用 Socket类模拟用户加入 QQ 群时,QQ 小冰发送欢迎消息的 ...

  9. 解决Docker运行命令时提示"Got permission denied while trying to connect to the Docker daemon socket"类情况

    Docker安装命令: 解决Docker运行命令时提示"Got permission denied while trying to connect to the Docker daemon ...

随机推荐

  1. HDU 4344 随机法判素数(费马小定理

    #include <cstdio> #include <ctime> #include <cmath> #include <algorithm> usi ...

  2. 与众不同 windows phone (6) - Isolated Storage(独立存储)

    原文:与众不同 windows phone (6) - Isolated Storage(独立存储) [索引页][源码下载] 与众不同 windows phone (6) - Isolated Sto ...

  3. 推荐一款功能强大的js 在线编辑器

    http://jszi.cn/public/oherub/11/edit

  4. 使用wireshark常用的过滤命令

    使用wireshark常用的过滤命令 方法/步骤 过滤源ip.目的ip.在wireshark的过滤规则框Filter中输入过滤条件.如查找目的地址为192.168.101.8的包,ip.dst==19 ...

  5. Hongwei Xi

    Hongwei Xi Hongwei Xi Hongwei Xi's Curriculum Vita Hongwei Xi

  6. CorePlot学习

    阅读这篇文章,指出它在国外    原文地址:https://github.com/core-plot/core-plot/wiki/High-Level-Design-Overview 强烈推荐阅读该 ...

  7. New 和 GetMem 的不同之处

    如果操作一个 record 指针中的字符串变量,会不会丢失 string 的内 存空间,造成内存泄漏? 结果是:使用 New() 分配的内存,会自动初始化 record 的内容,并且在 Dispose ...

  8. ThinkPhp学习09

    原文:ThinkPhp学习09 三.区间查询 $data['id']=array(array('gt',4),array('lt',10));//默认关系是 and 的关系 //SELECT * FR ...

  9. 与众不同 windows phone (33) - Communication(通信)之源特定组播 SSM(Source Specific Multicast)

    原文:与众不同 windows phone (33) - Communication(通信)之源特定组播 SSM(Source Specific Multicast) [索引页][源码下载] 与众不同 ...

  10. 【夯实基础】Spring在ssh中的作用

    尊重版权:http://blog.csdn.net/qjlsharp/archive/2009/03/21/4013255.aspx 写的真不错. 在SSH框假中spring充当了管理容器的角色.我们 ...