php的curl实现get和post的代码


Posted in PHP onAugust 23, 2008

curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道就最常用的来说,是基于http的get和post方法。

代码实现:

1、http的get实现

$ch = curl_init("https://3water.com/") ; 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; 
$output = curl_exec($ch) ; 
$fh = fopen("out.html", 'w') ; 
fwrite($fh, $output) ; 
fclose($fh) ;

2、http的post实现
//extract data from the post 
extract($_POST) ; 
//set POST variables 
$url = 'https://3water.com/get-post.php' ; 
$fields = array( 
'lname'=>urlencode($last_name) , 
'fname'=>urlencode($first_name) , 
'title'=>urlencode($title) , 
'company'=>urlencode($institution) , 
'age'=>urlencode($age) , 
'email'=>urlencode($email) , 
'phone'=>urlencode($phone) 
); 
//url-ify the data for the POST 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&' ; } 
rtrim($fields_string ,'&') ; 
//open connection 
$ch = curl_init() ; 
//set the url, number of POST vars, POST data 
curl_setopt($ch, CURLOPT_URL,$url) ; 
curl_setopt($ch, CURLOPT_POST,count($fields)) ; 
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string) ; 
//execute post 
$result = curl_exec($ch) ; 
//close connection 
curl_close($ch) ;
PHP 相关文章推荐
PHP VS ASP
Oct 09 PHP
用PHP生成静态HTML速度快类库
Mar 18 PHP
php之字符串变相相减的代码
Mar 19 PHP
php中支持多种编码的中文字符串截取函数!
Mar 20 PHP
认识并使用PHP超级全局变量
Jan 26 PHP
PHP实现域名whois查询的代码(数据源万网、新网)
Feb 22 PHP
php INI配置文件的解析实现分析
Jan 04 PHP
php 批量生成html,txt文件的实现代码
Jun 26 PHP
利用PHP fsockopen 模拟POST/GET传送数据的方法
Sep 22 PHP
thinkPHP基于ajax实现的菜单与分页示例
Jul 12 PHP
PHP自定义函数获取URL中一级域名的方法
Aug 23 PHP
laravel利用中间件做防非法登录和权限控制示例
Oct 21 PHP
PHP Smarty生成EXCEL文档的代码
Aug 23 #PHP
php过滤危险html代码
Aug 18 #PHP
php htmlentities和htmlspecialchars 的区别
Aug 18 #PHP
php magic_quotes_gpc的一点认识与分析
Aug 18 #PHP
php数组应用之比较两个时间的相减排序
Aug 18 #PHP
php中的数组操作函数整理
Aug 18 #PHP
PHP去除数组中重复的元素并按键名排序函数
Aug 18 #PHP
You might like
第二节 对象模型 [2]
2006/10/09 PHP
php使用COPY函数更新配置文件的方法
2015/06/18 PHP
centos7上编译安装php7以php-fpm方式连接apache
2018/11/08 PHP
PHP 自动加载类原理与用法实例分析
2020/04/14 PHP
关于JavaScript的with 语句的使用方法
2011/05/09 Javascript
JavaScript中getUTCSeconds()方法的使用详解
2015/06/11 Javascript
json对象转为字符串,当做参数传递时加密解密的实现方法
2016/06/29 Javascript
javascript history对象详解
2017/02/09 Javascript
Angular2学习教程之组件中的DOM操作详解
2017/05/28 Javascript
js 奇葩技巧之隐藏代码
2017/08/11 Javascript
JS实现的全排列组合算法示例
2017/10/09 Javascript
AngularJS实现的简单拖拽功能示例
2018/01/02 Javascript
vue: WebStorm设置快速编译运行的方法
2018/10/18 Javascript
layui form表单提交之后重新加载数据表格的方法
2019/09/11 Javascript
jQuery实现简单三级联动效果
2020/09/05 jQuery
nuxt.js添加环境变量,区分项目打包环境操作
2020/11/06 Javascript
Python文件操作,open读写文件,追加文本内容实例
2016/12/14 Python
浅谈Python的垃圾回收机制
2016/12/17 Python
详解django实现自定义manage命令的扩展
2019/08/13 Python
详解Python self 参数
2019/08/30 Python
Python 文件操作之读取文件(read),文件指针与写入文件(write),文件打开方式示例
2019/09/29 Python
Django使用list对单个或者多个字段求values值实例
2020/03/31 Python
Python sublime安装及配置过程详解
2020/06/29 Python
在终端启动Python时报错的解决方案
2020/11/20 Python
Python中Qslider控件实操详解
2021/02/20 Python
Css3圆角边框制作代码
2015/11/18 HTML / CSS
css3 边框、背景、文本效果的实现代码
2018/03/21 HTML / CSS
利用简洁的图片预加载组件提升html5移动页面的用户体验
2016/03/11 HTML / CSS
Ralph Lauren意大利官方网站:时尚界最负盛名的品牌之一
2018/10/18 全球购物
美国领先的眼镜和太阳镜在线零售商:Glasses.com
2019/08/26 全球购物
TCP/IP模型的分界线
2012/12/01 面试题
六一儿童节活动策划方案
2014/01/27 职场文书
认真学习保证书
2015/02/26 职场文书
反腐倡廉观后感
2015/06/08 职场文书
导游词之南京汤山温泉
2019/11/26 职场文书
Go中的条件语句Switch示例详解
2021/08/23 Golang