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);

How to append html code using javascript and php

append html code using javascript and php, In this example I am going to show you how to add the dynamic HTML code into Document using javascript and PHP.

How to append html code using javascript and php

01<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
02 
03$random_id = 125845465; // PUTTING SOME RANDOM ID YOU CAN USE YOURSELF.
04 
05$DYNAMIC_HTML = '<div>
06<b>Sponsored Links</b>
07 
08<div>
09 12px;font-weight:bold;" href="http://www.ucoz.com/" rel="nofollow">Create a website for free - uCoz
10 
11Build a website quickly and easily
12Customizable templates and graphics
13 
14</div>
15 
16<div>
17 12px;font-weight:bold;" href="http://www.purabtech.in/files/domain_names/">$1.99 Domain Names
18 
19 With every new non-domain purchase thru <a href="http://www.purabtech.in/files/domain_names/" rel="nofollow">wordpressapi</a>, you get a domain name for only $1.99.
20</div>
21 
22<div>
23 12px;font-weight:bold;" href="http://www.purabtech.in/files/" rel="nofollow">FREE Hosting!
24 
25 With every domain you register with <a href="http://www.purabtech.in/files/" rel="nofollow">wordpressapi</a> you get FREE hosting.
26</div>
27</div>';
28 
29 $HTML = "var objHead = document.getElementsByTagName('head');
30 var objCSS = objHead[0].appendChild(document.createElement('link'));
31 objCSS.id = '.$random_id.';
32 objCSS.rel = 'stylesheet';
33 objCSS.href = 'http://YOURSITE/STYLE.css';
34 objCSS.type = 'text/css';";
35 
36 $HTML .= '
37 var WORDPRESAPI_html_'.$random_id.' = document.createElement("div");
38 WORDPRESAPI_html_'.$random_id.'.id = "WORDPRESAPI_html_'.$random_id.'";
39 WORDPRESAPI_html_'.$random_id.'.style.position = "absolute";
40 WORDPRESAPI_html_'.$random_id.'.style.zIndex = "100";
41 WORDPRESAPI_html_'.$random_id.'.style.left = "0px";
42 WORDPRESAPI_html_'.$random_id.'.style.top = "0px";
43 WORDPRESAPI_html_'.$random_id.'.style.visibility = "hidden";
44 WORDPRESAPI_html_'.$random_id.'.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)";
45 WORDPRESAPI_html_'.$random_id.'.innerHTML = "'.addcslashes($DYNAMIC_HTML,"\\\'\"&\n\r<>").'";
46 document.body.appendChild(WORDPRESAPI_html_'.$random_id.');';
47 
48echo $HTML;
49 
50?>

In this code if you observe then following line is very important.

1WORDPRESAPI_html_'.$uniqid.'.innerHTML = "'.addcslashes($HTML,"\\\'\"&\n\r<>").'";
How to append html code using javascript and php
How to append html code using javascript and php

Whenever you are using javascript and PHP both together then use following way to push your HTML in Dom.

install php and memcached server on centos5

In this article I given full step by step installation guide about php and memcached . Check full article for install php and memcached server on centos5.

install php and memcached server on centos5

What is Memcached?

Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

install php and memcached server on centos5
install php and memcached server on centos5

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Please use following commands for installtion;
[root@ip-192-168-2-125 wordpressapi]# wget http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-2.noarch.rpm

[root@ip-192-168-2-125 wordpressapi]# yum remove php-common

[root@ip-192-168-2-125 wordpressapi]# yum install php-pdo php-mcrypt squirrelmail php-pecl-apc php-xml php-gd php-devel php php-imap php-pgsql php-pear php-soap php-mbstring php-ldap php-mysql php-cli php-pecl-memcache

[root@ip-192-168-2-125 wordpressapi]# /etc/init.d/memcached restart
Shutting down Distributed memory caching (memcached): [ OK ]
Starting Distributed memory caching (memcached): [ OK ]

[root@ip-192-168-2-125 wordpressapi]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]

[root@ip-192-168-2-125 wordpressapi]# vim /etc/php.ini
In php.ini file Please find the “extension=modulename.extension” words
Under that sentance please paste following line:
extension=memcache.so
Note: dont comments that line.

