-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.php
76 lines (63 loc) · 1.45 KB
/
example.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* dHttp is library to work with Curl
* Example to use library
*/
use dHttp\Client;
include_once('src/Client.php');
include_once('src/Response.php');
$http = new dHttp\Client('http://website.com', [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1',
CURLOPT_TIMEOUT => 5,
CURLOPT_HEADER => true
]);
/*
* Simple get request
*/
$resp = $http->get();
// Get response code
$resp->getCode();
// Get response body
$resp->getBody();
// Get request errors
$resp->getErrors();
/*
* Simple post request
*/
$resp = $http->post([
'field1' => 'value1',
'field2' => 'value2',
]);
$resp->getRaw();
// Return response headers
$resp->getHeaders();
// Return a specific (text/html; charset=utf-8)
$resp->getHeader('Content-Type');
/**
* Another way of setting.
* Output response
*/
$http = new dHttp\Client();
$http->addOptions([CURLOPT_RETURNTRANSFER => false])
->setUserAgent('Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1')
->setCookie('/tmp/cookies.txt')
->setUrl('http://website.com')
->get();
/**
* Use multi curl
*/
$multi = new dHttp\Client();
$response_array = $multi->multi([
new dHttp\Client('http://website1.com'),
new dHttp\Client('http://website2.com', [
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:5.0.1) Gecko/20100101 Firefox/5.0.1',
CURLOPT_TIMEOUT => 5,
])
]);
foreach ($response_array as $item) {
$resp->getCode();
}
/**
* Get cURL version
*/
Client::v();