http--发送post请求 发帖
2016-01-15 09:48
507 查看
在之前get请求的类上继续,更改了 construct,request,setHeader方法
写setBody,post,close方法
实例化,参数 你要提交帖子内容的网址(是需要收到post内容的网址)
调用post方法 ,参数 你要提交的帖子需要的参数组成的数组详情看下方代码
<?php
/*php+socket编程 发送http
http类 操作案例get--获取网页 post批量发帖 */
// 接口
interface Proto{
//连接
function conn($url);
//发送get
function get();
//发送post*继承后 参数必须一致
function post($body);
//关闭
function close();
}
//* 继承的东西全要继承不能不写
class Http implements Proto{
protected $url=null;
protected $line=array();
protected $header=array();
protected $body=array();
protected $fh=NULL;
protected $version="HTTP/1.1";
protected $errno= -1;
protected $errstr='';
protected $resp='';//响应
const CRLF="\r\n";
//* 一开始就需要的变量 填在construct里
public function __construct($url){
$this->conn($url);
//$this->setHeader();//本来写在get方法,但所有请求方法header相同
$this->setHeader("Host:".$this->url['host']);
}
//连接,得到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);
// resource fsockopen ( string $hostname [, int $port = -1 [,
// int &$errno [, string &$errstr [, float $timeout
// = ini_get("default_socket_timeout") ]]]] )
// 初始化一个套接字连接到指定主机(hostname)。也就是打开了通道可以读取写入进这个网站
}
//发送get *怎么get写在$line前面 setline方法设一个变量,在不同请求方法里用
public function get(){
$this->setLine('GET');
$this->request();
return $this->resp;
}
//发送post
public function post($body){
// print_r($body);exit;
$this->setLine('POST');
// 设置content-type
$this->setHeader("Content-type:application/x-www-form-urlencoded");
$this->setBody($body);
//设置content-length
$this->setHeader("Content-length: ".strlen($this->body[0]));
$this->request();
// return $this->resp;
}
// 请求拼接发送 *怎么整合 请求数组变成str
public function request(){
$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req=implode(self::CRLF, $req);//crlf换行
echo $req;exit;
// 写入请求*
fwrite($this->fh, $req);
while (!feof($this->fh)) {
$this->resp.=fread($this->fh, 1024);
}
// 关闭连接
$this->close();
}
//关闭
public function close(){
//
fclose($this->fh);
}
// 拼接 请求行1
protected function setLine($method){
$this->line[]=$method." ".$this->url['path']." ".$this->version;
}
//请求头部2,*因为post有type和length,所以改!
protected function setHeader($headerline){
// $this->header[]="Host:".$this->url['host'];
$this->header[]=$headerline;
}
//请求主体部分3,收到数组
protected function setBody($body){
// 把数组直接变成&连接的post形式
$this->body[]=http_build_query($body);
}
}
// 下面两行,是get方法,发送请求获取网页
// $http=new Http('http://news.qq.com/a/20160107/019440.htm');
// echo $http->get();
// url====[scheme] => http [host] => news.163.com [path] => /16/0107/06/BCN66S6S00014AED.html
//line === Array ( [0] => GET /16/0107/06/BCN66S6S00014AED.html HTTP/1.1 )
//header === Array ( [0] => Host:news.163.com )
// $http->request();
// 下面两行是post ,发帖 #如果需要循环就循环下面两行,就str_shuffle一下,然后substr截取前几个字符给下面的参数
$http=new Http('http://localhost/~xxxxx/project/tieba/p_action.php');
$http->post(array('username'=>'lisi','title'=>'good idea','content'=>'yep'));
// ,'submit'=>'发布新帖'
// 一直空白,检查提交的http://localhost/~xxxxx/project/tieba/p_action.php 这个
// 路径对不对,之前是index页就是显示页,其实应该是参数提交到的那个操作页
?>
卡过两次,一次是post里加了参数,但是上面接口没有加,一直没反应,而且看network报304
还有一次就是发现写的网址不是接收post数据的网址(因为我用的是自己的贴吧项目),也是一直空白,network200OK
写setBody,post,close方法
实例化,参数 你要提交帖子内容的网址(是需要收到post内容的网址)
调用post方法 ,参数 你要提交的帖子需要的参数组成的数组详情看下方代码
<?php
/*php+socket编程 发送http
http类 操作案例get--获取网页 post批量发帖 */
// 接口
interface Proto{
//连接
function conn($url);
//发送get
function get();
//发送post*继承后 参数必须一致
function post($body);
//关闭
function close();
}
//* 继承的东西全要继承不能不写
class Http implements Proto{
protected $url=null;
protected $line=array();
protected $header=array();
protected $body=array();
protected $fh=NULL;
protected $version="HTTP/1.1";
protected $errno= -1;
protected $errstr='';
protected $resp='';//响应
const CRLF="\r\n";
//* 一开始就需要的变量 填在construct里
public function __construct($url){
$this->conn($url);
//$this->setHeader();//本来写在get方法,但所有请求方法header相同
$this->setHeader("Host:".$this->url['host']);
}
//连接,得到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);
// resource fsockopen ( string $hostname [, int $port = -1 [,
// int &$errno [, string &$errstr [, float $timeout
// = ini_get("default_socket_timeout") ]]]] )
// 初始化一个套接字连接到指定主机(hostname)。也就是打开了通道可以读取写入进这个网站
}
//发送get *怎么get写在$line前面 setline方法设一个变量,在不同请求方法里用
public function get(){
$this->setLine('GET');
$this->request();
return $this->resp;
}
//发送post
public function post($body){
// print_r($body);exit;
$this->setLine('POST');
// 设置content-type
$this->setHeader("Content-type:application/x-www-form-urlencoded");
$this->setBody($body);
//设置content-length
$this->setHeader("Content-length: ".strlen($this->body[0]));
$this->request();
// return $this->resp;
}
// 请求拼接发送 *怎么整合 请求数组变成str
public function request(){
$req=array_merge($this->line,$this->header,array(''),$this->body,array(''));
$req=implode(self::CRLF, $req);//crlf换行
echo $req;exit;
// 写入请求*
fwrite($this->fh, $req);
while (!feof($this->fh)) {
$this->resp.=fread($this->fh, 1024);
}
// 关闭连接
$this->close();
}
//关闭
public function close(){
//
fclose($this->fh);
}
// 拼接 请求行1
protected function setLine($method){
$this->line[]=$method." ".$this->url['path']." ".$this->version;
}
//请求头部2,*因为post有type和length,所以改!
protected function setHeader($headerline){
// $this->header[]="Host:".$this->url['host'];
$this->header[]=$headerline;
}
//请求主体部分3,收到数组
protected function setBody($body){
// 把数组直接变成&连接的post形式
$this->body[]=http_build_query($body);
}
}
// 下面两行,是get方法,发送请求获取网页
// $http=new Http('http://news.qq.com/a/20160107/019440.htm');
// echo $http->get();
// url====[scheme] => http [host] => news.163.com [path] => /16/0107/06/BCN66S6S00014AED.html
//line === Array ( [0] => GET /16/0107/06/BCN66S6S00014AED.html HTTP/1.1 )
//header === Array ( [0] => Host:news.163.com )
// $http->request();
// 下面两行是post ,发帖 #如果需要循环就循环下面两行,就str_shuffle一下,然后substr截取前几个字符给下面的参数
$http=new Http('http://localhost/~xxxxx/project/tieba/p_action.php');
$http->post(array('username'=>'lisi','title'=>'good idea','content'=>'yep'));
// ,'submit'=>'发布新帖'
// 一直空白,检查提交的http://localhost/~xxxxx/project/tieba/p_action.php 这个
// 路径对不对,之前是index页就是显示页,其实应该是参数提交到的那个操作页
?>
卡过两次,一次是post里加了参数,但是上面接口没有加,一直没反应,而且看network报304
还有一次就是发现写的网址不是接收post数据的网址(因为我用的是自己的贴吧项目),也是一直空白,network200OK
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- PHP VBS JS 函数 对照表
- C语言实现的统计php代码行数功能源码(支持文件夹、多目录)
- php xml 入门学习资料