Heim >Backend-Entwicklung >PHP-Tutorial >php 中的 curl 函数
<?php //1. 初始化$curl = curl_init();//2. 配置相关参数信息 curl_setoptcurl_setopt($curl, 常量参数,可选参数);//3. 执行$res = curl_exec($curl);//4. 关闭 链接curl_close($curl);?>
具体的参数使用信息 请参阅:http://php.net/manual/zh/book.curl.php
<?php $this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));?>
/** * 普通函数传递方式(已做证书跳过) * 调用基础: * $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); }
$this->curl->debug();
$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 "<br>","cUrl Error:".curl_error($ch); } curl_close($ch); $obj = json_decode($output);
$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);