colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
 * PhalApi_CUrl CURL请求类
 *
 * 通过curl实现的快捷方便的接口请求类
 * 
 * <br>示例:<br>
 * 
```
 *  // 失败时再重试2次
 *  $curl = new PhalApi_CUrl(2);
 *
 *  // GET
 *  $rs = $curl->get('http://phalapi.oschina.mopaas.com/Public/demo/?service=Default.Index');
 *
 *  // POST
 *  $data = array('username' => 'dogstar');
 *  $rs = $curl->post('http://phalapi.oschina.mopaas.com/Public/demo/?service=Default.Index', $data);
```
 *
 * @package     PhalApi\CUrl
 * @license     http://www.phalapi.net/license GPL 协议
 * @link        http://www.phalapi.net/
 * @author      dogstar <chanzonghuang@gmail.com> 2015-01-02
 */
 
class PhalApi_CUrl {
 
    /**
     * 最大重试次数
     */
    const MAX_RETRY_TIMES = 10;
 
    /**
     * @var int $retryTimes 超时重试次数;注意,此为失败重试的次数,即:总次数 = 1 + 重试次数
     */
    protected $retryTimes;
    
    protected $header = array();
    
    protected $option = array();
    
    /**
     * 设置请求头,后设置的会覆盖之前的设置
     *
     * @param array $header 传入键值对如:
```     
     * array(
     *     ['Accept'=>'text/html'],
     *     ['Connection'=>'keep-alive'],
     * )
```     
     *
     * @return $this
     */
    public function setHeader( $header )
    {
        $this->header = array_merge( $this->header, $header);
        return $this;
    }
    
    /**
     * 设置curl配置项
     * 1、后设置的会覆盖之前的设置
     * 2、开发者设置的会覆盖框架的设置
     * @param array $option 格式同上
     *
     * @return $this
     */
    public function setOption( $option )
    {
        $this->option = array_merge( $this->option, $option);
        return $this;
    }
 
    /**
     * @param int $retryTimes 超时重试次数,默认为1
     */
    public function __construct($retryTimes = 1) {
        $this->retryTimes = $retryTimes < static::MAX_RETRY_TIMES 
            ? $retryTimes : static::MAX_RETRY_TIMES;
    }
 
    /**
     * GET方式的请求
     * @param string $url 请求的链接
     * @param int $timeoutMs 超时设置,单位:毫秒
     * @return string 接口返回的内容,超时返回false
     */
    public function get($url, $timeoutMs = 3000) {
        return $this->request($url, array(), $timeoutMs);
    } 
 
    /**
     * POST方式的请求
     * @param string $url 请求的链接
     * @param array $data POST的数据
     * @param int $timeoutMs 超时设置,单位:毫秒
     * @return string 接口返回的内容,超时返回false
     */
    public function post($url, $data, $timeoutMs = 3000) {
        return $this->request($url, $data, $timeoutMs);
    }
    
    /**
     *
     * @return array
     */
    protected function getHeaders() {
        $arrHeaders = array();
        foreach ($this->header as $key => $val) {
            $arrHeaders[] = $key . ':' . $val;
        }
        return $arrHeaders;
    }
 
    /**
     * 统一接口请求
     * @param string $url 请求的链接
     * @param array $data POST的数据
     * @param int $timeoutMs 超时设置,单位:毫秒
     * @return string 接口返回的内容,超时返回false
     */
    protected function request($url, $data, $timeoutMs = 3000) {
        $options = array(
            CURLOPT_URL                 => $url,
            CURLOPT_RETURNTRANSFER      => TRUE,
            CURLOPT_HEADER              => 0,
            CURLOPT_CONNECTTIMEOUT_MS   => $timeoutMs,
            CURLOPT_HTTPHEADER          => $this->getHeaders(),
        );
 
        if (!empty($data)) {
            $options[CURLOPT_POST]          = 1;
            $options[CURLOPT_POSTFIELDS]    = $data;
        }
        
        $options = $this->option + $options;//$this->>option优先
        
        $ch = curl_init();
        curl_setopt_array( $ch, $options);
        $curRetryTimes = $this->retryTimes;
        do {
            $rs = curl_exec($ch);
            $curRetryTimes--;
        } while($rs === FALSE && $curRetryTimes >= 0);
 
        curl_close($ch);
 
        return $rs;
    }
}