PHP CURL 实现 GET/POST数据
1: 使用Curl组件GET/POST数据
//Curl Get数据
function curl_get($url) {
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$data=curl_exec($ch);
curl_close($ch);
if ($data)
return$data;
else
returnfalse;
}
//Curl Post数据
function curl_post($url,$vars) {
$ch=curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data=curl_exec($ch);
curl_close($ch);
if ($data)
return$data;
else
returnfalse;
}
2: fsockopen手工GET/POST数据
//直接POST数据
function post_file($host,$file,$vars){
$fp=fsockopen($host,80,$errno,$errstr,10);
if (!$fp) {
echo"Socket Error: $errstr ($errno)\n";
returnfalse;
}
$post.="POST $file HTTP/1.1\r\n";
$post.="Host: $host\r\n";
$post.="User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$post.="Keep-Alive: 300\r\n";
$post.="Connection: keep-alive\r\n";
$post.="Referer: http://$host";
$post.="Content-Type: application/x-www-form-urlencoded\r\n";
$post.="Content-Length: ".strlen($vars) ."\r\n";
$post.=$vars;
$post.="\r\n\r\n";
fwrite($fp,$post);
while (!feof($fp)) {
$res=fread($fp,1024);
}
fclose($fp);
return$res;
}
//直接GET数据
function get_file($host,$file){
$fp=fsockopen($host,80,$errno,$errstr,10);
if (!$fp) {
echo"SocketError: $errstr ($errno)\n";
returnfalse;
}
$get="GET $file HTTP/1.1\r\n";
$get.="Host: $host\r\n";
$get.="User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$get.="Referer: http://$host\r\n";
$get.="Connection: Close\r\n\r\n";
fwrite($fp,$get);#p#分页标题#e#
while (!feof($fp)) {
echofread($fp,1024);
}
fclose($fp);
}
3:
$data= array ('foo'=>'bar','bar'=>'baz');
$data=http_build_query($data);
$context_options= array (
'http'=> array (
'method'=>'POST',
'header'=>"Content-type: application/x-www-form-urlencoded\r\n"
."Content-Length: ".strlen($data) ."\r\n",
'content'=>$data
)
);
$context=context_create_stream($context_options)
$fp=fopen($url,'r',false,$context);
//file_get_contents($url,false,$context);
————————————————————-
$url='http://www.awzsr.com/';
//目标来源页面
$referer='http://www.awzsr.com/';
//如果目标网站需要登录,可以在这里写入模拟的Cookie值
$cookie='';
//将$post_data中写入你所截取的数据包,可以使用Winsock Expert截取数据包
$post_data='uid=49940&tid=35&l=0&m=0&c=2';
//部分网站验证浏览器信息,可以在下面模拟浏览器,可以直接将USERAGENT复制到这里面
$useragent='';
//下面的内容禁止修改
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_COOKIE,$cookie);
curl_setopt($ch, CURLOPT_USERAGENT,$useragent);
$result = curl_exec($ch);
php socket 实现POST GET操作
<?php
/**
* socket 错误 或者http协议不对 都返回false
* 获得的字符串为空 就返回 空字符串
* 头部是200的 都返回NULL
* 否则返回http协议的body部分
*/
function httpRequest($url, $type = "GET", $post_data = NULL, $fsock_timeout = 20)
{
$http_info = array();
$url2 = parse_url($url);
$url2["path"] = ($url2["path"] == "" ? "/" : $url2["path"]);
$url2["port"] = !$url2["port"] ? 80 : $url2["port"];
$host_ip = @gethostbyname($url2["host"]);
$request = $url2["path"] . ($url2["query"] != "" ? "?" . $url2["query"] : "") . ($url2["fragment"] != "" ? "#" . $url2["fragment"] : "");
$fsock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if(!$fsock){
// trigger_error(socket_strerror(socket_last_error()));
return false;
}
/**
* connect host with timeout
*/
socket_set_nonblock($fsock);
@socket_connect($fsock, $host_ip, $url2["port"]);
$ret = socket_select($fd_read = array($fsock), $fd_write = array($fsock), $except = NULL, $fsock_timeout, 0);
if($ret != 1){
# trigger_error("connect error or timeout");
@socket_close($fsock);#p#分页标题#e#
return false;
}
if($type == "GET"){ // GET method
$in = "GET " . $request . " HTTP/1.0\r\n";
$in .= "Accept: */*\r\n";
$in .= "User-Agent: Lowell-Agent\r\n";
$in .= "Host: " . $url2["host"] . "\r\n";
$in .= "Connection: Close\r\n\r\n";
}else if($type == "POST"){ // POST method
// build post data
$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);
}else{ // unknowd method
exit;
}
if(!@socket_write($fsock, $in, strlen($in))){
socket_close($fsock);
return false;
}
unset($in);
socket_set_block($fsock);
@socket_set_option($fsock, SOL_SOCKET, SO_RCVTIMEO, array("sec" => $fsock_timeout, "usec" => 0));
// process response
$out = '';
while($buff = socket_read($fsock, 2048)){
$out .= $buff;
}
// finish socket
@socket_close($fsock);
if(!$out){
return '';
}
$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); //page body
if(preg_match("/^HTTP\/\d\.\d\s(\d{3,4})\s/", $status, $matches)){
if(intval($matches[1]) / 100 == 2){
return $body;
}else{
return NULL;
}
}else{
return false;
}
}
?>