Home > Backend Development > PHP Tutorial > php 中的 curl 函数

php 中的 curl 函数

WBOY
Release: 2016-06-23 13:35:48
Original
805 people have browsed it

1. curl的安装配置:


Linux下的安装:
http://php.net/manual/zh/curl.setup.php
windows下的安装:
开启:
extension=php_curl.dll //注意对应版本的 dll 文件

2. curl 使用步骤

<?php //1. 初始化$curl = curl_init();//2. 配置相关参数信息 curl_setoptcurl_setopt($curl, 常量参数,可选参数);//3. 执行$res = curl_exec($curl);//4. 关闭 链接curl_close($curl);?>
Copy after login

具体的参数使用信息 请参阅:http://php.net/manual/zh/book.curl.php

3. 部分使用案例

3.1 使用https 时 需要对证书认证进行过滤

<?php $this->curl->options(array(CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false));?>
Copy after login

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);    }
Copy after login

3.3 CI框架中 CURL的调试 方式

$this->curl->debug();
Copy after login

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 "<br>","cUrl Error:".curl_error($ch);      }      curl_close($ch);      $obj = json_decode($output);
Copy after login

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);
Copy after login









Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template