-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathACurlOptions.php
53 lines (53 loc) · 1.71 KB
/
ACurlOptions.php
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
<?php
/**
* Holds a list of options for ACurl.
* This allows easier access to CURLOPT constants, for example:
* <pre>
* $curl->options->ssl_verifypeer = true;
* </pre>
* is the same as:
* <pre>
* curl_setopt($curl->getHandle(),CURLOPT_SSL_VERIFYPEER,true);
* </pre>
* The properties are case insensitive and are automatically turned into the relevant
* constant names.
* The options are then applied to the ACurl object's curl handler by calling
* the {@link applyTo()} method.
* @author Charles Pick
* @package packages.curl
*/
class ACurlOptions extends CAttributeCollection {
/**
* Applies the options to the specified ACurl object.
* @param ACurl $curl The ACurl object to apply the options to
*/
public function applyTo(ACurl $curl) {
foreach($this as $key => $value) {
$constant = constant($this->getConstantName($key));
curl_setopt($curl->getHandle(), $constant, $value);
}
}
/**
* Returns the curl constant name for the given property name.
* e.g.
* <pre>
* echo $curl->options->getConstantName("userAgent"); // outputs "CURLOPT_USERAGENT"
* echo $curl->options->getConstantName("ssl_verifypeer"); // outputs "CURLOPT_SSL_VERIFYPEER"
* </pre>
* @param string $name The name of the property
* @return string the name of the constant
*/
public function getConstantName($name) {
return "CURLOPT_".strtoupper($name);
}
/**
* Adds a http header
* @param string $key the header name to add
* @param string $value the header value
*/
public function addHeader($key, $value) {
$headers = ($existing = $this->itemAt("httpHeader")) ? $existing : array();
$headers[] = $key.": ".$value;
$this->add("httpHeader",$headers);
}
}