POST to a REST API from a wordpress form

I dont want to use a REST API to publish to wordpress.

I want to create a form on a wordpress page which will send a POST to an external REST API. Users will put in their information, and perhaps upload a file. When the press "submit" I want this information to be sent to a REST API on a different server (not wordpress).

Are there any plugins that allow this? Has anyone done this? Of course, I might run into troubles with this: http://en.wikipedia.org/wiki/Same_origin_policy

It looks like I want to do the reverse of this: http://wordpress.org/extend/plugins/json-api/ Instead of making a REST API for wordpress, I want to make wordpress communicate send a POST to an external REST API when a user presses a button on a form.

Topic plugin-json-api http-api api Wordpress

Category Web


I don't know of a plugin that does it in a general way; for the most part, you'll need to build something custom for each specific API you intend to communicate with.

For your purposes, the key function will be wp_remote_post(), which is a wrapper for the POST method of WP's HTTP class. (Use this instead of making manual cURL requests, because WP_Http has all kinds of fallbacks for different modes of HTTP transport.) Here's a nice introduction: http://yoast.com/wp-best-practice/wordpress-http-api/ Same origin policies generally only apply to browsers. wp_remote_post() and its ilk are fired on the server.

Sending data is pretty easy - just put an array into the 'body' of your wp_remote_post() call. The tricky part is always authentication - does the API server expect a pre-registered key, or some sort of oAuth handoff, or what. If it's as simple as a shared key, you can just pass it as part of the 'body' payload.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.