模拟post提交 数据
<?php
function imitate_post($url,$data){
$ch = curl_init ();
// print_r($ch); //资源类型
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 );//正规的HTTP POST
curl_setopt ( $ch, CURLOPT_HEADER, 0 );//设置header头,是否包含header头,(一般不用)
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); //returntransfer 返回转换,要求结果为json字符串(或者false)
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec( $ch );
curl_close ( $ch );
//print_r($return);
return $return;
}
////////////////////////////////
$url = "http://www.cms.com/post.php";//这里换成你服务器的地址
$info = array (
'name' => 'hao' ,
'password' => '123'
);
$json = imitate_post($url ,$info);
//返回的json数据 {"access_token":"asdfaxcd132","expire":7200}
//转换成数组
$arr = json_decode($json,true);
//var_dump($arr);
echo 1;
//模拟post提交数据给微信
//就是curl 1初始化,2设置选项,3执行,4关闭.
===============本地接收测试
<?php
function mylog111($str){
file_put_contents('./mylog.txt',$str);
}
$data = json_encode($_POST); //获取全部post数据,转化成json,写入txt文件中,很多内容.content加上s
//$data = file_get_contents("php://input");
mylog111($data);
$arr = array('access_token'=>'asdfaxcd132','expire'=>7200);
echo json_encode($arr);
======================模拟get提交.就是 file_get_contents
<?php
function imitate_get($url){
$data = file_get_contents($url);
var_dump($data);
}
$url = 'http://www.cms.com/get.php';
imitate_get($url);
==========get.php
<html>
<body>
hi, iam the superman;
</body>
</html>