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
1fd01c91
Commit
1fd01c91
authored
5 years ago
by
Marcin Kolanko
Browse files
Options
Downloads
Patches
Plain Diff
fixed issue with headers
parent
c5dab20b
No related branches found
No related tags found
1 merge request
!3
Devel
Pipeline
#5051
failed with stages
in 2 minutes and 28 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Curl/CurlClient.php
+188
-201
188 additions, 201 deletions
src/Curl/CurlClient.php
with
188 additions
and
201 deletions
src/Curl/CurlClient.php
+
188
−
201
View file @
1fd01c91
<?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
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