curl

/** Get提交获取数据
 * @desc 获取access_token
 * @return String access_token
 */ 
function getAccessToken(){ 
    $AppId = '1232assad13213123'
    $AppSecret = '2312312321adss3123213'
    $getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret
        $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $getUrl); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURL_SSLVERSION_SSL, 2); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    $data = curl_exec($ch); 
    $response = json_decode($data); 
    return $response->access_token; 
}
 
/** Post提交获取数据
 * @desc 实现天气内容回复
 */ 
public function testWeixin(){ 
    $access_token = $this->getAccessToken(); 
    $customMessageSendUrl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token;
    $description = '今天天气的详细信息(从第三方获取)。';
    $url = 'http://weather.com/';
    $picurl = 'http://weather.com/';
    $postDataArr = array(
        'touser'=>'OPENID',
        'msgtype'=>'news',
        'news'=>array(
            'articles'=>array(
                'title'=>'当天天气',
                'description'=>$description,
                'url'=>$url,
                'picurl'=>$picurl,
                ),
            ),
        ); 
    $postJosnData = json_encode($postDataArr); 
    $ch = curl_init($customMessageSendUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);   
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    $data = curl_exec($ch);
    var_dump($data); 
}



/**
 * @description: 封装CURL扩展
 * @date: 2014-07-28 16:04
 */
 
/**
 * @编码规范
 * @class 类名首字母大写,类名为多个单词, 每个大字首字母大写 eg: class Curl , class CurlPage
 * @variable 变量名小写, 变量名为多个单词, 每个单词小写,使用下划线_分割 eg: $curl_result
 * @function 函数名与类名规则相同 eg: function SendRequest
 * @params 函数形参规则与变量名相同
 * @class-variable 成员变量,以下划线结尾,多个单词使用下划线分隔. eg: private $host_name_
 */
/**
 * @要求
 *
 */
class Curl{
 
    /**
     * @请求的host
     */
    private $host_;
 
    /**
     * @curl 句柄
     */
    private $ch_;
 
    /**
     * @超时限制时间
     */
    const time_=5;
    /**
     * @请求的设置
     */
    private $options_;
 
    /**
     * @保存请求头信息
     */
    private $request_header_;
 
    /**
     * @保存响应头信息
     */
    private $response_header_;
 
    /**
     * @body_ 用于保存curl请求返回的结果
     */
    private $body_;
 
    /**
     * @读取cookie
     */
    private $cookie_file_;
 
    /**
     * @写入cookie
     */
    private $cookie_jar_;
 
