码迷,mamicode.com
首页 > Web开发 > 详细

php curl那点事儿

时间:2017-09-23 15:25:08      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:数据   function   amp   set   执行   func   数据格式   each   www.   

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那点事儿

标签:数据   function   amp   set   执行   func   数据格式   each   www.   

原文地址:http://www.cnblogs.com/thingk/p/6776153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!