forked from ornicar/php-github-api
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCurl.php
More file actions
130 lines (115 loc) · 4.36 KB
/
Curl.php
File metadata and controls
130 lines (115 loc) · 4.36 KB
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
<?php
/**
* Performs requests on GitHub API. API documentation should be self-explanatory.
*
* @author Thibault Duplessis <thibault.duplessis at gmail dot com>
* @license MIT License
*/
class Github_HttpClient_Curl extends Github_HttpClient
{
/**
* Send a request to the server, receive a response
*
* @param string $url Request url
* @param array $parameters Parameters
* @param string $httpMethod HTTP method to use
* @param array $options Request options
*
* @return string HTTP response
*/
public function doRequest($url, array $parameters = array(), $httpMethod = 'GET', array $options = array())
{
$curlOptions = array();
if ($options['login'] || $options['auth_method'] == Github_Client::OAUTH_ACCESS_TOKEN) {
switch ($options['auth_method']) {
case Github_Client::AUTH_HTTP_PASSWORD:
$curlOptions += array(
CURLOPT_USERPWD => $options['login'].':'.$options['secret'],
);
break;
case Github_Client::AUTH_HTTP_TOKEN:
$curlOptions += array(
CURLOPT_USERPWD => $options['login'].'/token:'.$options['secret'],
);
break;
case Github_Client::OAUTH_ACCESS_TOKEN:
$curlOptions += array(
CURLOPT_HTTPHEADER => array('Authorization: token '.$options['secret']),
);
break;
case Github_Client::AUTH_URL_TOKEN:
default:
$parameters = array_merge(array(
'login' => $options['login'],
'token' => $options['secret']
), $parameters);
break;
}
}
if (!empty($parameters)) {
$queryString = utf8_encode(http_build_query($parameters, '', '&'));
if ('GET' === $httpMethod) {
$url .= '?'.$queryString;
} else {
$curlOptions += array(
CURLOPT_POSTFIELDS => json_encode($parameters),
);
}
}
$curlValue = true;
switch($httpMethod) {
case 'GET':
$curlMethod = CURLOPT_HTTPGET;
break;
case 'POST':
$curlMethod = CURLOPT_POST;
break;
case 'HEAD':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "HEAD";
break;
case 'PUT':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "PUT";
break;
case 'DELETE':
$curlMethod = CURLOPT_CUSTOMREQUEST;
$curlValue = "DELETE";
break;
case 'PATCH':
// since PATCH is new the end points accept as POST
$curlMethod = CURLOPT_POST;
break;
default:
throw new Github_HttpClient_Exception('Method currently not supported');
}
$curlOptions += array(
CURLOPT_URL => $url,
CURLOPT_PORT => $options['http_port'],
CURLOPT_USERAGENT => $options['user_agent'],
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => $options['timeout'],
$curlMethod => $curlValue
);
$response = $this->doCurlCall($curlOptions);
if (!in_array($response['headers']['http_code'], array(0, 200, 201, 204))) {
throw new Github_HttpClient_Exception(null, (int) $response['headers']['http_code']);
}
if ($response['errorNumber'] != '') {
throw new Github_HttpClient_Exception('error '.$response['errorNumber']);
}
return $response['response'];
}
protected function doCurlCall(array $curlOptions)
{
$curl = curl_init();
curl_setopt_array($curl, $curlOptions);
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
curl_close($curl);
return compact('response', 'headers', 'errorNumber', 'errorMessage');
}
}