模拟POST提交 http/https
function curl_post($url,$data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl);
return $output;
}
$data=[];
$data['abc']='6666';
$data['bbb']='2222';
$res=curl_post("http://192.168.1.16:10011/4.php",$data);
var_dump($res);
模拟POST josn格式 http/https
$data=[];
$data['abc']='大师傅似的';
$data['bbb']='撒旦';
$res=https_curl_json("http://192.168.1.16:10011/4.php",$data,'json');
var_dump($res);
function https_curl_json($url,$data,$type=''){
if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);获取
$headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
$data=json_encode($data, JSON_UNESCAPED_UNICODE);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if(isset($headers)){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
}
$output = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl);
return $output;
}
带证书的ssl 请求
function curl_post_ssl($url, $vars, $second=30,$aHeader=array())
{
$ch = curl_init();
//超时时间
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//以下两种方式需选择一种
//第一种方法,cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
//curl_setopt($ch,CURLOPT_SSLCERT,BASE_PATH.'/apiclient_cert.pem');
//默认格式为PEM,可以注释
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
//curl_setopt($ch,CURLOPT_SSLKEY,BASE_PATH.'/apiclient_key.pem');
//第二种方式,两个文件合成一个.pem文件
//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
$data = curl_exec($ch);
if($data){
curl_close($ch);
file_put_contents(BASE_PATH.'qian.txt',date("Y-n-j G:i:s").$data."\r\n",FILE_APPEND);
return $data;
}
else {
$error = curl_errno($ch);
file_put_contents(BASE_PATH.'qian_no.txt','发送失败'.$error."\r\n",FILE_APPEND);
//echo "发送失败, errorCode:$error\n请联系管理员";
curl_close($ch);
return false;
}
}
$aHeader=array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
$res=curl_post_ssl("https://api.weixin.qq.com/",$data,60,$aHeader);
发送x-www-form-urlencoded格式数据
$data=[];
$data['abc']='6666';
$data['bbb']='2222';
postx("http://www.qq.com/asd.php",$data);
function postx($url,$data)
{
$post_data = http_build_query($data);//重点
////application/x-www-form-urlencoded 需要使用aaa=555&bbb=666 格式
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);//不抓取头部信息。只返回数据
curl_setopt($curl, CURLOPT_TIMEOUT, 60);//超时设置
//curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1表示不返回bool值
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));//重点
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$resdata = curl_exec($curl);
if (curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
return $resdata;
}
//异步执行url访问
function ts2($url='')
{
$url='https://666.666.cn/index/ts?ss='.rand(1111,9999);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_NOSIGNAL, 1); //启用时忽略所有的curl传递给php进行的信号
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 100); //设置cURL允许执行的最长毫秒数
$data = curl_exec($curl);
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
curl_close($curl);
return $data;
}
$a=explode(' ', microtime());
echo date('Y-n-j G:i:s',$a[1]).'-'.$a[0].'<br>';
ts2();
echo '<br>';
$b=explode(' ', microtime());
echo date('Y-n-j G:i:s',$b[1]).'-'.$b[0].'<br>';