分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

php curl那点事儿

发布时间:2023-09-06 01:13责任编辑:郭大石关键词:url
curl是最常用功能之一
初始化句柄$ch = curl_init();post 传$data1. 如果$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那点事儿

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved