首页 > 编程 > PHP > 正文

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

2020-03-22 17:18:54
字体:
来源:转载
供稿:网友
这里给大家分享一段使用PHP Socket 编程模拟Http post和get请求的代码,非常的实用,结尾部分我们再讨论下php模拟http请求的几种方法。www.xxx.com"
* 不能够带"http://"这种
*/
$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-
return $this- responseInfo;
}
private function parseurl($url) {
$this- urlinfo = parse_url($url);
}
} // $url = "http://news.163.com/14/1102/01/AA0PFA7Q00014AED.html";
$url = "http://localhost/httppro/post.php"; $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请求方法一:利用php的socket编程来直接给接口发送数据来模拟post的操作。建立两个文件post.php,getpost.php
post.php内容如下:http://localhost:81/flandy/getpost2.php web=" . $website .
"&pwd=" . $pwd . "&action=check&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量
$curl_result = curl_exec($ch);
$result = explode(',', $curl_result);
curl_close($ch);
print_r($result);

getpost2.php的内容如下:http://scripts.incutio.com/httpclient/复制代码 代码如下:
< php
require_once 'HttpClient.class.php';
$params = array('web' => 'www.baidu.com',
'pwd' => '123456',
'action' => 'check',
'pseid' => 'NDE005',
'amt' => 1);
$pageContents = HttpClient::quickPost('http://localhost:81/flandy/getpost3.php', $params);
$result = explode(',', $pageContents);
print_r($result);

PHP教程

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表