脚本宝典收集整理的这篇文章主要介绍了PHP socket 模拟POST 请求实例代码,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我们用到最多的模拟POST请求几乎都是使用PHP curl来实现了,没考虑到PHP socket也可以实现,今天看到朋友写了一文章,下面我来给大家分享一下PHP socket模拟POST请求实例。
以前模拟post请求俺都用PHP curl扩展实现来着,没想过PHP socket也可以实现。最近翻了下相关资料才发现原来没有那么高深,只是以前一直没有完全理解post的原理和本质而已,其实就是发送给目的程序一个标志为post的协议串如下:
POST /目的程序url HTTP/1.1
username=fengdingbo&password=js-code.cc //post发送的数据
我想大家对表单的post方法提交数据应该是最熟悉不过了,例如我们想把用户名和密码发送给某个@L_126_12@的时候,填写好相应的input框,点击提交按钮,最后把这个表单发送到action程序的就是以上数据。知道了这一点我想就不难了
这时候我们只需要用PHP的socket打开一个端口,例如80端口,把以上信息利用这个端口发送给目的程序就行了。
官方给的原型:
下边是人类的理解:
fsockopen(主机名称,端口号,错误号的&变量,错误提示的&变量,超时时间) 主机名称就是你需要发送数据的目的地; 端口号就是这个目的程序会在哪个端口等着你的数据; 错误号的&变量,这个是如果建立socket不成功的时候返回的错误编号; 错误提示的&变量,是错误的时候返回的错误提示信息; 超时时间,就是POST数据之后如果对方没有回应信息,等待的最长时间。
如果不出意外(你正确的设置fsockopen()函数的参数)的话,一个socket通道现在已经打开了,我们下一步需要做的就是,通过这个打开的通道把post请求协议发给目的程序,这时候可以使用fwrITe或者fputs函数中的任意一个,把post的请求格式发给fsockopen()打开的资源句柄,这时候一个伟大的socket模拟的post请求就诞生了。
brush:PHp;"> <>PHP /** * SOCKET扩展函数 * @copyright (c) 2013 * @author Qiufeng* @link //www.js-code.COM * @version 1.0 */ /**
- Post Request
- @param string $url
- @param array $data
- @param string $referer
- @return array
*/
if ( ! function_exists('socket_post'))
{
function socket_post($url,$data,$referer='')
{
if( ! is_array($data))
{
return;
}$data = http_build_query($data);
$url = parse_url($url);if ( ! isset($url['scheme']) || $url['scheme'] != 'http')
{
die('Error: Only HTTP request are supported !');
}$host = $url['host'];
$path = isset($url['path']) ? $url['path'] : '/';// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host,80,$errno,$errstr,30);if ($fp)
{
// send the request headers:
$length = strlen($data);
$POST = <<<HEADER
POST {$path} HTTP/1.1
Accept: text/plain,text/htML
Referer: {$referer}
Accept-Language: zh-CN,zh;q=0.8
Content-Type: application/x-www-form-urlencodem
Cookie: token=value; pub_cookietime=2592000; pub_sauth1=value; pub_sauth2=value
User-Agent: Mozilla/5.0 (X11; Linux i686) Applewebkit/537.17 (KHTML,like Gecko) Chrome/24.0.1312.56 Safari/537.17
Host: {$host}
Content-Length: {$length}
Pragma: no-cache
Cache-Control: no-cache
Connection: closern
{$data}
HEADER;
fwrite($fp,$POST);
$result = '';
while(!feof($fp))
{
// receive the results of the request
$result .= fread($fp,512);
}
}
else
{
return array(
'status' => 'error','error' => "$errstr ($errno)"
);
}// close the socket connection:
fclose($fp);// split the result header From the content
$result = explode("rnrn",$result,2);// return as structured array:
return array(
'status' => 'ok','header' => isset($result[0]) ? $result[0] : '','content' => isset($result[1]) ? $result[1] : ''
);
}
}print_r(socket_post('//www.js-code.com/',array('name='=>'qiufeng','password'=>md5('www.js-code.com'))));
/ e.g: socket_post('//www.js-code.com','password'=>md5('js-code.cc'))); /
/ End of file socket_helper.PHP /
以上是脚本宝典为你收集整理的PHP socket 模拟POST 请求实例代码全部内容,希望文章能够帮你解决PHP socket 模拟POST 请求实例代码所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。