socket编程发送GET请求
可以根据几根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请求的更多相关文章
- Linux C Socket编程发送结构体、文件详解及实例
利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...
- socket编程实现HTTP请求
利用c++语言+socket实现HTTP请求,请求获得的数据效果图如下: HTTP协议的下一层是TCP,根据HTTP协议只需要利用TCP发送下面的数据到达目标主机,目标主机就会发送相应的数据到客户端. ...
- HTTP 笔记与总结(5)socket 编程:使用 HTTP 协议模拟登录并发帖
在 VeryCD 上注册两个帐号,发送和接收站内信,观察 POST 请求时发送的参数(h****2 发送给 d***2).(最好用 FireFox 的 FireBug 工具,发送站内信之前选中 “保持 ...
- HTTP 笔记与总结(4 )socket 编程:批量发帖
浏览器发送 POST 请求: 表单 form.html <!doctype html> <html lang="en"> <head> < ...
- HTTP 笔记与总结(3 )socket 编程:发送 GET 请求
使用 PHP + socket 模拟发送 HTTP GET 请求,过程是: ① 打开连接 ② 构造 GET 请求的数据:写入请求行.请求头信息.请求主体信息(GET 请求没有主体信息) ③ 发送 GE ...
- TCP socket 编程
TCP socket 编程 讲一下 socket 编程 步骤 使用 socket 模块 建立 TCP socket 客户端和服务端 客户端和服务端之间的通信 图解 编程 举个例子 tcp_server ...
- 使用PHP Socket 编程模拟Http post和get请求
这里给大家分享一段使用PHP Socket 编程模拟Http post和get请求的代码,非常的实用,结尾部分我们再讨论下php模拟http请求的几种方法. <?php /** * 使用PHP ...
- PHP + Socket 发送http请求进而实现站点灌水
本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...
- 使用socket发送http请求(get/post)
手动发送http请求 解释说明 https://blog.csdn.net/zhangliang_571/article/details/23508953 http://www.cnblogs.com ...
随机推荐
- 解决adb问题的方法
The connection to adb is down,and a server error has occured. 在网上找的那个高端方法根本不管用,来,试试我的方法.. 先装个360手机助手 ...
- Android开发--ListPreferance 运行报错:android.preference.ListPreference.findIndexOfValue(ListPreference.java:169)
在Stack Overflow上找到的答案:http://stackoverflow.com/questions/4357094/exception-on-listpreferences “i fix ...
- homework-01 博客记录
程序思路: 一维的主要思想是:最大序列的初始项一定是正数,然后在此项基础上向后遍历,当该连续序列的的总和小于或等于0时,就是这个序列的断点,因为若把该序列当做一个数则为负数,一定不是另一个序列的初始. ...
- Redis集群的使用测试(Jedis客户端的使用)
Redis集群的使用测试(Jedis客户端的使用)1.Jedis客户端建议升级到最新版(当前为2.7.3),这样对3.0.x集群有比较好的支持.https://github.com/xetorthio ...
- 空循环比较 for foreach array_map array_walk
申请一个数组,然后不断的跑空循环,看看执行时间 for循环 foreach (不使用键) foreach(使用键) array_map array_walk 查看效率速度发现很明显 是foreach更 ...
- JavaScript要点(十二) HTML DOM 事件
HTML DOM 使 JavaScript 有能力对 HTML 事件做出反应. 对事件做出反应 我们可以在事件发生时执行 JavaScript,比如当用户在 HTML 元素上点击时. 如需在用户点击某 ...
- Finite Difference Method with Mathematica
Euler's method
- C++11 std::bind std::function 高级使用方法
从最基础的了解,std::bind和std::function /* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ # ...
- 继续推广我的新博客xysay:http://www.xysay.com/
RT 博客收拾了一下,准备以后就在那里记录论文笔记啦,求交流,求推荐,求友链~~~ http://www.xysay.com/
- Nginx与Tomcat、Client之间请求的长连接配置不一致问题解决[转]
http://bert82503.iteye.com/blog/2152613 前些天,线上出现“服务端长连接与客户端短连接引起Nginx的Writing.Active连接数过高问题”,这个是由于“服 ...