【www.bbyears.com--apache】
php模拟http请求需要实现以下步骤:
1.连接apache服务器
使用fsockopen:专门用于连接服务器,得到一个连接资源
2.写入http协议
使用fwrite向资源写入内容
3.接收数据
请求成功后返回的数据会被存放在资源中
4.解析数据:
使用fgets,和fgetc函数
实现代码:
apache $f=fsockopen("localhost",98,$erron,$error); //2.写入http协议 //2.1拼凑http协议 //请求行 $http="GET /phpstudy/index.php HTTP/1.1\r\n"; //请求头 $http .="Host:localhost\r\n"; //空行 $http .="\r\n"; //2.2写给apache服务器 if(fwrite($f,$http)) { //写入成功 //3.数据已经接收并存放在f资源中 //4.解析资源 //循环遍历 while($line=fgets($f,1024)) { //输出 echo $line .""; } }