    /**
     * @todo proxy
     * @构造函数,初始化CURL回话
     */
    public function Start($url){
        $this->ch_ = curl_init($url);
        curl_setopt($this->ch_, CURLOPT_HEADER, 1);
        curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 1 );
    }
 
    /**
     * @返回响应头
     */
    public function ResponseHeader($url){
        if (!function_exists('http_parse_headers')) {
            function http_parse_headers ($raw_headers){
                $headers = array();
                foreach (explode("\n", $raw_headers) as $i => $h) {
                    $h = explode(':', $h, 2);
                    if (isset($h[1])) {
                        if(!isset($headers[$h[0]])) {
                            $headers[$h[0]] = trim($h[1]);
                        } else if(is_array($headers[$h[0]])) {
                            $tmp = array_merge($headers[$h[0]],array(trim($h[1])));
                            $headers[$h[0]] = $tmp;
                        } else {
                            $tmp = array_merge(array($headers[$h[0]]),array(trim($h[1])));
                            $headers[$h[0]] = $tmp;
                        }
                    }
                }
                return $headers;
            }
        }
        $this->Start($url);
        curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
        $this->body_=$this->Execx();
        $header_size = curl_getinfo($this->ch_, CURLINFO_HEADER_SIZE);
        $this->response_header_ = substr($this->body_, $start = 0, $offset = $header_size);
        $this->response_header_ = http_parse_headers($this->response_header_);
        print_r($this->response_header_);
        return $this->Close($this->body_);
    }
    /**
     * @读取cookie
     */
    public function LoadCookie($url,$cookie_file){
        $this->Start($url);
        curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
        curl_setopt($this->ch_, CURLOPT_COOKIEFILE , $cookie_file);
        $this->body_=$this->Execx();
        return $this->Close($this->body_);
    }
 
    /**
     * @写入cookie
     */
    public function SaveCookie($url){
        $this->Start($url);
        curl_setopt($this->ch_, CURLOPT_COOKIE, 1);
        curl_setopt($this->ch_, CURLOPT_COOKIEFILE ,'cookie.txt');
        curl_setopt($this->ch_, CURLOPT_COOKIEJAR , 'cookie.txt');
        $this->body_=$this->Execx();
        return $this->Close($this->body_);
    }
 
    /**
     * @设置HEADER
     */
 
    public function SetHeader($headers = null){
        if (is_array($headers) && count($headers) > 0) {
            curl_setopt($this->ch_, CURLOPT_HTTPHEADER, $headers);
        }
    }
 
    /**
     * @GET请求
     */
    public function Get($url, array $params = array()) {
        if ($params) {
            if (strpos($url, '?')) {
                $url .= "&".http_build_query($params);
            }
            else {
                $url .= "?".http_build_query($params);
            }
        }
        $this->Start($url);
        curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
        if (strpos($url, 'https') === 0) {
            curl_setopt($this->ch_, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
        }
        $this->body_=$this->Execx();
        return $this->Close($this->body_);
    }
 
    /**
     * @POST请求
     */
    public function Post($url, array $params = array()) {
        $this->Start($url);
        curl_setopt($this->ch_, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($this->ch_, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($this->ch_, CURLOPT_POST, true);
        curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
        if ($params) {
            curl_setopt($this->ch_, CURLOPT_POSTFIELDS, http_build_query($params));
        }
        $this->body_=$this->Execx();
        return $this->Close($this->body_);
    }
 
    /**
     * @tips: google http head 方法
     */
    public function Head($url, array $params = array()) {
        $this->Start($url);
        curl_setopt($this->ch_, CURLOPT_TIMEOUT, Curl::time_);
        curl_setopt($this->ch_, CURLOPT_RETURNTRANSFER , 0);
        curl_setOpt($this->ch_,CURLOPT_NOBODY, true);
        $this->body_=$this->Execx();
        return $this->Close($this->body_);
    }
 
    /**
     * @执行CURL会话
     */
    public function Execx(){
        return curl_exec($this->ch_);
    }
 
    /**
     * @关闭CURL句柄
     */
    public function Close($body_){
        if ($body_ === false) {
            echo "CURL Error: " . curl_error($body_);
            return false;
        }
        curl_close($this->ch_);
        return $body_;
    }
}

Curl是一个强大的工具,简单来说,它就像一个“命令行版的浏览器”,但比浏览器更轻量、更灵活,专注于数据传输而非页面渲染,因此成为开发者调试网络请求、自动化数据交互的利器。其基本语法为`curl [选项] [URL]` [^2]。 在C++中,curl库可用于与HTTPS集成等网络请求操作。随着对curl库在不同协议中的应用和网络功能的封装的深入讨论,能不断强化对HTTPS协议工作原理等概念的理解,并进一步提升网络请求的安全性和效率 [^1]。 在PHP里,CURL函数库中最重要的函数是`curl_setopt()`,它可以通过设定CURL函数库定义的选项来定制HTTP请求。其使用方法为`bool curl_setopt (int ch, string option, mixed value)`,此函数将为一个CURL会话设置选项,`option`参数是想要的设置,`value`是这个选项给定的值 [^3]。 此外,在PHP中还能使用curl模拟referer、伪造来源ip和refer,例如给定了一个IP相关的数组`$arr_1 = array("218","218","66","66","218","218","60","60","202","204","66","66","66","59","61","60","222","221","66","59","60","60","66","218","218","62","63","64","66","66","122","211");` [^4]。 ### 示例代码 - **基本的Curl命令示例(命令行)** ```bash curl https://example.com ``` 此命令会向`https://example.com`发送一个基本的HTTP请求,并返回响应内容。 - **PHP中使用Curl的示例** ```php // 初始化一个CURL会话 $ch = curl_init(); // 设置URL和其他选项 curl_setopt($ch, CURLOPT_URL, "https://example.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 执行CURL会话 $response = curl_exec($ch); // 关闭CURL会话 curl_close($ch); // 输出响应内容 echo $response; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值