curl PHP 脚本例子
2015-09-20 18:59
821 查看
上网查了写资料写了个自己需要的PHP Curl函数,调用的是百度API获取数据:function getData($add) {
$post_data = array( 'ak=kkkk', 'callback=renderOption', 'output=json', 'city=XXX, 'address='.$add ); //传输的参数拼装
//print_r($post_data);
$post_data = implode('&',$post_data);
$url='http://api.map.baidu.com/geocoder/v2/?'; //路由拼装,你需要的参数就不需要再放到这里,上面拼装好就行。
//下面是curl的一些初始化,这里看PHP手册。
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch); //执行curl
$result = ob_get_contents() ; // 获取内容
ob_end_clean();
return json_decode($result)->result->location;
}
$post_data = array( 'ak=kkkk', 'callback=renderOption', 'output=json', 'city=XXX, 'address='.$add ); //传输的参数拼装
//print_r($post_data);
$post_data = implode('&',$post_data);
$url='http://api.map.baidu.com/geocoder/v2/?'; //路由拼装,你需要的参数就不需要再放到这里,上面拼装好就行。
//下面是curl的一些初始化,这里看PHP手册。
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
ob_start();
curl_exec($ch); //执行curl
$result = ob_get_contents() ; // 获取内容
ob_end_clean();
return json_decode($result)->result->location;
}
相关文章推荐
- 深入PHP使用技巧之变量
- 使用php导入excel文件
- php判断数值是否是素数算法
- mkyaffs2image编译
- yaffs2文件镜像制作工具yaffs2image
- yaffs2文件系统
- 安卓ContentProvider的理论知识
- PHP内核探索:新垃圾回收机制说明
- getAttribute()与getParameter的区别
- php用空格代替标点符号
- PHP的文件加载机制到底是什么目录
- php传参方式1--ajax
- 访问wamp的PHPadmin时遇到Forbidden You don't have permission to access / on this server. You don't have per
- php 设计模式
- ThinkPHP实战之登录
- php Hash Table(二) Hash函数
- php基础------私有字段取值赋值
- php Hash Table(一) Hash Table的结构
- PHPExcel 读excel
- getParameter