php之header重定向及socket编程
正常header
header('Location: http://www.baidu.com'); // 默认是302重定向
// 指定用301重定向, true参加意指用301信息替换原来的头信息.
header('Location: http://www.baidu.com',true,301);复制代码
临时重定向
对于一篇新闻, GET请求,重定向无所谓,还能看到原来的内容就行. 但如果是POST数据,比如 表单-->05.php, 05.php-重定向->06.php
// 05.html 表单
<form action="05.php" method="post">
<input type="text" name="username" />
<input type="submit" value="提交" />
</form>复制代码
// 05.php
<?php
// 307 临时重定向
header('Location: 06.php',true,307);
exit;
print_r($_POST);复制代码
// 06.php
<?php
print_r($_POST);复制代码
telnet发送请求
要求用 telnet 来请求02.php
这个程序是把收到的POST数据,写入文本
// o2.php
<?php
/*
分析: 要用POST方法
$方法 $路径 $版本
请求行..
主体内容....
据上:
POST /0606/02.php HTTP/1.1
Host: localhost
Content-type: application/x-www-form-urlencoded
Content-length: 23
*/
$str = implode($_POST,"\n");
file_put_contents('./post.txt',$str);
print_r($str);
复制代码
PHP+socket编程发送HTTP请求
利用socket打开80端口,打开后,"写入"请求信息,"读取"响应信息
class Http {
private $Version = '1.1';
private $urlinfo = array();
private $fh = null;
protected function conn($url) {
$this->urlinfo = parse_url($url);
$this->fh = fsockopen($this->urlinfo['host'],80);
}
public function get($url) {
$this->conn($url);
// 构造请求信息
$req = array();
$req[] = 'GET ' . $this->urlinfo['path'] . ' HTTP/1.l';
$req[] = 'Host: ' . $this->urlinfo['host'];
$req[] = '';
$req[] = '';
$req = implode($req,"\r\n");
fwrite($this->fh,$req);
$res = '';
while(!feof($this->fh)) {
$res .= fread($this->fh,1024);
}
return $res;
}
public function post() {
}
}
$http = new Http();
$html = $http->get('http://qdgithub.com/home/index/article/aid/60.html');
file_put_contents('./caiji.txt',$html);
echo 'OK';复制代码
本文为作者原创文章,转载无需和我联系,但请注明转载链接。 【前端黑猫】