PHP模拟post,get

来源:互联网 发布:位面淘宝txt全集下载 编辑:程序博客网 时间:2024/05/19 13:22
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;
}
 
 
 
 
 
 
 
原创粉丝点击