搜索
首页php教程php手册PHp + socket编程练习回复

因为也是学习的路上,先画出思路,代码和看别人的学习的,最后是根据的思路完成的,可能比较简单,后期在去完善和更改

 

 

/**

 * php+socket 编程 发送HTTP请求

 * @version  c.php

 * 模拟博客园评论

 */

interface Proto{

    // 连接url

    public function conn($url);

    // 发送get请求

    public function get();

    // 发送post请求

    public function post($body);

    //关闭连接

    public function close();

}

class Http implements Proto{

    protected $response = '';

    protected $fh=null;

    protected $errno = -1;

    protected $errorstr = '';

 

    protected $line = array();

    protected $header = array();

    protected $body = array();

    public $url = array();

 

    public function __construct($url){

        $this->conn($url);

        $this->setHeader('Host: ' . $this->url['host']);

    }

    // 请求行

    protected function setLine($method){

        $this->line[0] = $method . ' ' . $this->url['path'] .  ' ' . 'HTTP/1.1';

    }

    // 头信息

    public function setHeader($headerline){

        $this->header[] = $headerline;

    }

    // 主体信息

    protected function setBody($body){

                $this->body[] = 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->errorstr,3);

    }

    // 构建get请求

    public function get(){

        $this->setLine('GET');

        $this->request();

        return $this->response;

    }

    // 发送post请求

    public function post($bo){

        $this->setLine('POST');

                // content-type

                $this->setHeader("Content-type: application/x-www-form-urlencoded");

 

                $this->setBody($bo);

                // content-length

                $this->setHeader("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);die;

        $req = implode(PHP_EOL,$req); // PHP_EOL 换行

//      echo $req;die;

        fwrite($this->fh,$req);

        while(!feof($this->fh)){

            $this->response .= fread($this->fh,1024);

        }

        $this->close();

    }

    public function close(){

            fclose($this->fh);

    }

}

 上面是简单的类

 

 

 

require("./c.php"); // 引入上面的类

$url = "http://www.cnblogs.com/mvc/PostComment/Add.aspx";

$http = new Http($url);

$http->setHeader("Cookie:xxxx");

$http->setHeader("Referer: http://www.cnblogs.com/geek12/p/4024793.html");

$http->setHeader("User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0");

 

$msg =array(

"blogApp"=>"geek12",

"body"=>"来自robot",

"parentCommentId"=>0,

"postId"=>4024793);

 

$http->post($msg);



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。