Last active
April 8, 2024 09:15
-
-
Save RadGH/68f5542ee756aae3c9e0a28984409fa5 to your computer and use it in GitHub Desktop.
WP Remote Get/Post/Put/Patch/Del wrapper for apis with json decoded body and consistent result data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // sample usage | |
| $url = 'http://example.org/'; | |
| $url_args = array( 'id' => 47, 'name' => 'radley' ); | |
| $body_fields = array( 'nonce' => 12345 ); | |
| $headers = array( 'Authorization' => 'api 123456' ); // Mailchimp-style api key using basic authorization | |
| $result = rs_remote_request( $url, 'GET', $url_args, $body_fields, $headers ); | |
| // Success (200 = changes ok, 204 = no change needed) | |
| if ( $result['response_code'] == 200 || $result['response_code'] == 204 ) { | |
| return $result; | |
| } | |
| // Item does not exist | |
| else if ( $result['response_code'] == 404 ) { | |
| return false; | |
| } | |
| // Unexpected response code, display a warning and exit. | |
| else { | |
| // You should not use this in production code, at least make it more friendly and prevent it from exiting the script. | |
| echo '<div class="rs-api-warning">'; | |
| echo '<p>RS API Warning ('. $api_result['response_code'].'): '. $api_result['response_message'] .'</p>'; | |
| echo '<pre>'. esc_html(print_r($api_result, true)) .'</pre>'; | |
| echo '</div>'; | |
| exit; | |
| } | |
| // ------------------ | |
| function rs_remote_request( $url, $method = 'GET', $url_args = array(), $body_fields = array(), $headers = array() ) { | |
| // Add url args (get parameters) to the main url | |
| if ( $url_args ) $url = add_query_arg( $url_args, $url ); | |
| // Prepare arguments for wp_remote_request | |
| $args = array(); | |
| if ( $method ) $args['method'] = $method; | |
| if ( $headers ) $args['headers'] = $headers; | |
| if ( $body_fields ) $args['body'] = json_encode( $body_fields ); | |
| // Make the request | |
| $response = wp_remote_request($url, $args); | |
| // Get the results | |
| $response_code = wp_remote_retrieve_response_code( $response ); | |
| $response_message = wp_remote_retrieve_response_message( $response ); | |
| $response_body = wp_remote_retrieve_body( $response ); | |
| // Decode the JSON in the body, if it is json | |
| if ( $response_body ) { | |
| $j = json_decode( $response_body ); | |
| if ( $j ) $response_body = $j; | |
| } | |
| // Return this information in the same format for success or error. Includes debugging information. | |
| return array( | |
| 'response_body' => $response_body, | |
| 'response_code' => $response_code, | |
| 'response_message' => $response_message, | |
| 'response' => $response, | |
| 'debug' => array( | |
| 'file' => __FILE__, | |
| 'line' => __LINE__, | |
| 'function' => __FUNCTION__, | |
| 'args' => array( | |
| 'url' => $url, | |
| 'method' => $method, | |
| 'url_args' => $url_args, | |
| 'body_fields' => $body_fields, | |
| 'headers' => $headers, | |
| ), | |
| ) | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment