Skip to content

Commit db845a9

Browse files
BenjaminBenjamin
authored andcommitted
Adds switchable http clients with configuration options
1 parent 12d583a commit db845a9

18 files changed

+1741
-212
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ require 'autoload.php';
4040
Initialization
4141
---------------
4242

43-
After including the required files from the SDK, you need to initalize the ParseClient using your Parse API keys:
43+
After including the required files from the SDK, you need to initialize the ParseClient using your Parse API keys:
4444

4545
```php
4646
ParseClient::initialize( $app_id, $rest_key, $master_key );
@@ -63,6 +63,43 @@ For example if your parse server's url is `http://example.com:1337/parse` then y
6363

6464
`ParseClient::setServerURL('https://example.com:1337','parse');`
6565

66+
67+
Http Clients
68+
------------
69+
70+
This SDK has the ability to change the underlying http client at your convenience.
71+
The default is to use the curl http client if none is set, there is also a stream http client that can be used as well.
72+
73+
Setting the http client can be done as follows:
74+
```php
75+
// set curl http client (default if none set)
76+
ParseClient::setHttpClient(new ParseCurlHttpClient());
77+
78+
// set stream http client
79+
// ** requires 'allow_url_fopen' to be enabled in php.ini **
80+
ParseClient::setHttpClient(new ParseStreamHttpClient());
81+
```
82+
83+
If you have a need for an additional http client you can request one by opening an issue or by submitting a PR.
84+
85+
If you wish to build one yourself make sure your http client implements ```ParseHttpable``` for it be compatible with the SDK. Once you have a working http client that enhances the SDK feel free to submit it in a PR so we can look into adding it in.
86+
87+
88+
Alternate Certificate Authority File
89+
------------------------------------
90+
91+
It is possible that your local setup may not be able to verify with peers over SSL/TLS. This may especially be the case if you do not have control over your local installation, such as for shared hosting.
92+
93+
If this is the case you may need to specify a Certificate Authority bundle. You can download such a bundle from (http://curl.haxx.se/ca/cacert.pem)[http://curl.haxx.se/ca/cacert.pem] to use for this purpose. This one happens to be a Mozilla CA certificate store, you don't necessarily have to use this one but it's recommended.
94+
95+
Once you have your bundle you can set it as follows:
96+
```php
97+
// ** Use an Absolute path for your file! **
98+
// holds one or more certificates to verify the peer with
99+
ParseClient::setCAFile(__DIR__ . '/certs/cacert.pem');
100+
```
101+
102+
66103
Usage
67104
-----
68105

src/Parse/HttpClients/ParseCurl.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
/**
3+
* ParseCurl - Wrapper for abstracted curl usage
4+
*
5+
* @author Ben Friedman <[email protected]>
6+
*/
7+
8+
namespace Parse\HttpClients;
9+
10+
11+
use Parse\ParseException;
12+
13+
/**
14+
* Class ParseCurl
15+
* @package Parse\HttpClients
16+
*/
17+
class ParseCurl
18+
{
19+
/**
20+
* Curl handle
21+
* @var resource
22+
*/
23+
private $curl;
24+
25+
/**
26+
* Sets up a new curl instance internally if needed
27+
*/
28+
public function init()
29+
{
30+
if($this->curl === null) {
31+
$this->curl = curl_init();
32+
33+
}
34+
35+
}
36+
37+
/**
38+
* Executes this curl request
39+
*
40+
* @return mixed
41+
* @throws ParseException
42+
*/
43+
public function exec()
44+
{
45+
if(!isset($this->curl)) {
46+
throw new ParseException('You must call ParseCurl::init first');
47+
48+
}
49+
50+
return curl_exec($this->curl);
51+
}
52+
53+
/**
54+
* Sets a curl option
55+
*
56+
* @param int $option Option to set
57+
* @param mixed $value Value to set for this option
58+
* @throws ParseException
59+
*/
60+
public function setOption($option, $value)
61+
{
62+
if(!isset($this->curl)) {
63+
throw new ParseException('You must call ParseCurl::init first');
64+
65+
}
66+
67+
curl_setopt($this->curl, $option, $value);
68+
69+
}
70+
71+
/**
72+
* Sets multiple curl options
73+
*
74+
* @param array $options Array of options to set
75+
* @throws ParseException
76+
*/
77+
public function setOptionsArray($options)
78+
{
79+
if(!isset($this->curl)) {
80+
throw new ParseException('You must call ParseCurl::init first');
81+
82+
}
83+
84+
curl_setopt_array($this->curl, $options);
85+
86+
}
87+
88+
/**
89+
* Gets info for this curl handle
90+
*
91+
* @param int $info Constatnt for info to get
92+
* @return mixed
93+
* @throws ParseException
94+
*/
95+
public function getInfo($info)
96+
{
97+
if(!isset($this->curl)) {
98+
throw new ParseException('You must call ParseCurl::init first');
99+
100+
}
101+
102+
return curl_getinfo($this->curl, $info);
103+
104+
}
105+
106+
/**
107+
* Gets the curl error message
108+
*
109+
* @return string
110+
* @throws ParseException
111+
*/
112+
public function getError()
113+
{
114+
if(!isset($this->curl)) {
115+
throw new ParseException('You must call ParseCurl::init first');
116+
117+
}
118+
119+
return curl_error($this->curl);
120+
121+
}
122+
123+
/**
124+
* Gets the curl error code
125+
*
126+
* @return int
127+
* @throws ParseException
128+
*/
129+
public function getErrorCode()
130+
{
131+
if(!isset($this->curl)) {
132+
throw new ParseException('You must call ParseCurl::init first');
133+
134+
}
135+
136+
return curl_errno($this->curl);
137+
138+
}
139+
140+
/**
141+
* Closed our curl handle and disposes of it
142+
*/
143+
public function close()
144+
{
145+
if(!isset($this->curl)) {
146+
throw new ParseException('You must call ParseCurl::init first');
147+
148+
}
149+
150+
// close our handle
151+
curl_close($this->curl);
152+
153+
// unset our curl handle
154+
$this->curl = null;
155+
156+
}
157+
158+
}

0 commit comments

Comments
 (0)