forked from LiveCarta/LiveCartaWP
started with wp-rest-api-event plugin
This commit is contained in:
63
htdocs/wp-content/plugins/wp-rest-api-event/HttpClient.php
Normal file
63
htdocs/wp-content/plugins/wp-rest-api-event/HttpClient.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
namespace app\wpRestApiEvent;
|
||||
|
||||
/**
|
||||
* Class HttpClient
|
||||
* @package app\wpRestApiEvent
|
||||
*/
|
||||
class HttpClient
|
||||
{
|
||||
/**
|
||||
* @param $data
|
||||
* @return null|string
|
||||
*/
|
||||
public function sendRequest($data)
|
||||
{
|
||||
return $this->doPostRequest(LAWCARTA_WP_REST_API_REQUEST_URL, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $sysLogId
|
||||
* @param $data
|
||||
* @return string|\WP_Error
|
||||
*/
|
||||
public function sendResponse($sysLogId, $data)
|
||||
{
|
||||
$data['sysLogId'] = $sysLogId;
|
||||
return $this->doPostRequest(LAWCARTA_WP_REST_API_RESPONSE_URL, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @param $data
|
||||
* @return string|\WP_Error
|
||||
*/
|
||||
private function doPostRequest($url, $data)
|
||||
{
|
||||
$response = wp_remote_post($url, [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'body' => json_encode($data),
|
||||
'sslverify' => false
|
||||
|
||||
]);
|
||||
$sysLogId = null;
|
||||
/** @var \WP_HTTP_Requests_Response $httpResponse */
|
||||
$httpResponse = $response['http_response'];
|
||||
if ($httpResponse->get_status() == 200) {
|
||||
$body = isset($response['body']) ? json_decode($response['body'], true) : null;
|
||||
$sysLogId = $body['sysLogId'] ?? null;
|
||||
}
|
||||
|
||||
return $sysLogId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HttpClient
|
||||
*/
|
||||
public static function create()
|
||||
{
|
||||
return new self();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user