/** * CURL模拟post请求 * @param 请求地址$url * @param 请求数据,数组形式 $data * @param 请求数据是否以json形式发送(1表示json形式,0表示非json形式) $json * @return 接口返回数据 调用:$res=curl($url,array('msg'=>'关机闹钟'),$json=1); */ function curl_post($url, $data = null, $json = false) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); if (!empty($data)) { if ($json && is_array($data)) { $data = json_encode($data); } curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); if ($json) { // 发送JSON数据 curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data), )); } } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); $errorno = curl_errno($curl); if ($errorno) { return array( 'errorno' => false, 'errmsg' => $errorno, ); } curl_close($curl); return json_decode($res, true); }