From 1bff19f4b680e4eb2cb18ee211ba49c549b8c9a2 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 28 Oct 2024 17:05:49 +0900 Subject: [PATCH] Update UrlRequests with patch, admin test page for it Also update delete to have optional body (content) --- www/admin/UrlReqeusts.target.php | 35 ++++++++ www/admin/class_test.url-requests.curl.php | 83 ++++++++++++++++++- www/lib/CoreLibs/UrlRequests/Curl.php | 54 ++++++++++-- .../Interface/RequestsInterface.php | 9 +- 4 files changed, 169 insertions(+), 12 deletions(-) create mode 100644 www/admin/UrlReqeusts.target.php diff --git a/www/admin/UrlReqeusts.target.php b/www/admin/UrlReqeusts.target.php new file mode 100644 index 00000000..79feafde --- /dev/null +++ b/www/admin/UrlReqeusts.target.php @@ -0,0 +1,35 @@ + BASE . LOG, + 'log_file_id' => $LOG_FILE_ID, + 'log_per_date' => true, +]); + +$http_headers = array_filter($_SERVER, function ($value, $key) { + if (str_starts_with($key, 'HTTP_')) { + return true; + } +}, ARRAY_FILTER_USE_BOTH); + +$file_get = file_get_contents('php://input'); +// str_replace('\"', '"', trim($file_get, '"')); + +$log->debug('SERVER', $log->prAr($_SERVER)); +$log->debug('HEADERS', $log->prAr($http_headers)); +$log->debug('POST', $log->prAr($_POST)); +$log->debug('PHP-INPUT', $log->prAr($file_get)); + +print Json::jsonConvertArrayTo([ + 'HTTP_HEADERS' => $http_headers, + "_GET" => $_GET, + "_POST" => Json::jsonConvertToArray($file_get), +]); + +$log->debug('[END]', '=========================================>'); + +// __END__ diff --git a/www/admin/class_test.url-requests.curl.php b/www/admin/class_test.url-requests.curl.php index d34a85d6..32d6931a 100644 --- a/www/admin/class_test.url-requests.curl.php +++ b/www/admin/class_test.url-requests.curl.php @@ -35,13 +35,88 @@ print ""; print '
Class Test Master
'; print '

' . $PAGE_NAME . '

'; -$url = 'https://soba.egplusww.jp'; - -$data = $client->requestGet($url, []); +print "
"; +$data = $client->requestGet( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + . '?other=get_a', + ['test-header: ABC', 'request-type: _GET'], + ['foo' => 'BAR'] +); +print "_GET RESPONSE:
" . print_r($data, true) . "
"; print "
"; -print "RESPONSE:
" . print_r($data, true) . "
"; +$data = $client->requestPost( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + . '?other=post_a', + ['payload' => 'data post'], + [ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _POST' + ], + ['foo' => 'BAR post'], +); +print "_POST RESPONSE:
" . print_r($data, true) . "
"; + print "
"; +$data = $client->requestPut( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + . '?other=put_a', + ['payload' => 'data put'], + [ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _PUT' + ], + ['foo' => 'BAR put'], +); +print "_PUT RESPONSE:
" . print_r($data, true) . "
"; + +print "
"; +$data = $client->requestPatch( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + . '?other=patch_a', + ['payload' => 'data patch'], + [ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _PATCH' + ], + ['foo' => 'BAR patch'], +); +print "_PATCH RESPONSE:
" . print_r($data, true) . "
"; + +print "
"; +$data = $client->requestDelete( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + . '?other=delete_no_body_a', + null, + [ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _DELETE' + ], + ['foo' => 'BAR delete'], +); +print "_DELETE RESPONSE:
" . print_r($data, true) . "
"; +$data = $client->requestDelete( + 'https://soba.egplusww.jp/developers/clemens/core_data/php_libraries/trunk/www/admin/UrlReqeusts.target.php' + . '?other=delete_body_a', + ['payload' => 'data delete'], + [ + 'Content-Type: application/json', + 'Accept: application/json', + 'test-header: ABC', + 'info-request-type: _DELETE' + ], + ['foo' => 'BAR delete'], +); +print "_DELETE RESPONSE:
" . print_r($data, true) . "
"; + print ""; diff --git a/www/lib/CoreLibs/UrlRequests/Curl.php b/www/lib/CoreLibs/UrlRequests/Curl.php index ed3f6294..8cf0f47c 100644 --- a/www/lib/CoreLibs/UrlRequests/Curl.php +++ b/www/lib/CoreLibs/UrlRequests/Curl.php @@ -15,7 +15,11 @@ use CoreLibs\Convert\Json; class Curl implements Interface\RequestsInterface { /** @var array all the valid request type */ - private const VALID_REQUEST_TYPES = ["get", "post", "put", "delete"]; + private const VALID_REQUEST_TYPES = ["get", "post", "put", "patch", "delete"]; + /** @var array list of requests type that are set as custom in the curl options */ + private const CUSTOM_REQUESTS = ["put", "patch", "delete"]; + /** @var array list of requests types that have _POST type fields */ + private const HAVE_POST_FIELDS = ["post", "put", "patch", "delete"]; /** @var int error bad request */ public const HTTP_BAD_REQUEST = 400; /** @var int error not authorized Request */ @@ -143,10 +147,10 @@ class Curl implements Interface\RequestsInterface // for post we set POST option if ($type == "post") { curl_setopt($handle, CURLOPT_POST, true); - } elseif (in_array($type, ["put", "delete"])) { + } elseif (in_array($type, self::CUSTOM_REQUESTS)) { curl_setopt($handle, CURLOPT_CUSTOMREQUEST, strtoupper($type)); } - if (in_array($type, ["post", "put"]) && !empty($params)) { + if (in_array($type, self::HAVE_POST_FIELDS) && !empty($params)) { curl_setopt($handle, CURLOPT_POSTFIELDS, $params); } // run curl execute @@ -386,19 +390,55 @@ class Curl implements Interface\RequestsInterface } /** - * Makes an request to the target url via curl: DELETE + * Makes an request to the target url via curl: PATCH * Returns result as string (json) * * @param string $url The URL being requested, * including domain and protocol + * @param string|array $payload String to pass on as POST + * @param array $headers [default=[]] Headers to be used in the request + * @param null|string|array $query [default=null] Optinal query parameters, array will be converted + * @return array{code:string,content:string} Result code and content as array, content is json + */ + public function requestPatch( + string $url, + string|array $payload, + array $headers = [], + null|string|array $query = null + ): array { + return $this->curlRequest( + "patch", + $this->convertQuery($url, $query), + $headers, + $this->convertPayloadData($payload) + ); + } + + /** + * Makes an request to the target url via curl: DELETE + * Returns result as string (json) + * Note that DELETE payload is optional + * + * @param string $url The URL being requested, + * including domain and protocol + * @param null|string|array $payload [default=null] Data to pass on as POST * @param array $headers [default=[]] Headers to be used in the request * @param null|string|array $query [default=null] String to pass on as GET, * if array will be converted * @return array{code:string,content:string} Result code and content as array, content is json */ - public function requestDelete(string $url, array $headers = [], null|string|array $query = null): array - { - return $this->curlRequest("delete", $this->convertQuery($url, $query), $headers); + public function requestDelete( + string $url, + null|string|array $payload = null, + array $headers = [], + null|string|array $query = null + ): array { + return $this->curlRequest( + "delete", + $this->convertQuery($url, $query), + $headers, + $payload !== null ? $this->convertPayloadData($payload) : null + ); } } diff --git a/www/lib/CoreLibs/UrlRequests/Interface/RequestsInterface.php b/www/lib/CoreLibs/UrlRequests/Interface/RequestsInterface.php index 1d649199..ed527edc 100644 --- a/www/lib/CoreLibs/UrlRequests/Interface/RequestsInterface.php +++ b/www/lib/CoreLibs/UrlRequests/Interface/RequestsInterface.php @@ -63,15 +63,22 @@ interface RequestsInterface /** * Makes an request to the target url via curl: DELETE * Returns result as string (json) + * Note that DELETE payload is optional * * @param string $url The URL being requested, * including domain and protocol + * @param null|string|array $payload [default=null] Data to pass on as POST * @param array $headers [default=[]] Headers to be used in the request * @param null|string|array $query [default=null] String to pass on as GET, * if array will be converted * @return array{code:string,content:string} Result code and content as array, content is json */ - public function requestDelete(string $url, array $headers = [], null|string|array $query = null): array; + public function requestDelete( + string $url, + null|string|array $payload = null, + array $headers = [], + null|string|array $query = null + ): array; } // __END__