登录  /  注册
首页 > php教程 > php手册 > 正文

php中curl get/post 例子 (包括https协议)

php中文网
发布: 2016-05-24 13:00:49
原创
899人浏览过

php中curl函数可以实现get与post操作,我们经常使用它来做一些人为模仿操作了,下面我来简单的介绍post与get的例子.

get 方法,代码如下:

$url = "http://www.phprm.com /index.php?a=b&c=d&e=f&g=".urlencode('王璐个人博客'); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    // 要求结果为字符串且输出到屏幕上 
curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 
curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
$output = curl_exec($ch); 
curl_close($ch); 
var_dump($output);
登录后复制

post 方法,代码如下:

$url = "http://www.phprm.com/index.php"; 
$params = "a=b&c=d&e=f&g=" . urlencode('王璐个人博客'); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上 
curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 
curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
//开源代码phprm.com 
curl_setopt($ch, CURLOPT_POST, 1);    // post 提交方式 
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
 
$output = curl_exec($ch); 
curl_close($ch); 
var_dump($output);
登录后复制

当请求https的数据时,会要求证书,这时候,加上下面这两个参数,规避ssl的证书检查,代码如下:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);// https请求 不验证证书和hosts.
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
登录后复制


永久链接:

转载随意!带上文章地址吧。

相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学