Post 接口使用实例

POST 使用实例

<?php
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json;');

$api = 'https://xxxx.cn/api.php'; //地址
$json = array(	//参数
	'key'=>'参数'
);
$value = http_build_query($json);

echo PostCurl($api,$value);

function PostCurl($url, $data){    //Curl POST
    $ch = curl_init();//初使化init方法    
    curl_setopt($ch, CURLOPT_URL, $url);//指定URL   
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设定请求后返回结果    
    curl_setopt($ch, CURLOPT_POST, 1);//声明使用POST方式来进行发送    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//发送什么数据呢    
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//忽略证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//忽略host     
    curl_setopt($ch, CURLOPT_HEADER, 0);//忽略header头信息   
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置超时时间
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
	curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); //强制协议为1.0
	curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); //强制使用IPV4协议解析域名
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}