您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php curl详解

2025/12/16 12:41:17发布13次查看
curl介绍curl 是一个利用url语法规定来传输文件和数据的工具,支持很多协议,如http、ftp、telnet等。最爽的是,php也支持 curl 库。本文将介绍 curl 的一些高级特性,以及在php中如何运用它
基本结构(1)初始化
curl_init()
(2)设置变量
curl_setopt()
(3)执行并获取结果
curl_exec()
(4)释放curl句柄
curl_close()
curl实现get和postget方式实现
//初始化$ch = curl_init();//设置选项,包括urlcurl_setopt($ch, curlopt_url, http://www.jb51.net);curl_setopt($ch, curlopt_returntransfer, 1);curl_setopt($ch, curlopt_header, 0);//执行并获取html文档内容$output = curl_exec($ch);//释放curl句柄curl_close($ch);//打印获得的数据print_r($output);
post方式实现
$url = http://localhost/web_services.php;$post_data = array (username => bob,key => 12345);$ch = curl_init();curl_setopt($ch, curlopt_url, $url);curl_setopt($ch, curlopt_returntransfer, 1);// post数据curl_setopt($ch, curlopt_post, 1);// post的变量curl_setopt($ch, curlopt_postfields, $post_data);$output = curl_exec($ch);curl_close($ch);//打印获得的数据print_r($output);
以上方式获取到的数据是json格式的,使用json_decode函数解释成数组。
$output_array = json_decode($output,true);
如果使用json_decode($output)解析的话,将会得到object类型的数据。
ubuntu下错误的解决
php fatal error: call to undefined function curl_init() in /var/www/protected/config/functions.php on line 132
在终端命令窗口键入
sudo apt-get install php5-curlsudo service apache restart
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product