php发送get代码,php 利用socket发送GET,POST请求的实例代码

php 利用socket发送GET,POST请求的实例代码

fd2630bd3492bdb3e411096f43d5ed2d.png

HTTP响应的格式与请求的格式十分相似:

[]

我们可以利用HTTP发送请求的原理,可以重新考虑利用socket发送HTTP请求。

Socket的英文原义是“孔”或“插座”。通常也称作“套接字”,用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信。在Internet上的主机一般运行了多个服务软件,同时提供几种服务。每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务。如此看来,其实利用socket操作远程文件和读写本地的文件一样容易,把本地文件看成通过硬件传输,远程文件通过网线传输就行了。

因而可以将发送请求的考虑成 建立连接->打开socket接口(fsockopen())->写入请求(fwrite())->读出响应(fread()->关闭文件(fclose())。话不多说,直接上代码:

interface Proto {

// 连接url

function conn($url);

//发送get查询

function get();

// 发送post查询

function post();

// 关闭连接

function close();

}

class Http implements Proto {

const CRLF = "

";

protected $errno = -1;

protected $errstr = "";

protected $response = "";

protected $url = null;

protected $version = "HTTP/1.1";

protected $fh = null;

protected $line = array();

protected $header = array();

protected $body = array();

public function __construct($url) {

$this->conn($url);

$this->setHeader("Host: " . $this->url["host"]);

}

// 此方法负责写请求行

protected function setLine($method) {

$this->line[0] = $method . " " . $this->url["path"] . "?" .$this->url["query"] . " ". $this->version;

}

// 此方法负责写头信息

public function setHeader($headerline) {

$this->header[] = $headerline;

}

// 此方法负责写主体信息

protected function setBody($body) {

$this->body[] = http_build_query($body);

}

// 连接url

public function conn($url) {

$this->url = parse_url($url);

// 判断端口

if(!isset($this->url["port"])) {

$this->url["port"] = 80;

}

// 判断query

if(!isset($this->url["query"])) {

$this->url["query"] = "";

}

$this->fh = fsockopen($this->url["host"],$this->url["port"],$this->errno,$this->errstr,3);

}

//构造get请求的数据

public function get() {

$this->setLine("GET");

$this->request();

return $this->response;

}

// 构造post查询的数据

public function post($body = array()) {

$this->setLine("POST");

// 设计content-type

$this->setHeader("Content-type: application/x-www-form-urlencoded");

// 设计主体信息,比GET不一样的地方

$this->setBody($body);

// 计算content-length

$this->setHeader("Content-length: " . strlen($this->body[0]));

$this->request();

return $this->response;

}

// 真正请求

public function request() {

// 把请求行,头信息,实体信息 放在一个数组里,便于拼接

$req = array_merge($this->line,$this->header,array(""),$this->body,array(""));

//print_r($req);

$req = implode(self::CRLF,$req);

//echo $req; exit;

fwrite($this->fh,$req);

while(!feof($this->fh)) {

$this->response .= fread($this->fh,1024);

}

$this->close(); // 关闭连接

}

// 关闭连接

public function close() {

fclose($this->fh);

}

}

利用此类发送一个简单的GET请求:

$url="http://home.cnblogs.com/u/DeanChopper/";

$http=new Http($url);

$response=$http->get();

print_r($response);

返回值为信息,可以对响应信息进行进一步处理,得到自己想得到的内容。

以上就是php 利用socket发送GET,POST请求的实例代码的详细内容,更多关于php 发送GET,POST请求的资料请关注云海天教程其它相关文章!

php 利用socket发送GET,POST请求的实例代码相关教程

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值