您好,欢迎访问一九零五行业门户网

php 中的 curl 函数

1. curl的安装配置:
linux下的安装:
http://php.net/manual/zh/curl.setup.php
windows下的安装:
开启:
extension=php_curl.dll //注意对应版本的 dll 文件
2. curl 使用步骤

具体的参数使用信息 请参阅:http://php.net/manual/zh/book.curl.php
3. 部分使用案例
3.1 使用https 时 需要对证书认证进行过滤
curl->options(array(curlopt_ssl_verifypeer => false, curlopt_ssl_verifyhost => false));?>
3.2 使用 body(一般对于java 的认证方式) 传输接口时 使用下面的方法
/** * 普通函数传递方式(已做证书跳过) * 调用基础: * $url = https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accesstoken; * $res = json_decode($this->httpget($url)); */private function httpget($url) { $curl = curl_init(); curl_setopt($curl, curlopt_returntransfer, true); curl_setopt($curl, curlopt_timeout, 500); curl_setopt($curl, curlopt_ssl_verifypeer, false); curl_setopt($curl, curlopt_ssl_verifyhost, false); curl_setopt($curl, curlopt_url, $url); $res = curl_exec($curl); curl_close($curl); return $res; } /** * java 的body 认证传输方式 * 调用基础: * $result = list($return_code, $return_content) = $this->http_post_data($url, json_encode(array(key=>$pass))); * $res = json_decode($return_content); * $return_code = $res->rspmsg; */ public function http_post_data($url, $data_string) { $ch = curl_init(); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_postfields, $data_string); curl_setopt($ch, curlopt_httpheader, array( 'content-type: application/json; charset=utf-8', 'content-length: ' . strlen($data_string)) ); ob_start(); curl_exec($ch); $return_content = ob_get_contents(); ob_end_clean(); $return_code = curl_getinfo($ch, curlinfo_http_code); return array($return_code, $return_content); }
3.3 ci框架中 curl的调试 方式
$this->curl->debug();
3.4 curl 模拟文件上传 (微信声音文件上传)
$soundurl = '你本地声音路径的绝对地址 如:/data/sound/ins.amr'; //初始化 $ch = curl_init(); //文件路径地址 $furl = @.$soundurl; $post_data = array ( media => $furl ); //提交文件地址 $url = http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=.$token.&type=voice; //设置变量 curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_returntransfer, 1);//执行结果是否被返回,0是返回,1是不返回 curl_setopt($ch, curlopt_postfields, $post_data); //执行并获取结果 $output = curl_exec($ch); if($outopt === false){ echo
,curl error:.curl_error($ch); } curl_close($ch); $obj = json_decode($output);
3.5 声音文件的本地 下载
$soundfile = $this->curl->simple_get(http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=.$token.&media_id=.$soundid); $soundurl = ./sound/.$id..amr; file_put_contents($soundurl, $soundfile);
其它类似信息

推荐信息