There isn’t a decent measure of data on wp_remote_get() in the codex. you can Investigate this yourself to understand better with the link bellow:
https://codex.wordpress.org/Function_Reference/wp_remote_get
After you’ve utilized this function, the data it returns must be organized positively or you won’t have the capacity to do anything with it.
wp_remote_get() Basics
We should begin with a basic understanding:
1 2 3 4 5 6 7 |
$response = wp_remote_get( 'http://yoursite.com/api/somecallhere' ); if ( is_wp_error( $response ) ) { echo 'There be errors, yo!'; } else { echo 'It worked!'; } |
With this Function, we’re utilizing wp_remote_get()
to recover our URL. On the off chance that there’s a blunder, we show the primary sentence. On the off chance that it worked, we show the second sentence.
Very little incentive in that, so we require a couple of more lines of code.
What Comes Next?
When we recover information from the URL, we have to design it accurately.
1 2 3 4 5 6 7 8 9 10 11 12 |
$response = wp_remote_get( 'http://yoursite.com/api/somecallhere' ); if ( is_wp_error( $response ) ) { echo 'There be errors, yo!'; } else { $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body ); } if ( $data->Data ) { echo 'We got data, yo!'; } |
After we recovget our information, we use wp_remote_retrieve_body()
and json_decode()
to process it and get what we require.
We can even go beyond and show our information.
1 2 3 4 5 6 7 8 9 10 11 12 |
$response = wp_remote_get( 'http://yoursite.com/api/somecallhere' ); if ( is_wp_error( $response ) ) { echo 'There be errors, yo!'; } else { $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body ); } if ( $data->Data ) { print_r( $data->Data ); } |
This we’ll show the variety of information that was returned.
Conclussion
When you have to guide into an API or recover information from a URL, wp_remote_get()
is the capacity you require. Simply recollect that you’ll additionally need to make a couple of more strides to get to the real information you need.
On the off chance that you have any inquiries or input on this post, please utilize the remarks underneath to begin a talk.
Leave a comment