1. 程式人生 > >php curl那點事兒

php curl那點事兒

func utf8 格式 姓名 url orm peer div 是否

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; }

php curl那點事兒