`
mengdejun
  • 浏览: 405295 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

php socket模拟POST GET请求 fsockopen版

    博客分类:
  • Php
阅读更多
function httpRequestGET($url){
$url2 = parse_url($url);
$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
$host_ip = @gethostbyname($url2["host"]);
$fsock_timeout=20;
if(($fsock = fsockopen($host_ip, 80, $errno, $errstr, $fsock_timeout)) < 0){
return false;
}

$request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : ""); 
$in = "GET " . $request . " HTTP/1.0\r\n";
$in .= "Accept: */*\r\n";
$in .= "User-Agent: Payb-Agent\r\n";
$in .= "Host: " . $url2["host"] . "\r\n";
$in .= "Connection: Close\r\n\r\n";
if(!@fwrite($fsock, $in, strlen($in))){
fclose($fsock);
return false;
}
unset($in);

$out = ""; 
while($buff = @fgets($fsock, 2048)){
$out .= $buff;
}
fclose($fsock);
$pos = strpos($out, "\r\n\r\n");
$head = substr($out, 0, $pos);    //http head
$status = substr($head, 0, strpos($head, "\r\n"));    //http status line
$body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body
if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){
if(intval($matches[1]) / 100 == 2){
return $body; 
}else{
return false;
}
}else{
return false;
}
}
function httpRequestPOST($url,$post_data){
$url2 = parse_url($url);
$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
$url2["port"] = ($url2["port"] == "" ? 80 : $url2["port"]);
$host_ip = @gethostbyname($url2["host"]);
$fsock_timeout=20;//秒
if(($fsock = fsockopen($host_ip, 80, $errno, $errstr, $fsock_timeout)) < 0){
return false;
}

$request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : ""); 

$needChar = false; 

foreach($post_data as $key => $val) { 

$post_data2 .= ($needChar ? "&" : "") . urlencode($key) . "=" . urlencode($val); 
$needChar = true;
}
$in = "POST " . $request . " HTTP/1.0\r\n";
$in .= "Accept: */*\r\n";
$in .= "Host: " . $url2["host"] . "\r\n";
$in .= "User-Agent: Lowell-Agent\r\n";
$in .= "Content-type: application/x-www-form-urlencoded\r\n";
$in .= "Content-Length: " . strlen($post_data2) . "\r\n";
$in .= "Connection: Close\r\n\r\n";
$in .= $post_data2 . "\r\n\r\n";

unset($post_data2); 
if(!@fwrite($fsock, $in, strlen($in))){
fclose($fsock);
return false;
}
unset($in);

$out = ""; 
while($buff = fgets($fsock, 2048)){
$out .= $buff;
}

fclose($fsock); 
$pos = strpos($out, "\r\n\r\n");
$head = substr($out, 0, $pos);    //http head
$status = substr($head, 0, strpos($head, "\r\n"));    //http status line
$body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body
if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)){
if(intval($matches[1]) / 100 == 2){
return $body;
}else{
return false;
}
}else{
return false;
}
}

 

$post_data = array("name"=>"xd","sex"=>"man");
httpRequestPOST("http://localhost/post.php",$post_data);

 

socket写的顺序:


POST /post.php HTTP/1.0
Accept: */*
Host: localhost
User-Agent: Lowell-Agent
Content-type: application/x-www-form-urlencoded
Content-Length: 15
Connection: Close
name=xd&sex=man


普通POST的结果演示:

POST/?username=111&password=222HTTP/1.1
Host:127.0.0.1:8000
User-Agent:Mozilla/5.0(Windows;U;WindowsNT5.2;zh-CN;rv:1.9.0.1)Gecko/2008070208Firefox/3.0.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:zh-cn,zh;q=0.5
Accept-Encoding:gzip,deflate
Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive:300
Connection:keep-alive
Referer:http://127.0.0.1:8000/?username=111&password=222
Content-Type:application/x-www-form-urlencoded
Content-Length:25
username=111&password=222

 


GET的运行结果演示:

 

GET/?username=111&password=222HTTP/1.1
Host:127.0.0.1:8000
User-Agent:Mozilla/5.0(Windows;U;WindowsNT5.2;zh-CN;rv:1.9.0.1)Gecko/2008070208Firefox/3.0.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:zh-cn,zh;q=0.5
Accept-Encoding:gzip,deflate
Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive:300
Connection:keep-alive
Referer:http://127.0.0.1:8000/?username=1&password=2


POST文件上传的结果演示:

 

POST/?username=111&password=222HTTP/1.1
Host:127.0.0.1:8000
User-Agent:Mozilla/5.0(Windows;U;WindowsNT5.2;zh-CN;rv:1.9.0.1)Gecko/2008070208Firefox/3.0.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language:zh-cn,zh;q=0.5
Accept-Encoding:gzip,deflate
Accept-Charset:gb2312,utf-8;q=0.7,*;q=0.7
Keep-Alive:300
Connection:keep-alive
Referer:http://127.0.0.1:8000/?username=111&password=222
Content-Type:multipart/form-data;boundary=---------------------------23757983230932
Content-Length:1704
-----------------------------23757983230932
Content-Disposition:form-data;name="phototitle"
12
-----------------------------23757983230932

 

转自:http://blog.bigqi.com/read.php?157  

分享到:
评论

相关推荐

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

    在现代网络开发中,使用PHP进行Socket编程是一种常见的需求,尤其在模拟HTTP请求方面。本文将深入探讨如何使用PHP的Socket编程来模拟HTTP的GET和POST请求。我们将从一个PHP类开始,该类能够构建HTTP请求并处理响应,...

    PHP socket 模拟POST 请求实例代码

    以下是一个简单的PHP Socket模拟POST请求的示例代码: ```php function socket_post($url, $data, $referer='') { if (!is_array($data)) { return; } $data = http_build_query($data); $url = parse_url($...

    php-socket拟POST.doc

    在Web开发中,通常我们使用cURL或者file_get_contents函数发送HTTP请求,但当需要更底层、更灵活的网络通信时,socket提供了一个低级别的接口。通过socket,开发者可以直接与服务器进行数据交换,实现POST数据的发送...

    php实现模拟post请求用法实例

    本文介绍了PHP中模拟POST请求的三种方法:使用cURL、使用file_get_contents和使用fsockopen。每种方法都有自己的特点和适用场景。cURL是最常用的方法,因为它功能强大且易于使用;file_get_contents更适合简单的HTTP...

    php中用socket模拟http中post或者get提交数据的示例代码

    下面的代码展示了如何使用`socket`函数模拟POST和GET请求: 1. **创建socket连接**: 首先,我们需要解析目标URL以获取主机名和端口,然后使用`fsockopen()`函数建立TCP连接。如果无法打开连接,该函数返回错误...

    利用PHP fsockopen 模拟POST/GET传送数据的方法

    利用PHP的fsockopen函数可以模拟向其他网页或站点发送HTTP请求,具体来说,可以通过模拟POST和GET方法来传送数据。下面详细说明如何使用fsockopen函数来完成这一任务。 首先,要理解fsockopen函数是PHP中用于打开...

    php模拟POST提交的4种方法.pdf

    在PHP中,模拟POST提交是常见的任务,通常用于与服务器进行交互、自动化测试或执行API调用。以下四种方法展示了如何在PHP中实现POST提交: 1. **使用cURL库** cURL库是PHP中广泛使用的HTTP客户端,它允许开发者...

    php自定义类fsocket模拟post或get请求的方法

    本文实例讲述了php自定义类fsocket模拟post或get请求的方法。分享给大家供大家参考。具体如下: zsocket.class.php文件如下: &lt;?php class ZSocket { /* * Init */ private function _fsockopen&#40;$host, ...

    PHP中的POST&GET的应用.docx

    在PHP中,可以通过fsockopen函数创建一个socket连接来模拟POST请求。在示例代码中,我们看到`$ps`数组用于存储POST参数,然后通过`urlencode`函数编码键值对,并将它们添加到`$posts`字符串中。最后,使用`fputs`将...

    PHP实现模拟http请求的方法分析

    在PHP编程中,有时我们需要模拟HTTP请求,例如发送POST或GET数据到远程服务器,获取响应内容,或者测试API接口。本文将深入探讨如何使用PHP实现模拟HTTP请求,并分析其背后的原理和步骤。 首先,理解HTTP请求的基本...

    php fsockopen伪造post与get方法的详解

    在PHP编程中,fsockopen函数是用于打开一个网络连接或者一个Unix套接字连接的内置...通过本文的介绍和示例代码的分析,我们可以了解到如何使用fsockopen来模拟GET和POST请求,并且对HTTP请求的构造有了更深入的理解。

Global site tag (gtag.js) - Google Analytics