,error?:array,status?:int,message?:string} */ public static function get_json(string $base_url, string $path, string $api_key, string $request_id): array { $base_url = self::normalize_base_url($base_url); $path = '/' . ltrim($path, '/'); $url = $base_url . $path; $args = [ 'timeout' => 20, 'headers' => [ 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, 'X-Request-Id' => $request_id, 'X-2DL-Request-Id' => $request_id, ], ]; $res = wp_remote_get($url, $args); if (is_wp_error($res)) { return [ 'ok' => false, 'status' => 0, 'error' => [ 'code' => 'WP_ERROR', 'message' => $res->get_error_message(), ], 'message' => $res->get_error_message(), ]; } $status = (int) wp_remote_retrieve_response_code($res); $raw = (string) wp_remote_retrieve_body($res); $json = json_decode($raw, true); if (!is_array($json)) { return [ 'ok' => false, 'status' => $status, 'error' => [ 'code' => 'BAD_RESPONSE', 'message' => 'Invalid JSON from hosted platform', ], 'message' => 'Invalid JSON from hosted platform', ]; } $json['status'] = $status; return $json; } /** * @param array $payload * @return array{ok:bool,data?:array,error?:array,status?:int,message?:string} */ public static function post_json(string $base_url, string $path, array $payload, string $api_key, string $request_id): array { $base_url = self::normalize_base_url($base_url); $path = '/' . ltrim($path, '/'); $url = $base_url . $path; $args = [ 'timeout' => 20, 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json', 'Authorization' => 'Bearer ' . $api_key, 'X-Request-Id' => $request_id, 'X-2DL-Request-Id' => $request_id, ], 'body' => wp_json_encode($payload), ]; $res = wp_remote_post($url, $args); if (is_wp_error($res)) { return [ 'ok' => false, 'status' => 0, 'error' => [ 'code' => 'WP_ERROR', 'message' => $res->get_error_message(), ], 'message' => $res->get_error_message(), ]; } $status = (int) wp_remote_retrieve_response_code($res); $raw = (string) wp_remote_retrieve_body($res); $json = json_decode($raw, true); if (!is_array($json)) { return [ 'ok' => false, 'status' => $status, 'error' => [ 'code' => 'BAD_RESPONSE', 'message' => 'Invalid JSON from hosted platform', ], 'message' => 'Invalid JSON from hosted platform', ]; } // Hosted API already returns { ok, data?, error? } $json['status'] = $status; return $json; } }