Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
wp-http-client
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wpdesk
wp-http-client
Commits
2a9679ea
Commit
2a9679ea
authored
5 years ago
by
Marcin Kolanko
Browse files
Options
Downloads
Patches
Plain Diff
code format
parent
92828006
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!3
Devel
Pipeline
#5086
failed with stages
in 1 minute and 46 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Curl/CurlClient.php
+82
-73
82 additions, 73 deletions
src/Curl/CurlClient.php
with
82 additions
and
73 deletions
src/Curl/CurlClient.php
+
82
−
73
View file @
2a9679ea
...
@@ -5,8 +5,8 @@ namespace iFirmaVendor\WPDesk\HttpClient\Curl;
...
@@ -5,8 +5,8 @@ namespace iFirmaVendor\WPDesk\HttpClient\Curl;
use
iFirmaVendor\WPDesk\HttpClient\HttpClient
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClient
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClientResponse
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClientResponse
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException
;
class
CurlClient
implements
\iFirmaVendor\WPDesk\HttpClient\HttpClient
{
class
CurlClient
implements
\iFirmaVendor\WPDesk\HttpClient\HttpClient
{
/** @var resource */
/** @var resource */
private
$curlResource
;
private
$curlResource
;
/** @var string|null|boolean */
/** @var string|null|boolean */
...
@@ -15,69 +15,72 @@ class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
...
@@ -15,69 +15,72 @@ class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
private
$httpResponseCode
;
private
$httpResponseCode
;
/** @var array */
/** @var array */
private
$headers
=
array
();
private
$headers
=
array
();
/**
/**
* @param string $url
* @param string $url
* @param string $body
* @param string $body
* @param array $headers
* @param array $headers
* @param int $timeOut
* @param int $timeOut
*
* @return HttpClientResponse
* @return HttpClientResponse
* @throws HttpClientRequestException
* @throws HttpClientRequestException
*/
*/
public
function
get
(
$url
,
$body
,
array
$headers
,
$timeOut
)
public
function
get
(
$url
,
$body
,
array
$headers
,
$timeOut
)
{
{
return
$this
->
send
(
$url
,
'GET'
,
$body
,
$headers
,
$timeOut
);
return
$this
->
send
(
$url
,
'GET'
,
$body
,
$headers
,
$timeOut
);
}
}
/**
/**
* @param string $url
* @param string $url
* @param string $method
* @param string $method
* @param string $body
* @param string $body
* @param array $headers
* @param array $headers
* @param int $timeOut
* @param int $timeOut
*
* @return HttpClientResponse
* @return HttpClientResponse
* @throws HttpClientRequestException
* @throws HttpClientRequestException
*/
*/
public
function
send
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
public
function
send
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
{
{
$this
->
initResource
();
$this
->
initResource
();
try
{
try
{
$this
->
prepareConnection
(
$url
,
$method
,
$body
,
$headers
,
$timeOut
);
$this
->
prepareConnection
(
$url
,
$method
,
$body
,
$headers
,
$timeOut
);
$this
->
sendRequest
();
$this
->
sendRequest
();
$this
->
throwExceptionIfError
();
$this
->
throwExceptionIfError
();
$this
->
closeConnection
();
$this
->
closeConnection
();
return
$this
->
prepareResponse
();
return
$this
->
prepareResponse
();
}
catch
(
\Exception
$e
)
{
}
catch
(
\Exception
$e
)
{
$this
->
closeConnection
();
$this
->
closeConnection
();
if
(
$e
instanceof
\iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException
)
{
if
(
$e
instanceof
\iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException
)
{
throw
$e
;
throw
$e
;
}
}
throw
\iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory
::
createDefaultException
(
$e
->
getMessage
(),
$e
->
getCode
(),
$e
);
throw
\iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory
::
createDefaultException
(
$e
->
getMessage
(),
$e
->
getCode
(),
$e
);
}
}
}
}
private
function
initResource
()
{
private
function
initResource
()
{
$this
->
curlResource
=
\curl_init
();
$this
->
curlResource
=
\curl_init
();
}
}
/**
/**
* Opens a new curl connection.
* Opens a new curl connection.
*
*
* @param string $url The endpoint to send the request to.
* @param string $url
The endpoint to send the request to.
* @param string $method The request method.
* @param string $method
The request method.
* @param string $body The body of the request.
* @param string $body
The body of the request.
* @param array $headers The request headers.
* @param array
$headers The request headers.
* @param int $timeOut The timeout in seconds for the request.
* @param int
$timeOut The timeout in seconds for the request.
*/
*/
private
function
prepareConnection
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
private
function
prepareConnection
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
{
{
$options
=
[
$options
=
[
\CURLOPT_CUSTOMREQUEST
=>
$method
,
\CURLOPT_CUSTOMREQUEST
=>
$method
,
\CURLOPT_HTTPHEADER
=>
$this
->
compileRequestHeaders
(
$headers
),
\CURLOPT_HTTPHEADER
=>
$this
->
compileRequestHeaders
(
$headers
),
\CURLOPT_URL
=>
$url
,
\CURLOPT_URL
=>
$url
,
\CURLOPT_CONNECTTIMEOUT
=>
10
,
\CURLOPT_CONNECTTIMEOUT
=>
10
,
\CURLOPT_TIMEOUT
=>
$timeOut
,
\CURLOPT_TIMEOUT
=>
$timeOut
,
\CURLOPT_RETURNTRANSFER
=>
\true
,
\CURLOPT_RETURNTRANSFER
=>
\true
,
// Return response as string
// Return response as string
\CURLOPT_HEADER
=>
\false
,
\CURLOPT_HEADER
=>
\false
,
// Enable header processing
// Enable header processing
\CURLOPT_SSL_VERIFYHOST
=>
2
,
\CURLOPT_SSL_VERIFYHOST
=>
2
,
\CURLOPT_SSL_VERIFYPEER
=>
\true
,
\CURLOPT_SSL_VERIFYPEER
=>
\true
,
...
@@ -88,11 +91,12 @@ class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
...
@@ -88,11 +91,12 @@ class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
return
$len
;
return
$len
;
}
}
];
];
if
(
$method
!==
"GET"
)
{
if
(
$method
!==
"GET"
)
{
$options
[
\CURLOPT_POSTFIELDS
]
=
$body
;
$options
[
\CURLOPT_POSTFIELDS
]
=
$body
;
}
}
\curl_setopt_array
(
$this
->
curlResource
,
$options
);
\curl_setopt_array
(
$this
->
curlResource
,
$options
);
}
}
/**
/**
* Compiles the request headers into a curl-friendly format.
* Compiles the request headers into a curl-friendly format.
*
*
...
@@ -100,94 +104,99 @@ class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
...
@@ -100,94 +104,99 @@ class CurlClient implements \iFirmaVendor\WPDesk\HttpClient\HttpClient
*
*
* @return array
* @return array
*/
*/
private
function
compileRequestHeaders
(
array
$headers
)
private
function
compileRequestHeaders
(
array
$headers
)
{
{
$return
=
[];
$return
=
[];
foreach
(
$headers
as
$key
=>
$value
)
{
foreach
(
$headers
as
$key
=>
$value
)
{
$return
[]
=
$key
.
': '
.
$value
;
$return
[]
=
$key
.
': '
.
$value
;
}
}
return
$return
;
return
$return
;
}
}
/**
/**
* Send the request and get the raw response from curl
* Send the request and get the raw response from curl
*/
*/
private
function
sendRequest
()
private
function
sendRequest
()
{
{
$this
->
rawResponse
=
\curl_exec
(
$this
->
curlResource
);
$this
->
rawResponse
=
\curl_exec
(
$this
->
curlResource
);
$this
->
httpResponseCode
=
$this
->
getHttpResponseCode
();
$this
->
httpResponseCode
=
$this
->
getHttpResponseCode
();
}
}
/** @return int */
/** @return int */
private
function
getHttpResponseCode
()
private
function
getHttpResponseCode
()
{
{
return
\intval
(
\curl_getinfo
(
$this
->
curlResource
,
\CURLINFO_HTTP_CODE
)
);
return
\intval
(
\curl_getinfo
(
$this
->
curlResource
,
\CURLINFO_HTTP_CODE
));
}
}
private
function
throwExceptionIfError
()
{
private
function
throwExceptionIfError
()
{
$errorNumber
=
\curl_errno
(
$this
->
curlResource
);
$errorNumber
=
\curl_errno
(
$this
->
curlResource
);
if
(
$errorNumber
===
0
)
{
if
(
$errorNumber
===
0
)
{
return
;
return
;
}
}
$errorMessage
=
\curl_error
(
$this
->
curlResource
);
$errorMessage
=
\curl_error
(
$this
->
curlResource
);
throw
\iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory
::
createCurlException
(
$errorMessage
,
$errorNumber
);
throw
\iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory
::
createCurlException
(
$errorMessage
,
$errorNumber
);
}
}
/**
/**
* Closes an existing curl connection
* Closes an existing curl connection
*/
*/
private
function
closeConnection
()
private
function
closeConnection
()
{
{
\curl_close
(
$this
->
curlResource
);
\curl_close
(
$this
->
curlResource
);
$this
->
curlResource
=
null
;
$this
->
curlResource
=
null
;
}
}
private
function
prepareResponse
()
{
private
function
prepareResponse
()
{
list
(
$rawHeaders
,
$rawBody
)
=
$this
->
extractResponseHeadersAndBody
();
list
(
$rawHeaders
,
$rawBody
)
=
$this
->
extractResponseHeadersAndBody
();
return
new
\iFirmaVendor\WPDesk\HttpClient\HttpClientResponse
(
$rawHeaders
,
$rawBody
,
$this
->
httpResponseCode
);
return
new
\iFirmaVendor\WPDesk\HttpClient\HttpClientResponse
(
$rawHeaders
,
$rawBody
,
$this
->
httpResponseCode
);
}
}
/**
/**
* Extracts the headers and the body into a two-part array
* Extracts the headers and the body into a two-part array
*
*
* @return array
* @return array
*/
*/
private
function
extractResponseHeadersAndBody
()
private
function
extractResponseHeadersAndBody
()
{
{
$rawBody
=
\trim
(
$this
->
rawResponse
);
$raw
Body
=
\trim
(
$this
->
rawResponse
);
$raw
Headers
=
\trim
(
implode
(
"
\r\n
"
,
$this
->
headers
)
);
$rawHeaders
=
\trim
(
implode
(
"
\r\n
"
,
$this
->
headers
));
return
[
$rawHeaders
,
$rawBody
];
return
[
$rawHeaders
,
$rawBody
];
}
}
/**
/**
* @param string $url
* @param string $url
* @param string $body
* @param string $body
* @param array $headers
* @param array $headers
* @param int $timeOut
* @param int $timeOut
*
* @return HttpClientResponse
* @return HttpClientResponse
* @throws HttpClientRequestException
* @throws HttpClientRequestException
*/
*/
public
function
post
(
$url
,
$body
,
array
$headers
,
$timeOut
)
public
function
post
(
$url
,
$body
,
array
$headers
,
$timeOut
)
{
{
return
$this
->
send
(
$url
,
'POST'
,
$body
,
$headers
,
$timeOut
);
return
$this
->
send
(
$url
,
'POST'
,
$body
,
$headers
,
$timeOut
);
}
}
/**
/**
* @param string $url
* @param string $url
* @param string $body
* @param string $body
* @param array $headers
* @param array $headers
* @param int $timeOut
* @param int $timeOut
*
* @return HttpClientResponse
* @return HttpClientResponse
* @throws HttpClientRequestException
* @throws HttpClientRequestException
*/
*/
public
function
delete
(
$url
,
$body
,
array
$headers
,
$timeOut
)
public
function
delete
(
$url
,
$body
,
array
$headers
,
$timeOut
)
{
{
return
$this
->
send
(
$url
,
'DELETE'
,
$body
,
$headers
,
$timeOut
);
return
$this
->
send
(
$url
,
'DELETE'
,
$body
,
$headers
,
$timeOut
);
}
}
/**
/**
* @param string $url
* @param string $url
* @param string $body
* @param string $body
* @param array $headers
* @param array $headers
* @param int $timeOut
* @param int $timeOut
*
* @return HttpClientResponse
* @return HttpClientResponse
* @throws HttpClientRequestException
* @throws HttpClientRequestException
*/
*/
public
function
put
(
$url
,
$body
,
array
$headers
,
$timeOut
)
public
function
put
(
$url
,
$body
,
array
$headers
,
$timeOut
)
{
{
return
$this
->
send
(
$url
,
'PUT'
,
$body
,
$headers
,
$timeOut
);
return
$this
->
send
(
$url
,
'PUT'
,
$body
,
$headers
,
$timeOut
);
}
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment