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
Issue boards
Milestones
Code
Merge requests
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
wpdesk
wp-http-client
Merge requests
!3
Devel
Code
Review changes
Check out branch
Download
Patches
Plain diff
Expand sidebar
Merged
Devel
devel
into
master
Overview
0
Commits
3
Pipelines
1
Changes
1
Merged
Krzysztof Dyszczyk
requested to merge
devel
into
master
5 years ago
Overview
0
Commits
3
Pipelines
1
Changes
1
0
0
Merge request reports
Viewing commit
1fd01c91
Prev
Next
Show latest version
1 file
+
188
−
201
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
1fd01c91
fixed issue with headers
· 1fd01c91
Marcin Kolanko
authored
5 years ago
src/Curl/CurlClient.php
+
188
−
201
View file @ 1fd01c91
Edit in single-file editor
Open in Web IDE
<?php
<?php
namespace
WPDesk\HttpClient\Curl
;
namespace
iFirmaVendor\
WPDesk\HttpClient\Curl
;
use
WPDesk\HttpClient\HttpClient
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClient
;
use
WPDesk\HttpClient\HttpClientResponse
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClientResponse
;
use
WPDesk\HttpClient\HttpClientRequestException
;
use
iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException
;
class
CurlClient
implements
\iFirmaVendor\WPDesk\HttpClient\HttpClient
class
CurlClient
implements
HttpClient
{
{
/** @var resource */
/** @var resource */
private
$curlResource
;
private
$curlResource
;
/** @var string|null|boolean */
/** @var string|null|boolean */
private
$rawResponse
;
private
$rawResponse
;
/** @var int */
private
$httpResponseCode
;
/** @var int */
/** @var array */
private
$httpResponseCode
;
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 $method
* @param string $url
* @param string $body
* @param string $method
* @param array $headers
* @param string $body
* @param int $timeOut
* @param array $headers
* @return HttpClientResponse
* @param int $timeOut
* @throws HttpClientRequestException
* @return HttpClientResponse
*/
* @throws HttpClientRequestException
public
function
send
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
*/
{
public
function
send
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
$this
->
initResource
();
{
try
{
$this
->
initResource
();
$this
->
prepareConnection
(
$url
,
$method
,
$body
,
$headers
,
$timeOut
);
try
{
$this
->
sendRequest
();
$this
->
prepareConnection
(
$url
,
$method
,
$body
,
$headers
,
$timeOut
);
$this
->
throwExceptionIfError
();
$this
->
sendRequest
();
$this
->
closeConnection
();
$this
->
throwExceptionIfError
();
return
$this
->
prepareResponse
();
$this
->
closeConnection
();
}
catch
(
\Exception
$e
)
{
$this
->
closeConnection
();
return
$this
->
prepareResponse
();
if
(
$e
instanceof
\iFirmaVendor\WPDesk\HttpClient\HttpClientRequestException
)
{
throw
$e
;
}
catch
(
\Exception
$e
)
{
}
$this
->
closeConnection
();
throw
\iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory
::
createDefaultException
(
$e
->
getMessage
(),
$e
->
getCode
(),
$e
);
if
(
$e
instanceof
HttpClientRequestException
)
{
}
throw
$e
;
}
}
private
function
initResource
()
throw
CurlExceptionFactory
::
createDefaultException
(
$e
->
getMessage
(),
$e
->
getCode
(),
$e
);
{
}
$this
->
curlResource
=
\curl_init
();
}
}
/**
private
function
initResource
()
* Opens a new curl connection.
{
*
$this
->
curlResource
=
curl_init
();
* @param string $url The endpoint to send the request to.
}
* @param string $method The request method.
* @param string $body The body of the request.
/**
* @param array $headers The request headers.
* Opens a new curl connection.
* @param int $timeOut The timeout in seconds for the request.
*
*/
* @param string $url The endpoint to send the request to.
private
function
prepareConnection
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
* @param string $method The request method.
{
* @param string $body The body of the request.
* @param array $headers The request headers.
$options
=
[
* @param int $timeOut The timeout in seconds for the request.
\CURLOPT_CUSTOMREQUEST
=>
$method
,
*/
\CURLOPT_HTTPHEADER
=>
$this
->
compileRequestHeaders
(
$headers
),
private
function
prepareConnection
(
$url
,
$method
,
$body
,
array
$headers
,
$timeOut
)
\CURLOPT_URL
=>
$url
,
{
\CURLOPT_CONNECTTIMEOUT
=>
10
,
$options
=
[
\CURLOPT_TIMEOUT
=>
$timeOut
,
CURLOPT_CUSTOMREQUEST
=>
$method
,
\CURLOPT_RETURNTRANSFER
=>
\true
,
CURLOPT_HTTPHEADER
=>
$this
->
compileRequestHeaders
(
$headers
),
// Return response as string
CURLOPT_URL
=>
$url
,
\CURLOPT_HEADER
=>
\false
,
CURLOPT_CONNECTTIMEOUT
=>
10
,
// Enable header processing
CURLOPT_TIMEOUT
=>
$timeOut
,
\CURLOPT_SSL_VERIFYHOST
=>
2
,
CURLOPT_RETURNTRANSFER
=>
true
,
// Return response as string
\CURLOPT_SSL_VERIFYPEER
=>
\true
,
CURLOPT_HEADER
=>
true
,
// Enable header processing
\CURLOPT_HEADERFUNCTION
=>
function
(
$curl
,
$header
)
{
CURLOPT_SSL_VERIFYHOST
=>
2
,
$len
=
strlen
(
$header
);
CURLOPT_SSL_VERIFYPEER
=>
true
,
$this
->
headers
[]
=
trim
(
$header
);
//CURLOPT_CAINFO => __DIR__ . '/certs/DigiCertHighAssuranceEVRootCA.pem',
];
return
$len
;
if
(
$method
!==
"GET"
)
{
}
$options
[
CURLOPT_POSTFIELDS
]
=
$body
;
];
}
if
(
$method
!==
"GET"
)
{
$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.
*
*
* @param array $headers The request headers.
* @param array $headers The request headers.
*
*
* @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 */
private
function
getHttpResponseCode
()
/** @return int */
{
private
function
getHttpResponseCode
()
return
\intval
(
\curl_getinfo
(
$this
->
curlResource
,
\CURLINFO_HTTP_CODE
));
{
}
return
intval
(
curl_getinfo
(
$this
->
curlResource
,
CURLINFO_HTTP_CODE
));
private
function
throwExceptionIfError
()
}
{
$errorNumber
=
\curl_errno
(
$this
->
curlResource
);
private
function
throwExceptionIfError
()
if
(
$errorNumber
===
0
)
{
{
return
;
$errorNumber
=
curl_errno
(
$this
->
curlResource
);
}
if
(
$errorNumber
===
0
)
{
$errorMessage
=
\curl_error
(
$this
->
curlResource
);
return
;
throw
\iFirmaVendor\WPDesk\HttpClient\Curl\CurlExceptionFactory
::
createCurlException
(
$errorMessage
,
$errorNumber
);
}
}
/**
$errorMessage
=
curl_error
(
$this
->
curlResource
);
* Closes an existing curl connection
throw
CurlExceptionFactory
::
createCurlException
(
$errorMessage
,
$errorNumber
);
*/
}
private
function
closeConnection
()
{
/**
\curl_close
(
$this
->
curlResource
);
* Closes an existing curl connection
$this
->
curlResource
=
null
;
*/
}
private
function
closeConnection
()
private
function
prepareResponse
()
{
{
curl_close
(
$this
->
curlResource
);
list
(
$rawHeaders
,
$rawBody
)
=
$this
->
extractResponseHeadersAndBody
();
$this
->
curlResource
=
null
;
return
new
\iFirmaVendor\WPDesk\HttpClient\HttpClientResponse
(
$rawHeaders
,
$rawBody
,
$this
->
httpResponseCode
);
}
}
/**
private
function
prepareResponse
()
* Extracts the headers and the body into a two-part array
{
*
list
(
$rawHeaders
,
$rawBody
)
=
$this
->
extractResponseHeadersAndBody
();
* @return array
return
new
HttpClientResponse
(
$rawHeaders
,
$rawBody
,
$this
->
httpResponseCode
);
*/
}
private
function
extractResponseHeadersAndBody
()
{
/**
$rawBody
=
\trim
(
$this
->
rawResponse
);
* Extracts the headers and the body into a two-part array
$rawHeaders
=
\trim
(
implode
(
"
\r\n
"
,
$this
->
headers
));
*
return
[
$rawHeaders
,
$rawBody
];
* @return array
}
*/
/**
private
function
extractResponseHeadersAndBody
()
* @param string $url
{
* @param string $body
$parts
=
explode
(
"
\r\n\r\n
"
,
$this
->
rawResponse
,
2
);
* @param array $headers
$rawBody
=
array_pop
(
$parts
);
* @param int $timeOut
$rawHeaders
=
implode
(
"
\r\n\r\n
"
,
$parts
);
* @return HttpClientResponse
return
[
trim
(
$rawHeaders
),
trim
(
$rawBody
)];
* @throws HttpClientRequestException
}
*/
public
function
post
(
$url
,
$body
,
array
$headers
,
$timeOut
)
/**
{
* @param string $url
return
$this
->
send
(
$url
,
'POST'
,
$body
,
$headers
,
$timeOut
);
* @param string $body
}
* @param array $headers
/**
* @param int $timeOut
* @param string $url
* @return HttpClientResponse
* @param string $body
* @throws HttpClientRequestException
* @param array $headers
*/
* @param int $timeOut
public
function
post
(
$url
,
$body
,
array
$headers
,
$timeOut
)
* @return HttpClientResponse
{
* @throws HttpClientRequestException
return
$this
->
send
(
$url
,
'POST'
,
$body
,
$headers
,
$timeOut
);
*/
}
public
function
delete
(
$url
,
$body
,
array
$headers
,
$timeOut
)
{
/**
return
$this
->
send
(
$url
,
'DELETE'
,
$body
,
$headers
,
$timeOut
);
* @param string $url
}
* @param string $body
/**
* @param array $headers
* @param string $url
* @param int $timeOut
* @param string $body
* @return HttpClientResponse
* @param array $headers
* @throws HttpClientRequestException
* @param int $timeOut
*/
* @return HttpClientResponse
public
function
delete
(
$url
,
$body
,
array
$headers
,
$timeOut
)
* @throws HttpClientRequestException
{
*/
return
$this
->
send
(
$url
,
'DELETE'
,
$body
,
$headers
,
$timeOut
);
public
function
put
(
$url
,
$body
,
array
$headers
,
$timeOut
)
}
{
return
$this
->
send
(
$url
,
'PUT'
,
$body
,
$headers
,
$timeOut
);
/**
}
* @param string $url
* @param string $body
* @param array $headers
* @param int $timeOut
* @return HttpClientResponse
* @throws HttpClientRequestException
*/
public
function
put
(
$url
,
$body
,
array
$headers
,
$timeOut
)
{
return
$this
->
send
(
$url
,
'PUT'
,
$body
,
$headers
,
$timeOut
);
}
}
}
\ No newline at end of file
Loading