How to create a HTTP API
-
Hello!
In this post I found out, that it’s possible to send data to another server/domain with WP HTTP API.
I know already, how to build a post request. So on click a button at server1 it should send the userdata to server2:
//POST-REQUEST at server1/domain1 $body = array( 'username' => 'Bob', 'email' => '[email protected]' ); $args = array( 'body' => $body, 'method' => 'POST', 'timeout' => '45', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'cookies' => array() ); $response = wp_remote_post( $url, $args ); if ( is_wp_error( $response ) ) { $error_message = $response->get_error_message(); echo "Something went wrong: $error_message"; } else { echo 'Response:<pre>'; print_r( $response ); echo '</pre>'; }
What I’ve to do now at server2?
function getData(){ if($_POST['username']) echo "TEST: " . $_POST['name']; //DOESN'T WORK }
But how can I fetch this data ($body) at server2/domain2? How to listen at server2, if server1 is sending me data?
I want to grap at server2 the data which I sent on server1 on save it in the DB.
Could someone show me a little example?I’m sorry if this is pretty simple, but I’m quite new in APIs.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘How to create a HTTP API’ is closed to new replies.