不用form怎么post数据

数据传输是用户交互中最重要的环节,下面收集了几个数据传输的方法,作为记录(未测试,在使用之前需要测试,如果后面我测试了,会对已测试项进行标注)

一. 网址传递

<a href=”test.php?id=3&name=mike”>next</a>

可用 $_GET['id'] 和$_GET['name']访问GET 的数据。
二. Cookie 传递
1、 设置Cookie
简单的:

SetCookie("MyCookie", "Value of MyCookie"); 

带失效时间的:

SetCookie("WithExpire", "Expire in 1 hour", time()+3600);//3600秒=1小时

什么都有的: 

SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".phpuser.com", 1);

如果要设置同名的多个Cookie,要用数组,方法是: 

SetCookie("CookieArray[0]", "Value 1"); 
SetCookie("CookieArray[1]", "Value 2");

2、 接收和处理Cookie

echo $_COOKIE[‘MyCookie’]; 
echo $_COOKIE[‘CookieArray[0]’]; 
echo count($_COOKIE[‘CookieArray’]); 

3、删除Cookie 
要删除一个已经存在的Cookie,有两个办法: 
一个办法是调用只带有name参数的SetCookie,那么名为这个name的Cookie 将被从关系户机上删掉;
另一个办法是设置Cookie的失效时间为time()或time()-1,那么这个Cookie在这个页面的浏览完之后就被删除了(其实是失效了)。 
要注意的是,当一个Cookie被删除时,它的值在当前页在仍然有效的。
三. Session传递
test1.php 

<? 
session_start(); 
session_register("count"); 
echo $count=0; 
?> 

test2.php 

<? 
session_start(); 
echo $count++; 
?>

四.ajax post

jQuery(function($)
{
$.ajax({
 type: 'post',
 url: 'xxx.php',
 data: strPost,
 dataType: 'json',
 success: function(data){
 }
});
});

五.curl post

exm1:

$ch = curl_init();
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)";   
$header = array('Accept-Language: zh-cn','Connection: Keep-Alive','Cache-Control: no-cache'); 
curl_setopt($ch, CURLOPT_REFERER, "http://www.xxx.com");  
curl_setopt($ch, CURLOPT_URL, "http://www.xxx/login/login.php"); 
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);   
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);    
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);   
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);     
$strPOST = "url=/home/&email=xxx@sohu.com&password=xxx";
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $strPOST); 
$result = curl_exec($ch); 

exm2:

function cevin_http_open($url, $conf = array())   
{   
    if(!function_exists('curl_init') or !is_array($conf))  return FALSE;   
   
    $post = '';   
    $purl = parse_url($url);   
   
    $arr = array(   
        'post' => FALSE,   
        'return' => TRUE,   
        'cookie' => 'C:/cookie.txt',);   
    $arr = array_merge($arr, $conf);   
    $ch = curl_init();   
   
    if($purl['scheme'] == 'https')   
    {   
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);   
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);   
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);   
    }   
   
    curl_setopt($ch, CURLOPT_URL, $url);   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);   
    curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);   
    curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);   
   
    if($arr['post'] != FALSE)   
    {   
        curl_setopt($ch, CURL_POST, TRUE);   
        if(is_array($arr['post']))   
        {   
            $post = http_build_query($arr['post']);   
        } else {   
            $post = $arr['post'];   
        }   
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);   
    }   
   
    $result = curl_exec($ch);   
    curl_close($ch);   
   
    return $result;   
}
$postfield="userName=test&year=2008&passWord=123456&Submit=%CC%E1%BD%BB";    //curl post的数据格式
$post = 'aa=ddd&ee=d'; 
echo cevin_http_open('http://localhost/ret.php',array('post'=>$postfield)); //post 过去的目标页http://localhost/ret.php

ret.php

<?php
if($_POST)
print_r($_POST);
?>

 

六.fsockopen post

function post($data)
{
     //创建socket连接 
     $fp = fsockopen("www.xxx.com",80,$errno,$errstr,10) or exit($errstr."--->".$errno); 
     //构造post请求的头 
     $header = "POST /xxx.aspx HTTP/1.1\r\n"; 
     $header .= "Host:127.0.0.1\r\n"; 
     $header .= "Content-Type: application/x-www-form-urlencoded\r\n"; 
     $header .= "Content-Length: ".strlen($data)."\r\n"; 
     $header .= "Connection: Close\r\n\r\n";
     //添加post的字符串 
     $header .= $data."\r\n"; 
     //发送post的数据 
     fputs($fp,$header); 
     $inheader = 1; 
     while (!feof($fp)) {
             $line = fgets($fp,128); //去除请求包的头只显示页面的返回数据 
             if ($inheader && ($line == "\n" || $line == "\r\n")) {
                 $inheader = 0; 
             } 
             if ($inheader == 0) { 
                 $ret=$line; 
             } 
     } 
    fclose($fp); 
    return $ret;
}
$str = "Name=".urlencode($name)."&sex=".urlencode($sex)."&Age=".urlencode($age)."&Phone=".urlencode($phone)."&IDCard=".urlencode($idcard)."&Time=".urlencode($time)."&Email=".urlencode($email); 
$UserID = post($str); //返回值

七.include框架

include('get.php');

来源:http://bbs.csdn.net/topics/330033219

posted @ 2017-07-14 17:11  Yiven  阅读(2157)  评论(0编辑  收藏  举报