记录下 PHP使用post方式请求第三方接口获得结果
/*
* 请求第三方接口
* $data :请求参数
* $url :第三方接口地址
*/
function requests($data,$url)
{
$data = request_post($url, json_encode($data));
return json_decode($data, true);
}
/*
* post 发送JSON 格式数据
* @param $url string URL
* @param $data_string string 请求的具体内容
* @return array
*/
function request_post($url = '', $post_data = array(), $dataType = '')
{
if (empty($url) || empty($post_data)) {
return false;
}
$curlPost = $post_data;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Type:application/json',
'Content-Length: ' . strlen($curlPost)
)
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch);
return $data;
}