可以根据几根url地址,分析出主机,地址,协议等,然后用封装成的类拼接成GET请求信息,用fsockopen连接主机,进行读取操作,获取响应信息,打印

<?php
//http连接接口
interface proto{
//连接url
public function conn($url);
//发送get请求
public function get();
//发送post
public function post($num);
//关闭连接
public function close();
}
class Http implements proto{
const CRLF="\r\n";
protected $response='';
protected $poststr='';
protected $errno=-1;
protected $errstr='';
protected $version='HTTP/1.1';
protected $fh=null;
protected $url=array();
protected $line=array();
protected $header=array();
protected $body=array(); public function __construct($url){
$this->conn($url);
$this->setheader();
}
//负责写请求行
protected function setLine($method){
$this->line[0]=$method .' '.$this->url['path'] .' '.$this->version;
}
//负责写请求信息
protected function setHeader(){
$this->header[]='Host:'.$this->url['host'];
}
//负责写主题信息
protected function setBody($body){
$this->body=array(http_build_query($body));
} //连接url
public 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->request();
return $this->response;
}
//构造post请求数据
public function post($body=array()){
$this->setLine('POST'); $this->header[]='content-type:application/x-www-form-urlencoded';
// print_r($this->header);
$this->setBody($body);
$this->header[]='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);
} } //调试
$url='http://localhost/tieba/cunchu.php';
$http=new Http($url);
// echo $http->get();
// print_r($http);
echo $http->post(array('username'=>'zhangsan','title'=>'试试发帖','content'=>'这是用http协议拼接数据发送的'));
?>
 

socket编程发送GET请求的更多相关文章

  1. Linux C Socket编程发送结构体、文件详解及实例

    利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...

  2. socket编程实现HTTP请求

    利用c++语言+socket实现HTTP请求,请求获得的数据效果图如下: HTTP协议的下一层是TCP,根据HTTP协议只需要利用TCP发送下面的数据到达目标主机,目标主机就会发送相应的数据到客户端. ...

  3. HTTP 笔记与总结(5)socket 编程:使用 HTTP 协议模拟登录并发帖

    在 VeryCD 上注册两个帐号,发送和接收站内信,观察 POST 请求时发送的参数(h****2 发送给 d***2).(最好用 FireFox 的 FireBug 工具,发送站内信之前选中 “保持 ...

  4. HTTP 笔记与总结(4 )socket 编程:批量发帖

    浏览器发送 POST 请求: 表单 form.html <!doctype html> <html lang="en"> <head> < ...

  5. HTTP 笔记与总结(3 )socket 编程:发送 GET 请求

    使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...

  6. TCP socket 编程

    TCP socket 编程 讲一下 socket 编程 步骤 使用 socket 模块 建立 TCP socket 客户端和服务端 客户端和服务端之间的通信 图解 编程 举个例子 tcp_server ...

  7. 使用PHP Socket 编程模拟Http post和get请求

    这里给大家分享一段使用PHP Socket 编程模拟Http post和get请求的代码,非常的实用,结尾部分我们再讨论下php模拟http请求的几种方法. <?php /** * 使用PHP ...

  8. PHP + Socket 发送http请求进而实现站点灌水

    本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...

  9. 使用socket发送http请求(get/post)

    手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...

随机推荐

  1. 关于Aggregate 的一点用法

    比如 我们要 将数组或者对象中的某列或某属性 的值取出,然后 用 逗号隔开. 1.通常我们可能会用for 或foreach 来循环,然后将取出的值并添加至StringBuilder 2.用Aggreg ...

  2. 转:Unicode字符集和多字节字符集关系

    原文地址: http://my.oschina.net/alphajay/blog/5691 unicode.ucs-2.ucs-4.utf-16.utf-32.utf-8 http://stallm ...

  3. BNUOJ-29364 Bread Sorting 水题

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29364 题意:给一个序列,输出序列中,二进制1的个数最少的数.. 随便搞搞就行了,关于更多 ...

  4. HDU-4665 Unshuffle 搜索 | 2-SAT

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4665 本题的2-SAT建图颇为复杂,有时间再来填坑(自己写的一直挂着,标程建图太复杂了)...然后用暴 ...

  5. HDU-4614 Vases and Flowers 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...

  6. -lrt

    在编写pthread有关的程序时,编译时老是报"undefined reference to `pthread_create'"的错误,原因是没有链接pthread相关的库,gcc ...

  7. 做优步有什么旁门左道吗?No,贪小便宜会吃大亏!

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. A Tour of Go Errors

    An error is anything that can describe itself as an error string. The idea is captured by the predef ...

  9. 二、FreeMarker 模版开发指南 第二章 数值和类型

    章节内容如下:   基本内容 类型 一.基本内容 简介 什么是数值? 什么是类型? 数据模型是哈希表 a.简介 理解数值和类型的概念是理解数据模型的关键所在.然而,数值和类型的概念并不局限于数据模型, ...

  10. Jquery easyui treegrid实现树形表格的行拖拽

    前几天修改了系统的一个功能——实现树形列列表的行拖拽,以达到排序的目的.现在基本上功能实现,现做一个简单的总结. 1.拿到这个直接网上搜,有好多,但是看了后都觉得不是太复杂就是些不是特别想看的例子,自 ...