可以根据几根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. HW6.27

    import java.util.Scanner; import java.util.Arrays; public class Solution { public static void main(S ...

  2. 挑战树莓派:谁才是Geek最爱的开发板?

    树莓派(Raspberry Pi)是一块跟信用卡差不多大小的开发板,它的初衷是以低廉的硬件和开源软件扶持一些落后地区的电脑科学教育.由于它在性能和价格方面有一个很好的平衡点,所以很多硬件玩家也想买一个 ...

  3. adb不响应

    1.CMD命令窗口输入:adb nodaemon server .然后就会提示你哪个端口被占用了. 2.输入netstat -ano | findstr "5037" .然后会弹出 ...

  4. UINavgation日常小bug-有兴趣的朋友可以看看

    UINavgation日常 UINavgation 今天在做一个小Demo,发现一个Bug,挺有意思的,就是在你不断调用Navigation- (void)pushViewController:(UI ...

  5. ORA-12154 TNS无法解析指定的连接标识符

    又是这个百无聊赖的问题,尽管问题芝麻点大,却让我们好找啊! 非常久没有安装oracle了.今天安装11g的时候,用PLSQL Developer连接时,就出现了这个俗不可耐的问题:ORA-12154 ...

  6. 【转】使用junit4进行单元测试(高级篇)

    转自:http://blog.csdn.net/andycpp/article/details/1329218 通过前 2 篇文章,您一定对 JUnit 有了一个基本的了解,下面我们来探讨一下JUni ...

  7. Aizu 2305 Beautiful Currency DP

    Beautiful Currency Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest ...

  8. 网络防火墙实战-基于pfsense(2)

    安装虚拟机 本博客所有内容是原创,如果转载请注明来源 http://blog.csdn.net/myhaspl/

  9. 安装Windows SDK7.1时发生的一个错误(附解决办法)

    A problem occurred while installing selected Windows SDK components. Installation of the "Micro ...

  10. emplace_back与push_back的区别

    std::vector::emplace_back     C++   Containers library   std::vector   template< class... Args &g ...