37度2

日常搜集,解燃眉之急

PHP模拟post,get

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;
}
 
 
 
 
 
 
 
阅读更多
个人分类: PHP
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