可以根据几根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. HDU-4639 Hehe 简单DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4639 简单递推题,呵呵,不多说... //STATUS:C++_AC_15MS_272KB #incl ...

  2. ffmpeg编码YUV420视频序列

    依旧是这里的测试序列 http://www.cnblogs.com/zzugyl/p/3678865.html测试了JM和libx264的编解码质量后来用ffmpeg转码 发现忘记了命令行转码的命令网 ...

  3. -lrt

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

  4. Activity详解

    Activity是android应用的重要组成单元之一(另外3个是Service,BroadcastReceiver和ContentProvider).实际应用包含了多个Activity,不同的Act ...

  5. MSSQLSERVER数据库- SQL删除重复数据的五种方式

    删除重复的数据,在平时的工作中还是会和碰到的,感觉挺有用,从网上摘录的,记在这里,以备需要时查阅 --方法一,IN方式,适合2000/2005/2008,6728 毫秒 DELETE [student ...

  6. Python中如何把一个UTC时间转换为本地时间

    需求: 将20141126010101格式UTC时间转换为本地时间. 在网上搜了好长时间都没有找到完美的解决方案.有的引用了第三方库,这就需要在现网安装第三方的软件.这个是万万不可的.因为真实环境不一 ...

  7. 文件TEXTBOX

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. WCF之初体验

    什么是WCF? WCF的全称是:Windows通信基础(WindowsCommunication Foundation).本质来讲,他是一套软件开发包. WCF和WebService的差别 Webse ...

  9. leetcode解决问题的方法||Integer to Roman问题

    problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...

  10. MySQL查询in操作 查询结果按in集合顺序显示(转)

    MySQL 查询in操作,查询结果按in集合顺序显示的实现代码,需要的朋友可以参考下. MySQL 查询in操作,查询结果按in集合顺序显示 复制代码代码如下: select * from test ...