mg4377娱乐娱城官网_mg4377娱乐手机版_www.mg4377.com

简易利用,编制程序模拟Http

时间:2019-09-28 02:15来源:mg4377娱乐手机版
笔者的SimpleLoader里面包车型大巴一块 此处给大家分享一段使用PHP Socket 编制程序模拟Httppost和get须求的代码,极度的实用,结尾巴部分分我们再研商下php模拟http恳求的二种方法。 ?php

笔者的SimpleLoader里面包车型大巴一块

此处给大家分享一段使用PHP Socket 编制程序模拟Http post和get须求的代码,极度的实用,结尾巴部分分我们再研商下php模拟http恳求的二种方法。

<?php
/*socket收发数据
@host(string) socket服务器IP
@post(int) 端口
@str(string) 要发送的多寡
@back 1|0 socket端是否有数据再次回到
回来true|false|服务端数据
*/
function sendSocketMsg($host,$port,$str,$back=0){
echo '**************************';
$socket = socket_create(AF_INET,SOCK_STREAM,0);
if ($socket < 0) return false;
$result = @socket_connect($socket,$host,$port);
if ($result == false)return false;
socket_write($socket,$str,strlen($str));
socket_shutdown($socket);
if($back!=0){
$input = socket_read($socket,5000);
socket_close ($socket);
return $input;
}else{
简易利用,编制程序模拟Http。socket_close ($socket);
return true;
}}

复制代码 代码如下:

$host = '192.168.1.100';
$post = 12299;
$str = '{"@type":"xxx","args":[{"@type":"xxx","userInfo":{"nickname":"nickname","phoneNo":"123143345345","roleType":"USER"}}],"methodName":"registerUser","serviceInterface":"xxx"}';
$back = 1;

echo sendSocketMsg($host,$post,$str,$back);
<?phpnamespace Server;class Socket{    const PORT=1024;    public function create(){        $socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);        socket_bind($socket, "127.0.0.1",self::PORT);        socket_listen($socket,4);        echo "start http://127.0.0.1:1024rn";        while(true) {            $msgsock = socket_accept($socket);             $buf = socket_read($msgsock,9024);            if(preg_match HTTP/1.1/",$buf,$matchs)){                if($matchs[1]!="favicon.ico"){                    echo $buf;                    $_SERVER['PATH_INFO']=$matchs[1] ? $matchs[1] : "Index/Index/index";                    SimpleLoader::router();                    $html=SimpleLoader::pathInfo();                }                socket_write($msgsock,$html);            }else{                socket_write($msgsock,"hello world");            }             socket_close($msgsock);        }         socket_close($socket);    }}

<?php /**
 * 使用PHP Socket 编制程序模拟Http post和get需要
 * @author koma
 */ class Http{
    private $sp = "rn"; //这里必得求写成双引号     private $protocol = 'HTTP/1.1';
    private $requestLine = "";
    private $requestHeader = "";
    private $requestBody = "";
    private $requestInfo = "";
    private $fp = null;
    private $urlinfo = null;
    private $header = array();
    private $body = "";
    private $responseInfo = "";
    private static $http = null; //Http对象单例    
    private function __construct() {}
    public static function create() {
        if ( self::$http === null ) { 
            self::$http = new Http();
        }
        return self::$http;
    }
    public function init($url) {
        $this->parseurl($url);
        $this->header['Host'] = $this->urlinfo['host'];
        return $this;
    }
    public function get($header = array()) {
        $this->header = array_merge($this->header, $header);
        return $this->request('GET');
    }
    public function post($header = array(), $body = array()) {
        $this->header = array_merge($this->header, $header);
        if ( !empty($body) ) {
            $this->body = http_build_query($body);
            $this->header['Content-Type'] = 'application/x-www-form-urlencoded';
            $this->header['Content-Length'] = strlen($this->body);
        }
        return $this->request('POST');
    }
    private function request($method) {
        $header = "";
        $this->requestLine = $method.' '.$this->urlinfo['path'].'?'.$this->urlinfo['query'].' '.$this->protocol;
        foreach ( $this->header as $key => $value ) {
            $header .= $header == "" ? $key.':'.$value : $this->sp.$key.':'.$value;
        }
        $this->requestHeader = $header.$this->sp.$this->sp;
        $this->requestInfo = $this->requestLine.$this->sp.$this->requestHeader;
        if ( $this->body != "" ) {
            $this->requestInfo .= $this->body;
        }
        /*
         * 注意:这里的fsockopen中的url参数格局为"www.xxx.com"
         * 不可能带"
         */
        $port = isset($this->urlinfo['port']) ? isset($this->urlinfo['port']) : '80';
        $this->fp = fsockopen($this->urlinfo['host'], $port, $errno, $errstr);
        if ( !$this->fp ) {
            echo $errstr.'('.$errno.')';
            return false;
        }
        if ( fwrite($this->fp, $this->requestInfo) ) {
            $str = "";
            while ( !feof($this->fp) ) {
                $str .= fread($this->fp, 1024);
            }
            $this->responseInfo = $str;
        }
        fclose($this->fp);
        return $this->responseInfo;
    }
    private function parseurl($url) {
        $this->urlinfo = parse_url($url);
    }
} // $url = "";
$url = ""; $http = Http::create()->init($url); /* 发送get请求 
echo $http->get(array(
    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
));
*/
 /* 发送post请求 */ echo $http->post(array(
        'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36',
), array('username'=>'发三个中文', 'age'=>22));

效果:

php 模拟 http请求

图片 1

主意一:利用php的socket编程来间接给接口发送数据来模拟post的操作。

图片 2

确立七个公文post.php,getpost.php
post.php内容如下:

复制代码 代码如下:

<?php
 $flag = 0;
 $params = '';
 $errno = '';
 $errstr = '';
 //要post的数据
$argv = array(
    'var1'=>'abc',
    'var2'=>'how are you , my friend??');
//构造要post的字符串
foreach ($argv as $key=>$value) {
    if ($flag!=0) {
        $params .= "&";
        $flag = 1;
    }
    $params.= $key."="; $params.= urlencode($value);
    $flag = 1;
    }
    $length = strlen($params);
     //创建socket连接
    $fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);
    //构造post乞求的头
    $header  = "POST /flandy/getpost.php HTTP/1.1rn";
    $header .= "Host:127.0.0.1rn";
    $header .= "Referer:/flandy/post.phprn";
    $header .= "Content-Type: application/x-www-form-urlencodedrn";
    $header .= "Content-Length: ".$length."rn";
    $header .= "Connection: Closernrn";
    //添加post的字符串
    $header .= $params."rn";    

    //发送post的数据
    fputs($fp,$header);
    $inheader = 1;
    while (!feof($fp)) {
        $line = fgets($fp,1024); //去除央求包的头只呈现页面的回来数据
        if ($inheader && ($line == "n" || $line == "rn")) {
             $inheader = 0;
        }
        if ($inheader == 0) {
          echo $line;
        }
    }

fclose($fp);
?>

getpost.php的内容如下

编辑:mg4377娱乐手机版 本文来源:简易利用,编制程序模拟Http

关键词: php Socket Http PHP开发札记 开发杂记