wordpress XMLRPC api integration with ruby and rails

Ruby on Rails is really OOPs based framework. I personally love this framework. I worked on this for many years. Many Ruby lovers are looking to integrate the wordpress with Ruby on Rails. I strongly suggest to integrate wordpress with ROR using XMLRPC APIs. Using following code you can easily add the wordpress into Ruby on Rails Project. Use my following steps:

wordpress XMLRPC api integration with ruby and rails

Note: There are so many XMLRPC APIs provided by wordpress. I given the some simple example here.

First setup wordpress. Login to wordpress admin and enable the XMLRPC.
Go to Settings->writing and enable the XMLRPC checkbox.

wordpress XMLRPC api integration with ruby and rails
wordpress XMLRPC api integration with ruby and rails

Now you can fetch the wordpress posts, pages, tags etc.. using XMLRPC.

Following script is written in Ruby.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

01require 'xmlrpc/client'
02 
03# build a post
04 
05#Use this for reference - http://codex.wordpress.org/XML-RPC_wp , http://codex.wordpress.org/XML-RPC_WordPress_API
06 
07post = {
08'post_title'       => 'Post Title',
09'post_excerpt'       => 'Post excerpt',
10'post_content'       => 'this is Post content'
11}
12 
13# initialize the connection - Change
14 
15connection = XMLRPC::Client.new2('http://www.purabtech.in/wordpress3/xmlrpc.php'#Replace your wordpress URL
16 
17# make the call to publish a new post
18 
19#result = connection.call('metaWeblog.getRecentPosts', 1,'admin','123456') // Get Recent 10 Post
20#result = connection.call('wp.getPost', 1,'admin','123456',19) // Get Single Post
21#result = connection.call('wp.getPage', 1,'admin','123456',1) // Get Single Page
22#result = connection.call('wp.getPages', 1,'admin','123456',10) // Get Pages
23#result = connection.call('wp.getPosts', 1,'admin','123456') // Get 10 Posts from wordpress
24result = connection.call('wp.getPosts', 1,'admin','123456',1000) // Get 10000 Posts from wordpress
25#result = connection.call('wp.newPost', 1,'admin','123456',post) //For New Creating the Post
26 
27puts result // Printresult
28puts result.length // Printresult

[/viral-lock]

If you are facing any issue then write to me.

How to read xml using php

Many new PHP developer looking for how to easily read the xml file. In this tutorial I will show you how to read the xml file using PHP language. Here I given the sample code for parsing the XML using PHP.

How to read xml using php


This is my xml file format and file name is readxml.xml file

01<?xml version="1.0"?>
02 
03<!-- our XML-document describes a purchase order -->
04<purchase-order>
05 
06 <date>2005-10-31</date>
07 <number>12345</number>
08 
09 <purchased-by>
10 <name>My name</name>
11 <address>My address</address>
12 </purchased-by>
13 
14 <!-- a collection element, contains a set of items -->
15 <order-items>
16 
17 <item>
18 <code>687</code>
19 <type>CD</type>
20 <label>Some music</label>
21 </item>
22 
23 <item>
24 <code>129851</code>
25 <type>DVD</type>
26 <label>Some video</label>
27 </item>
28 
29 </order-items>
30 
31</purchase-order>

This is one php file called test.php and code as follows

01<?php
02 //create new document object
03 $dom_object = new DOMDocument();
04 //load xml file
05 $dom_object->load("test.xml");
06 
07 $item = $dom_object->getElementsByTagName("item");
08 
09 foreach( $item as $value )
10 {
11 $codes = $value->getElementsByTagName("code");
12 $code  = $codes->item(0)->nodeValue;
13 
14 $types = $value->getElementsByTagName("type");
15 $type  = $types->item(0)->nodeValue;
16 
17 $labels = $value->getElementsByTagName("label");
18 $label  = $labels->item(0)->nodeValue;
19 
20 echo "$code - $type - $label <br>";
21 }
22?>

When you run the readxml.php file. you will see the following output.

687 – CD – Some music
129851 – DVD – Some video

JSONP for cross domain communication solution using with javascript and PHP

Last year in Feb I heard about JSONP and really liked the JSONP solutions. JSONP allows you to make an HTTP request outside your own domain
JSONP consume the Web Services from JavaScript code. Earlier I worked on so on AJAX but there is issue with working or communication issue with cross domain sites.

JSONP for cross domain communication solution using with javascript and PHP
JSONP for cross domain communication solution using with javascript and PHP

XMLHttpRequest is blocked from making external requests.

JSONP for cross domain communication solution using with javascript and PHP

JavaScript code to make a JSONP call will as follows:

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

01function common_jsonpRequest(url, name, query) {
02 
03 if (url.indexOf("?") > -1)
04 url += "&jsonp="
05 else
06 url += name + "&";
07 if (query)
08 url += encodeURIComponent(query) + "&";
09 url += new Date().getTime().toString(); // prevent caching
10 
11 var JSONPscript = document.createElement("script");
12 JSONPscript.id = 'wordpressapi_jsonpRequest';
13 JSONPscript.setAttribute("src", url + "&CacheBuster=" + Math.random());
14 JSONPscript.setAttribute("type","text/javascript");
15 
16 // write the jsonp script tag
17 document.body.appendChild(JSONPscript);
18 //script.text = "";
19 
20}
21 
22function returncall (data){
23 alert(data);
24// you can use data as a variable also.
25}

[/viral-lock]

In same javascript you should write the returning function as above.

You can call JSONP this function as follows;

1<a href="http://images.purabtech.in/JSONP-request.gif">
2<img class="alignnone size-medium wp-image-1167" title="JSONP-request" src="http://images.purabtech.in/JSONP-request-300x125.gif" alt="" width="300" height="125" />
3</a>
4common_jsonpRequest(base_url+'getData.php?jsonp=mycallback&name=for_wordpressapi');

Notel: getData.php file will beahave like externaal javascript file so handle this file carefully.
getData.php sample file.

1<?php
2$data = "this is our dynamic data";
3if($_REQUEST['name']=='for_wordpressapi'){
4 
5echo "returncall(".$data.")";
6 
7}
8 
9?>&nbsp;

This will return the “this is our dynamic data” as a alert.

How to create and read the xml using php

In this tutorial we given code sample for create and read the xml using php. we given explanation and there details

What is XML?
Extensible Markup Language (XML) is described as both a markup language. Now it is known as data storage format also

How to create and read the xml using php
How to create and read the xml using php

XML is very important in in today’s application development environment.
If you’ve never before worked with XML in PHP or have not yet made the jump to PHP5, this starter guide to working with new functionality available in PHP5 for XML.

Using following code you are able to create simple xml file.

01<?php
02 
03//Creates XML string and XML document using the DOM
04$dom = new DomDocument('1.0');
05 
06//add root - <books>
07$books = $dom->appendChild($dom->createElement('books'));
08 
09//add <book> element to <books>
10$book = $books->appendChild($dom->createElement('book'));
11 
12//add <title> element to <book>
13$title = $book->appendChild($dom->createElement('title'));
14 
15//add <title> text node element to <title>
16$title->appendChild(
17$dom->createTextNode('Wordpressapi Magazine'));
18 
19//generate xml
20$dom->formatOutput = true; // set the formatOutput attribute of
21// domDocument to true
22// save XML as string or file
23$test1 = $dom->saveXML(); // put string in test1
24$dom->save('wordpressapi1.xml'); // save as file
25?>

Out put of above code will as follows:

1<?xml version="1.0"?>
2<books>
3<book>
4<title>Wordpressapi Magazine</title>
5</book>
6</books>

Now I am going to show you how to read the xml file using php

[/php]
loadXML(‘wordpressapi1.xml’);
if (!$dom) {
echo ‘Error while parsing the document’;
exit;
}

$s = simplexml_import_dom($dom);

echo $s->book[0]->title; // WordPressapi Magazine
?>
[/php]

or in other way

1$xmlFileData = file_get_contents(“wordpressapi1.xml”);
2// Here's our Simple XML parser!
3$xmlData = new SimpleXMLElement($xmlFileData);
4// output will be shown in array format
5print_r($xmlData);

php create xml file from mysql

php create xml file from mysql, Here I am giving you the very basic sample code for creating XML file through PHP and MYsql. We given code sample for this.

php create xml file from mysql

01<?php
02 
03// We'll be outputting a PDF
04header('Content-type: text/xml');
05 
06echo ""
07 
08$db_name = "testDB";
09$connection = mysql_connect("example.com", "username", "password") or die("Could not connect.");
10$table_name = 'user';
11 
12$query = "select * from " . $table_name;
13 
14$result = mysql_query($query, $connection) or die("Could not complete database query");
15$num = mysql_num_rows($result);
16 
17while ($row = mysql_fetch_assoc($result)) {
18echo "". $row['firstname']."";
19echo "". $row['lastname']."";
20echo "
21". $row['address']."
22 
23";
24echo "". $row['age']."";
25}
26 
27?>

Create XML document with Ruby on rails

XML is most popular data transfer layer. In every language we need XML parsing. In Ruby there is in build XML strong parsing support.

Create XML document with Ruby on rails

In this post I am going to cover the How to create XML file with simple Ruby code.

TIP: If you are new in Rails than follow step one or go to step two.

1. You need to install Ruby on your computer

http://www.ruby-lang.org/en/

2. Open command prompt(for windows) or console(linux)

3. Create file named CreateXML.rb and copy and paste following code

require “rexml/document”
include REXML

string = <<EOF
<xml>
<element attribute=”attr”>first XML document with ruby </element>
</xml>
EOF
doc = Document.new string

print doc

Output will be like this:

Create XML document with Ruby on rails
Create XML document with Ruby on rails