PHP Socket请求类

来源:互联网 发布:自制手机铃声软件 编辑:程序博客网 时间:2024/06/08 02:14

一、开启socket

phpinfo()查看是否开启socket扩展否则请在php.ini中开启。

二、什么是socket

Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。


三、socket函数

socket_accept() 接受一个Socket连接
socket_bind() 把socket绑定在一个IP地址和端口上
socket_clear_error() 清除socket的错误或最后的错误代码
socket_close() 关闭一个socket资源
socket_connect() 开始一个socket连接
socket_create_listen() 在指定端口打开一个socket监听
socket_create_pair() 产生一对没有差别的socket到一个数组里
socket_create() 产生一个socket,相当于产生一个socket的数据结构
socket_get_option() 获取socket选项
socket_getpeername() 获取远程类似主机的ip地址
socket_getsockname() 获取本地socket的ip地址
socket_iovec_add() 添加一个新的向量到一个分散/聚合的数组
socket_iovec_alloc() 这个函数创建一个能够发送接收读写的iovec数据结构
socket_iovec_delete() 删除一个已分配的iovec
socket_iovec_fetch() 返回指定的iovec资源的数据
socket_iovec_free() 释放一个iovec资源
socket_iovec_set() 设置iovec的数据新值
socket_last_error() 获取当前socket的最后错误代码
socket_listen() 监听由指定socket的所有连接
socket_read() 读取指定长度的数据
socket_readv() 读取从分散/聚合数组过来的数据
socket_recv() 从socket里结束数据到缓存
socket_recvfrom() 接受数据从指定的socket,如果没有指定则默认当前socket
socket_recvmsg() 从iovec里接受消息
socket_select() 多路选择
socket_send() 这个函数发送数据到已连接的socket
socket_sendmsg() 发送消息到socket
socket_sendto() 发送消息到指定地址的socket
socket_set_block() 在socket里设置为块模式
socket_set_nonblock() socket里设置为非块模式
socket_set_option() 设置socket选项
socket_shutdown() 这个函数允许你关闭读、写、或指定的socket
socket_strerror() 返回指定错误号的周详错误
socket_write() 写数据到socket缓存
socket_writev() 写数据到分散/聚合数组

四、PHP socket发送请求类

1、在根目录新建一个01.php文件

<?php/** * http请求类的接口 */interface proto {//链接urlfunction conn($url);//发送get查询function get();//发送post查询function post();//关闭链接function close();}/*** HTTP类*/class Http implements Proto{const CRLF = "\r\n";protected $version = 'HTTP/1.1';protected $url = array();protected $fh = null;protected $errno = -1;protected $errstr = '';protected $response = "";protected $pargma = array();protected $cache = array();protected $connection = array();protected $line = array();protected $header = array();protected $body = array();//构造函数public function __construct($url){$this->conn($url);$this->setHeader('Host: ' . $this->url['host']);$this->pargma[] = "Pragma: no-cache";$this->cache[] = "Cache-Control: no-cache";$this->connection[] = "Connection: Close";}//此方法负责写请求行protected function setLine($method){$this->line[0] = $method . ' ' . $this->url['path'] . ' ' . $this->version;}//此方法负责写头信息protected function setHeader($headerline){$this->header[] = $headerline;}//此方法负责写主体信息protected function setBody($body){$this->body[] = http_build_query($body);}//链接urlpublic function conn($url) {$this->url = parse_url($url);//判断端口if (!isset($this->url['port'])) {$this->url['port'] = 80;}$this->fh = fsockopen($this->url['host'],$this->url['port'],$this->errno,$this->errstr,3);}//构造get请求的数据public function get(){$this->setLine('GET');$this->getrequest();return $this->response;}//构造post查询的数据public function post($body = array()){$this->setLine('POST');$this->setHeader('Content-Type: application/x-www-form-urlencoded');//设置主体信息$this->setBody($body);$this->setHeader('Content-Length: ' . strlen($this->body[0]));$this->postrequest();return $this->response;}//真正get的请求public function getrequest(){//把请求放在一个数组里$req = array_merge($this->line,$this->header,$this->pargma,$this->connection,$this->cache,array(''),array(''));$req = implode(self::CRLF, $req);fwrite($this->fh, $req);while (!feof($this->fh)) {$this->response .= fread($this->fh, 1024);}$this->close();}//真正post的请求public function postrequest(){//把请求放在一个数组里$req = array_merge($this->line,$this->header,$this->pargma,$this->connection,$this->cache,array(''),$this->body,array(''));$req = implode(self::CRLF, $req);fwrite($this->fh, $req);while (!feof($this->fh)) {$this->response .= fread($this->fh, 1024);}$this->close();}//关闭链接public function close(){fclose($this->fh);}}

2、用get发送请求

$url = 'http://www.php.net/manual/zh/ref.sockets.php';$http = new Http($url);echo $http->get();

在浏览器中访问01.php


3、用post发送请求

在根目录新建一个文件02.php

<?phpvar_dump($_POST);?>

然后再01.php中加入


$url = 'http://localhost/02.php';$body = array('username'=>'csdn','age'=>10);$http = new Http($url);echo $http->post($body);

在浏览器中访问01.php


恭喜你已成功!

1 0
原创粉丝点击