commit | author | age
|
2207d6
|
1 |
# PHP Curl Class |
W |
2 |
|
|
3 |
This library provides an object-oriented wrapper of the PHP cURL extension. |
|
4 |
|
|
5 |
If you have questions or problems with installation or usage [create an Issue](https://github.com/php-mod/curl/issues). |
|
6 |
|
|
7 |
## Installation |
|
8 |
|
|
9 |
In order to install this library via composer run the following command in the console: |
|
10 |
|
|
11 |
```sh |
|
12 |
composer require curl/curl |
|
13 |
``` |
|
14 |
|
|
15 |
or add the package manually to your composer.json file in the require section: |
|
16 |
|
|
17 |
```json |
|
18 |
"curl/curl": "^1.5" |
|
19 |
``` |
|
20 |
|
|
21 |
## Usage examples |
|
22 |
|
|
23 |
```php |
|
24 |
$curl = new Curl\Curl(); |
|
25 |
$curl->get('http://www.example.com/'); |
|
26 |
``` |
|
27 |
|
|
28 |
```php |
|
29 |
$curl = new Curl\Curl(); |
|
30 |
$curl->get('http://www.example.com/search', array( |
|
31 |
'q' => 'keyword', |
|
32 |
)); |
|
33 |
``` |
|
34 |
|
|
35 |
```php |
|
36 |
$curl = new Curl\Curl(); |
|
37 |
$curl->post('http://www.example.com/login/', array( |
|
38 |
'username' => 'myusername', |
|
39 |
'password' => 'mypassword', |
|
40 |
)); |
|
41 |
``` |
|
42 |
|
|
43 |
```php |
|
44 |
$curl = new Curl\Curl(); |
|
45 |
$curl->setBasicAuthentication('username', 'password'); |
|
46 |
$curl->setUserAgent(''); |
|
47 |
$curl->setReferrer(''); |
|
48 |
$curl->setHeader('X-Requested-With', 'XMLHttpRequest'); |
|
49 |
$curl->setCookie('key', 'value'); |
|
50 |
$curl->get('http://www.example.com/'); |
|
51 |
|
|
52 |
if ($curl->error) { |
|
53 |
echo $curl->error_code; |
|
54 |
} |
|
55 |
else { |
|
56 |
echo $curl->response; |
|
57 |
} |
|
58 |
|
|
59 |
var_dump($curl->request_headers); |
|
60 |
var_dump($curl->response_headers); |
|
61 |
``` |
|
62 |
|
|
63 |
```php |
|
64 |
$curl = new Curl\Curl(); |
|
65 |
$curl->setOpt(CURLOPT_RETURNTRANSFER, TRUE); |
|
66 |
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, FALSE); |
|
67 |
$curl->get('https://encrypted.example.com/'); |
|
68 |
``` |
|
69 |
|
|
70 |
```php |
|
71 |
$curl = new Curl\Curl(); |
|
72 |
$curl->put('http://api.example.com/user/', array( |
|
73 |
'first_name' => 'Zach', |
|
74 |
'last_name' => 'Borboa', |
|
75 |
)); |
|
76 |
``` |
|
77 |
|
|
78 |
```php |
|
79 |
$curl = new Curl\Curl(); |
|
80 |
$curl->patch('http://api.example.com/profile/', array( |
|
81 |
'image' => '@path/to/file.jpg', |
|
82 |
)); |
|
83 |
``` |
|
84 |
|
|
85 |
```php |
|
86 |
$curl = new Curl\Curl(); |
|
87 |
$curl->delete('http://api.example.com/user/', array( |
|
88 |
'id' => '1234', |
|
89 |
)); |
|
90 |
``` |
|
91 |
|
|
92 |
```php |
|
93 |
$curl->close(); |
|
94 |
``` |
|
95 |
|
|
96 |
```php |
|
97 |
// Example access to curl object. |
|
98 |
curl_set_opt($curl->curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1'); |
|
99 |
curl_close($curl->curl); |
|
100 |
``` |
|
101 |
|
|
102 |
```php |
|
103 |
// Example of downloading a file or any other content |
|
104 |
$curl = new Curl\Curl(); |
|
105 |
// open the file where the request response should be written |
|
106 |
$file_handle = fopen($target_file, 'w+'); |
|
107 |
// pass it to the curl resource |
|
108 |
$curl->setOpt(CURLOPT_FILE, $file_handle); |
|
109 |
// do any type of request |
|
110 |
$curl->get('https://github.com'); |
|
111 |
// disable writing to file |
|
112 |
$curl->setOpt(CURLOPT_FILE, null); |
|
113 |
// close the file for writing |
|
114 |
fclose($file_handle); |
|
115 |
``` |
|
116 |
|
|
117 |
|
|
118 |
## Testing |
|
119 |
|
|
120 |
In order to test the library: |
|
121 |
|
|
122 |
1. Create a fork |
|
123 |
2. Clone the fork to your machine |
|
124 |
3. Install the depencies `composer install` |
|
125 |
4. Run the unit tests `./vendor/bin/phpunit tests` |