curl是最常用功能之一初始化句柄 $ch = curl_init(); post 传$data 1. 如果$data是字符串,则Content-Type是application/x-www-form-urlencoded。 2、如果$data是k=>v的数组,则Content-Type是multipart/form-data, 编码设置 $header = array(‘Content-Type:application/x-www-form-urlencoded;charset=utf8‘); curl_setopt($ch,CURLOPT_HTTPHEADER,$header); post方式 curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 返回值 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 超时 curl_setopt($ch, CURLOPT_TIMEOUT, 20); 执行 curl_exec($ch); 是否有异常 if (curl_errno($ch)) 关闭 curl_close($ch);
一个实例,post数据到某短信端口:
/** * $sender 发送人 *$reveivers 收信人手机号 数据格式 *$msg 短信内容 *sname 发送人姓名 */ function send($sender,$receivers,$msg,$sname){ $tos = ""; foreach ($receivers as $v) { //将收信人转为以‘,‘分割的字符串 $tos .= $v.","; } $userName = ‘hnxxx**‘; $pwd = ‘fuckwl***‘; $st = date(‘mdHis‘); $post_data = array () ;
$post_data[‘UserName‘] = $userName; $post_data[‘Key‘] = getKey($userName, $pwd, $st);
$post_data[‘Timestemp‘] = $st; $post_data[‘Content‘] = ‘【前缀】‘.$msg; $post_data[‘CharSet‘] = ‘utf-8‘; $post_data[‘Mobiles‘] = $tos; $url = ‘http://www.xxx.com:3070/Http_Service/SendSms‘; $o = "" ; foreach ( $post_data as $k => $v ) { $o .= "$k=".urlencode($v)."&" ; } $post_data = substr($o, 0, -1) ; $curl = curl_init(); $header = array(‘Content-Type:application/x-www-form-urlencoded;charset=utf8‘); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); if (curl_errno($curl)) { echo ‘Errno‘.curl_error($curl); } curl_close($curl); echo $result; echo $post_data; }
时间: 2024-10-22 03:23:57