关闭

PHP模拟post,get

776人阅读 评论(0) 收藏 举报
post调用
$URL = 'http://xx/xx/xx/'; //需要提交到的页面
//下面这段是要提交的数据
$post_data['email'] = $_POST['email'];
$post_data['password'] = $_POST['password'];
echo get_postData($URL,$post_data);


get调用
$URL = 'http://xx/xx/xx/?token='.$token; //需要提交到的页面
echo get_getData($URL);



function get_getData($URL){
$ch = curl_init($URL) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
$output = curl_exec($ch) ;
curl_close($fch) ;
return $output;
}




function get_postData($URL,$post_data){
$referrer="";
$URL_Info=parse_url($URL);
if($referrer==""){
    $referrer=$_SERVER["SCRIPT_URI"];
}
foreach ($post_data as $key=>$value){
    $values[]="$key=".urlencode($value);
}

$data_string=implode("&",$values);
if (!isset($URL_Info["port"])) {
$URL_Info["port"]=80;
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referrer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";
$request.="\n";
$request.=$data_string."\n";
}
$fp = fsockopen($URL_Info["host"], $URL_Info["port"]);
fputs($fp, $request);
$i = 0;
while(!feof($fp)) {
$result = fgets($fp, 1024);
$length = strlen($result);
$s1 = substr($result, 0, 1);
$s2 = substr($result, $length - 3, 1);
if($s1 == '{' && $s2 == '}')
$resultover = $result;
}

fclose($fp);
return $resultover;
}
 
 
 
 
 
 
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1250262次
    • 积分:14722
    • 等级:
    • 排名:第819名
    • 原创:420篇
    • 转载:31篇
    • 译文:0篇
    • 评论:81条
    文章分类
    最新评论