[root@ip-192-168-2-125 wordpressapi]# /etc/init.d/httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@ip-192-168-2-125 wordpressapi]#

How to install Nginx and PHP on Ubuntu

For this tutorial I am using the Ubuntu 8.10 linux version. We given very easy steps for installing php and ngnix and configuration details.

How to install Nginx and PHP on Ubuntu

Install Nginx
# sudo apt-get install nginx

If apache server is installed on your machine then please stop the apache server first.

#sudo /etc/init.d/nginx start

#sudo update-rc.d nginx defaults

Install PHP5

#sudo apt-get install php5-cgi php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-mhash php5-ming php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Open the PHP.ini file

#sudo vim /etc/php5/cgi/php.ini

Insert following line at the end of php.ini file
cgi.fix_pathinfo = 1

Install the lighttpd server

#sudo apt-get install lighttpd

You will see an error message saying that lighttpd couldn’t start because port 80 is already in use.

For removing the error message use following command

#sudo update-rc.d -f lighttpd remove

Start the FastCGI daemon service

#sudo /usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

If you want run following command whenever you boot the system. Open following file.

#sudo vim /etc/rc.local

Copy paste following following lines in file.

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -u www-data -g www-data -f /usr/bin/php5-cgi -P /var/run/fastcgi-php.pid

Configuration Nginx

#sudo vim /etc/nginx/nginx.conf
Just copy paste the following code in end of httpd block. Dont paste outside of http block.

server {
listen 80;
server_name _;

access_log /var/log/nginx/localhost.access.log;

location / {
root /var/www/nginx-default;
index index.php index.html index.htm;
}

location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}

location /images {
root /usr/share;
autoindex on;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
location ~ /\.ht {
deny all;
}
}

How to install Nginx and PHP on Ubuntu
How to install Nginx and PHP on Ubuntu

Just restart the Nginx server.

#sudo /etc/init.d/nginx restart

How to get Short Url from using PHP API WITH is.gd, api.tr.im, hex.io

Creating the short url is now becoming the popular trend because of twitter and facebook. Tutorial about How to get Short Url from using PHP API WITH is.gd, api.tr.im, hex.io.

 

get Short Url from using PHP
get Short Url from using PHP

If you want to use API in php for creating the short URL. use following code:

1<?php //Get short URL from free API with purabtech.in/files/ function get_short_url($long_url,$provider='isgd') { $ch = curl_init(); $timeout = 5; switch(strtolower(trim($provider))) { case "isgd": curl_setopt($ch,CURLOPT_URL,'http://is.gd/api.php?longurl='.$long_url); break; case "hexio": curl_setopt($ch,CURLOPT_URL,'http://hex.io/api-create.php?url='.$long_url); break; case "trim": $return = "http://api.tr.im/v1/trim_url.xml?url=%s"; curl_setopt($ch,CURLOPT_URL,'http://api.tr.im/v1/trim_url.xml?url='.$long_url); break; default: curl_setopt($ch,CURLOPT_URL,'http://is.gd/api.php?longurl='.$long_url); } curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $content = curl_exec($ch); curl_close($ch); // return the short URL return $content; } //uage $short_url = get_short_url('http://images.purabtech.in/'); echo $short_url; ?>

<br>

Sample ofHow to Use<br><br>

01<?php
02$long_url = 'http://images.purabtech.in/';
03$short_url = get_short_url($long_url,'hexio');
04echo $short_url;
05echo '
06';
07$short_url = get_short_url($long_url,'isgd');
08echo $short_url;
09echo '
10';
11$short_url = get_short_url($long_url,'trim');
12echo $short_url;
13echo '
14';
15 
16?>

install the YII on local machine (PHP framework)

Yii — a high-performance component-based PHP framework best for developing large-scale Web applications. Yii comes with a full stack of features, including MVC, DAO/ActiveRecord, I18N/L10N, caching, jQuery-based AJAX support, authentication and role-based access control, scaffolding, input validation, widgets, events, theming, Web services, and so on.

Written in strict OOP, Yii is easy to use and is extremely flexible and extensible. here in this article I given the steps for install the YII on local machine (PHP framework)

