www.6766.com使用PHP Socket 编程模拟Http post和get请求_php实例_脚本之家

复制代码 代码如下:”;print_r;echo
“”;?>

艺术大器晚成:利用php的socket编制程序来直接给接口发送数据来模拟post的操作。

复制代码 代码如下:this is the data
postedArray([var1] => abc[var2] => how are you , my friend??)

复制代码 代码如下:Array ( [0] =>
returndataArray([web] => ‘wwwbaiducom'[pwd] =>
123456[action] => check[pseid] => ‘NDE005′[amt] => 1))

以上代码在本机81端口下已经经过测量检验。

//发送post的数额 fputs; $inheader = 1; while { $line = fgets;
//去除哀告包的头只展现页面包车型地铁归来数据 if ($inheader && ($line == “n” ||
$line == “rn”)) { $inheader = 0; } if { echo $line; } }

“;?>

复制代码 代码如下:

";print_r;echo "

本条要依据第三方类库HttpClient能够到这里下载:

php 模拟 http请求

运用PHP的curl增添或HttpClient.class.php类,那多个极度雷同,上面轻松的列出curl的贯彻代码。三个文件post2.php和getpost2.phppost2.php的剧情如下:

复制代码 代码如下:<?phprequire_once
‘HttpClient.class.php’;$params = array(‘web’ => ‘www.baidu.com’,’pwd’
=> ‘123456’,’action’ => ‘check’,’pseid’ => ‘NDE005′,’amt’ =>
1);$pageContents =
HttpClient::quickPost(”,
$params);$result = explode;print_r;?>

复制代码 代码如下:”;echo “

复制代码 代码如下:’abc’, ‘var2’=>’how
are you , my friend??’State of Qatar;//布局要post的字符串foreach ($argv as
$key=>$value卡塔尔 { if { $params .= “&”; $flag = 1; } $params.= $key.”=”;
$params.= urlencode; $flag = 1; } $length = strlen; //创立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”;

fclose;?>

getpost2.php的剧情如下:

getpost.php的内容如下

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

复制代码 代码如下:parseurl;
$this->header[‘Host’] = $this->urlinfo[‘host’]; return $this;
} public function get { $this->header =
array_merge($this->header, $header); return $this->request; }
public function post, $body = array { $this->header =
array_merge($this->header, $header); if { $this->body =
http_build_query; $this->header[‘Content-Type’] =
‘application/x-www-form-urlencoded’;
$this->header[‘Content-Length’] = strlen; } return
$this->request; } private function request { $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->requestInfo .= $this->body; } /*www.6766.com , *
注意:这里的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 {
echo $errstr.”; return false; } if ( fwrite($this->fp,
$this->requestInfo) ) { $str = “”; while { $str .= fread; }
$this->responseInfo = $str; } fclose; return $this->responseInfo;
} private function parseurl { $this->urlinfo = parse_url; } } //
$url = “”; $url =
“”; $http = Http::create; /*
发送get请求 echo $http->get(array( ‘User-Agent’ => ‘Mozilla/5.0
(Windows NT 6.1; WOW64) AppleWebKit/537.36 Chrome/35.0.1916.153
Safari/537.36’, )); *//*www.6766.com使用PHP Socket 编程模拟Http post和get请求_php实例_脚本之家。 发送post请求 */ echo $http->post(array(
‘User-Agent’ => ‘Mozilla/5.0 (Windows NT 6.1; WOW64卡塔尔(قطر‎AppleWebKit/537.36 Chrome/35.0.1920.153 Safari/537.36’, 卡塔尔国,
array(‘username’=>’发三个中文’, ‘age’=>22卡塔尔卡塔尔;

那边给大家享用大器晚成段使用PHP Socket 编程模拟Http
post和get央求的代码,非常的实用,结尾巴部分分大家再研商下php模拟http央求的三种艺术。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图