使用PHP Socket 编程模拟Http post和get请求

时间:2021-05-25

这里给大家分享一段使用PHP Socket 编程模拟Http post和get请求的代码,非常的实用,结尾部分我们再讨论下php模拟http请求的几种方法。

复制代码 代码如下:
<?php /**
* 使用PHP Socket 编程模拟Http post和get请求
* @author koma
*/ class Http{
private $sp = "\r\n"; //这里必须要写成双引号 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-',
'pwd' => '123456',
'action' => 'check',
'pseid' => 'NDE005',
'amt' => 1);
$pageContents = HttpClient::quickPost('http://localhost:81/flandy/getpost3.php', $params);
$result = explode(',', $pageContents);
print_r($result);
?>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章