install the YII on local machine (PHP framework)

You can download the YII framework from following URL

http://www.yiiframework.com/download/

Yii PHP Framework: Best for Web 2.0 install the YII on local machine
install the YII on local machine

I copied the folder in my ROOT directory. I am using the fedora on my machine.

 

install the YII on local machine

 

Just follow my commands:

01[kapil@kapilk-pc ~]$ cd /var/www/html/yii/
02[kapil@kapilk-pc yii]$ cd framework/
03[kapil@kapilk-pc framework]$ ./yiic webapp ../testdrive
04PHP Warning:  Module 'memcache' already loaded in Unknown on line 0
05Create a Web application under '/var/www/html/yii/testdrive'? [Yes|No] y
06unchanged css/bg.gif
07unchanged css/main.css
08unchanged css/form.css
09unchanged css/print.css
10unchanged css/screen.css
11unchanged css/ie.css
12unchanged index.php
13unchanged themes/classic/views/.htaccess
14unchanged index-test.php
15unchanged protected/components/Controller.php
16unchanged protected/components/UserIdentity.php
17unchanged protected/yiic.php
18unchanged protected/config/main.php
19unchanged protected/config/console.php
20unchanged protected/config/test.php
21unchanged protected/yiic
22unchanged protected/models/ContactForm.php
23unchanged protected/models/LoginForm.php
24unchanged protected/data/schema.mysql.sql
25unchanged protected/data/testdrive.db
26unchanged protected/data/schema.sqlite.sql
27unchanged protected/.htaccess
28unchanged protected/controllers/SiteController.php
29unchanged protected/yiic.bat
30unchanged protected/views/site/pages/about.php
31unchanged protected/views/site/index.php
32unchanged protected/views/site/error.php
33unchanged protected/views/site/contact.php
34unchanged protected/views/site/login.php
35unchanged protected/views/layouts/main.php
36unchanged protected/tests/bootstrap.php
37unchanged protected/tests/WebTestCase.php
38unchanged protected/tests/phpunit.xml
39unchanged protected/tests/functional/SiteTest.php
40 
41Your application has been created successfully under /var/www/html/yii/testdrive.
42[kapil@kapilk-pc framework]$

Run Php code in html files

In this tutorial we will show you to Run Php code in html files. PHP is most popular server-side script in the world now. HTML is great for SEO purpose. executing PHP script is possible in HTML file.

What is PHP?

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.

Just use following code in your .htaccess file or Apache configuration file.

AddType application/x-httpd-php .html

Or for .htm

AddType application/x-httpd-php .htm

If you only plan on including the PHP on one page, it is better to setup this way:

AddType application/x-httpd-php .html
I found following article useful.
http://php.about.com/od/advancedphp/p/html_php.htm

solved – bash: phpize: command not found

This error is due to “phpize” command not available on your server.

To fix, install php-devel package.

If your server have yum (Eg: Fedora)

[root@localhost siwan]# phpize
bash: phpize: command not found
[root@localhost siwan]# yum -y install php-devel

[root@localhost siwan]# phpize
Cannot find config.m4.
Make sure that you run ‘/usr/bin/phpize’ in the top level source directory of the module

[root@localhost siwan]#
How to install the eaccelerator on fedora9?
use follwoing command

[root@localhost siwan]# yum install php-eaccelerator

how to check programs installation path in linux

There are many linux distributions like fedora, redhat, centos. Each has different program installation path. Using following commands you can check programs installation path in linux

how to check programs installation path in linux

Use following commands:

01</pre>
02[root@localhost ]# which php
03/usr/bin/php
04[root@localhost ]# which ruby
05/usr/bin/ruby
06[root@localhost ]# php --version
07PHP 5.2.9 (cli) (built: Apr 17 2009 03:42:25)
08Copyright (c) 1997-2009 The PHP Group
09Zend Engine v2.2.0, Copyright (c) 1998-2009 Zend Technologies
10with eAccelerator v0.9.5.2, Copyright (c) 2004-2006 eAccelerator, by eAccelerator
11[root@localhost ]# ruby --version
12ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-linux]
13[root@localhost Desktop]#

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?>