Re: php模拟post提交数据,可用来网站的采集,登陆等等

添加时间:14-10-14 所属分类:PHP工具与代码
  [code]<?php
// PHP POST数据的三种方法
// php有三种方法可以post数据,分别为Curl、socket、file_get_contents:


/**
* Socket版本
* 使用方法:
* $post_string = "app=socket&version=beta";
* request_by_socket('facebook.cn','/restServer.php',$post_string);
*/
function request_by_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30)
{
$socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout);
if (!$socket) die("$errstr($errno)");

fwrite($socket, "POST $remote_path HTTP/1.0\r\n");
fwrite($socket, "User-Agent: Socket Example\r\n");
fwrite($socket, "HOST: $remote_server\r\n");
fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($socket, "Content-length: " . (strlen($post_string) + 8) . '\r\n');
fwrite($socket, "Accept:*/*\r\n");
fwrite($socket, "\r\n");
fwrite($socket, "mypost=$post_string\r\n");
fwrite($socket, "\r\n");
$header = "";
while ($str = trim(fgets($socket, 4096))) {
$header .= $str;
}
$data = "";
while (!feof($socket)) {
$data .= fgets($socket, 4096);
}
return $data;
}



/**
* Curl版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_curl('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_curl($remote_server, $post_string)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");
$data = curl_exec($ch);
curl_close($ch);
return $data;
}


/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restServer.php',$post_string);
*/
function request_by_other($remote_server, $post_string)
{
$context = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded' .
'\r\n'.'User-Agent : Jimmy\'s POST Example beta' .
'\r\n'.'Content-length:' . strlen($post_string) + 8,
'content' => 'mypost=' . $post_string)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($remote_server, false, $stream_context);
return $data;
}

?>[/code]
  • 1
  • 2

前篇:php防止form重复提交的方法 后篇:退休校长带儿子为邻里修路15年 称看不得环境...
发表我的评论


推荐文章   印度演员阿米尔汗减肥视频播放两千万次...   清华附小校长肺腑之言:想让孩子成功,...   产假、痛经假最新规定 这些女职工福利6...   妈妈辞职为脑瘫致残女儿陪读七年 女儿考...   她失去双腿 却依然要挑战大自然
随机文章   php 用iconv()函数 将Unicode编码转换成...   PHP加解密迅雷,flashget快车,QQ旋风地...   PHP去除字符串尾部空格和小数点   PHP的简单文字过滤函数   用【ASP】【PHP】设为桌面快捷方式完整...
广告

其他推荐

因为本站所有内容均转载自其它媒体,本意为公众提供免费服务,但并不代表本网赞同其观点,也不能对其真实性验证负责,如稿件版权单位或个人不想在本网发布,请与我联系,本人会立即将其撤除,谢谢.联系方式:atseashawk@163.com QQ:99289555