Insert a post in Wordpress through Java.

Wordpress is very flexible but if you're looking on how to insert a post in wordpress through java, I can completely understand your pain. There are a lot of tutorials, libraries and methods available online but none of them seem to be working. They are very difficult to configure and some don't configure at all. I have tried most of the methods available online but no luck! Hence, I decided to create my own method which runs without even accessing the XMLRPC of wordpress. 

Most of the tutorials available online are using the XMLRPC protocol with some libraries like RedStone, Apache XMLRPC etc. So here's my method:


We will do this by creating a custom php script. The PHP Script will take a few arguements as post requests, the post requests will be processed and a new post will be generated. You can find the php script here: http://siddhantminocha.blogspot.com/2014/11/how-to-create-post-in-wordpress-through.html




Save this php script as newpost.php in your wordpress folder. Now, let's start with java. Create a new java file, add your class name, main methods and call the following method. You will need to import a few libraries to make the post request(links below the post). :


 public static void createpostviapost(String title, String body) throws Exception {

        String url = "http://path.to.wordpresssite.com/newpost.php";

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);

        // add header
        post.setHeader("User-Agent", "PosterBotDefault");

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("title", title));
        urlParameters.add(new BasicNameValuePair("body", body));
              

        post.setEntity(new UrlEncodedFormEntity(urlParameters));

        HttpResponse response = client.execute(post);
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + post.getEntity());
        System.out.println("Response Code : "
                + response.getStatusLine().getStatusCode());

        BufferedReader rd = new BufferedReader(
                new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);   
        }

        System.out.println(result.toString());

    }



This will create your post and get you a result from the php script saying "Post Created". 




You will need apache http client library which you can get from: http://hc.apache.org/httpclient-3.x/


Import a few classes by:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;



That's it! Lots of love! :)


SHARE ON:

Hi! I'm Siddhant Minocha, a Jaipur based hacker/developer. I spend most of my day coding things, trying to develop the next billion dollar idea. When I'm not over-analyzing random stuff and people, I hang out with friends (mostly entrepreneurs). I'm in my final year of graduation and till now I have started and exited 3 startups and developed a lot of websites and software for different companies. Follow me on twitter and other social networks to see what I keep doing. You can also hire me to design/develop your website, if you're worried about your website or app's security or any other tech related work. See ya! :D

    Blogger Comment

0 comments:

Post a Comment