Thursday, July 21, 2011

Web service in php

Most of the times it seems like developer are little bit confused about the webservice.



Webservice is nothing but the output in such a format so the technology going to use your webservice can manage it. Here i have tried to provide your all two solutions regarding the PHP web-service.
With use of below code, you can generate your desire output in XML as well in JSON format.





You can call this file to get XML output format, by typing http://localhost/index.php?user=1&num=10


To get output in json format, you can type http://localhost/index.php?user=1&num=10&format=json


index.php





/* require the user as the parameter */
if(isset($_GET['user']) && intval($_GET['user'])) {

/* soak in the passed variable or set our own */
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
$user_id = intval($_GET['user']); //no default

/* connect to the db */
$link = mysql_connect('DATBASE_HOST','DATABASE_USER','DATABASE_PASSWORD') or die('Cannot connect to the DB');
mysql_select_db('DATABASE_NAME',$link) or die('Cannot select the DB');

/* grab the posts from the db */
$query = "SELECT * FROM user WHERE userID = $user_id ORDER BY orderKey DESC LIMIT $number_of_posts";
$result = mysql_query($query,$link) or die('Errant query: '.$query);

/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
//print_r($post);
$posts[] = array('post'=>$post);
}
}


/* output in necessary format */
if($format == 'json') {
//header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
echo "
";
print_r(json_decode(json_encode(array('posts'=>$posts))));
}
else {
header('Content-type: text/xml');
echo '';
foreach($posts as $index => $post) {
if(is_array($post)) {
foreach($post as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
foreach($value as $tag => $val) {
echo '<',$tag,'>',htmlentities($val),'';
}
}
echo '';
}
}
}
echo '
';
}

/* disconnect from the db */
@mysql_close($link);
}?>

Redirecting www to non-www Using .htaccess

The Apache Web server supports URL rewriting with the mod_rewrite engine. Placing custom rules in an .htaccess file lets you do all sorts of useful things to keep your URLs tidy. One really handy thing you can do for search engines and visitors is redirecting traffic from www to non-www version of your domain (and vice versa).



Some people prefer to use www.somesite.com, but some people prefer the shorter somesite.com. There isn't really a right or wrong way to do it, but whatever you choose you can make sure all of your visitors get sent to the same place. With a few simple rules on the server you can choose from non-www to www, or redirecting from www to non-www.


If you already have a file named .htaccess on your Website you can add to it. If not, create one (yes, include the period at the beginning). Add either of the following rules and save. Replace yourdomain.com with your actual domain name.


Redirect www to non-www:


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Redirect non-www to www:


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]


Both of these rules send a full search engine friendly 301 HTTP redirect. They also preserve the entire URL (so yoursite.com/about redirects to www.yoursite.com/about).


Search engines can usually figure out which format is preferred, but since you're showing the same page for www and non-www URLs it can't hurt to be consistent about it.