وباکا

آموزش تخصصی برنامه نویسی وب

در این قسمت یک سرویس وب ابتدایی با زبان پی اچ پی(PHP) و بانک اطلاعاتی MySQL ایجاد کرده ایم که پاسخ را با فرمت XML یا JSON تولید می کند.

این وب سرویس بر اساس پارامترهای ارسال شده به آن، به جدول wp_posts از سایت متصل شده (wp_posts یکی از جداول وردپرس است که نوشته های سایت را نگهداری می کند) و تعداد درخواستی از پست های مربوط به کاربر تعیین شده را، با فرمت درخواستی تولید می کند.

<?php
$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
/* connect to the db */
$link = mysql_connect('localhost','root','') or die('Cannot connect to the DB');
mysql_select_db('wp47',$link) or die('Cannot select the DB');
/* grab the posts from the db */
$query = "SELECT post_title, guid FROM wp_posts WHERE post_status = 'publish' ORDER BY ID 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)) {
$posts[] = array('post'=>$post);
}
}
/* output in necessary format */
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts'=>$posts));
}
else {
header('Content-type: text/xml');
echo '<posts>';
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),'</',$tag,'>';
}
}
echo '</',$key,'>';
}
}
}
echo '</posts>';
}
/* disconnect from the db */
@mysql_close($link);
?>

با توجه به اینکه افرادی ممکن است قصد حمله به وب سرویس شما را داشته باشند، لازم است قبل از دستورات اتصال به پایگاه داده، برای جلوگیری از حملات تزریق،  اعتبار سنجی کافی، انجام دهید. هنگامی هم که نتایج دلخواه را از پایگاه داده دریافت کردیم، آنها را در آرایه ای قرار می دهیم. بسته به نوع پاسخ درخواستی مورد نظر، هدر و محتوای مناسب را در فرمت دلخواه به خروجی می فرستیم.

برای اتصال به این وب سرویس،  به عنوان مثال می توانید آدرس اینترنتی زیر را وارد کنید:

http://127.0.0.1/wp47/ws.php?format=json&&num=10

خروجی json می تواند بصورت زیر باشد:

{"posts":[{"post":{"post_title":"SSLmatic SSL Certificate Giveaway Winners","guid":"http:\/\/davidwalsh.name\/?p=2304"}},{"post":{"post_title":"MooTools FileManager","guid":"http:\/\/davidwalsh.name\/?p=2288"}},{"post":{"post_title":"PHPTVDB: Using PHP to Retrieve TV Show Information","guid":"http:\/\/davidwalsh.name\/?p=2266"}},{"post":{"post_title":"David Walsh: The Lost MooTools Plugins","guid":"http:\/\/davidwalsh.name\/?p=2258"}},{"post":{"post_title":"Create Short URLs Using U.Nu","guid":"http:\/\/davidwalsh.name\/?p=2218"}},{"post":{"post_title":"Create Bit.ly Short URLs Using PHP","guid":"http:\/\/davidwalsh.name\/?p=2194"}},{"post":{"post_title":"Represent Your Repositories Using the GitHub Badge!","guid":"http:\/\/davidwalsh.name\/?p=2178"}},{"post":{"post_title":"ZebraTable","guid":"http:\/\/davidwalsh.name\/?page_id=2172"}},{"post":{"post_title":"MooTools Zebra Table Plugin","guid":"http:\/\/davidwalsh.name\/?p=2168"}},{"post":{"post_title":"SSLmatic: Quality, Cheap SSL Certificates and Giveaway!","guid":"http:\/\/davidwalsh.name\/?p=2158"}}]}

و در صورتی که با آدرس زیر درخواست شود:

http://127.0.0.1/wp47/ws.php?format=xml&&num=10

می تواند خروجی به صورت زیر داشته باشد:

<posts>
<post>
<post_title>SSLmatic SSL Certificate Giveaway Winners</post_title>
<guid>https://davidwalsh.name/?p=2304</guid>
</post>
<post>
<post_title>MooTools FileManager</post_title>
<guid>https://davidwalsh.name/?p=2288</guid>
</post>
<post>
<post_title>PHPTVDB: Using PHP to Retrieve TV Show Information</post_title>
<guid>https://davidwalsh.name/?p=2266</guid>
</post>
<post>
<post_title>David Walsh: The Lost MooTools Plugins</post_title>
<guid>https://davidwalsh.name/?p=2258</guid>
</post>
<post>
<post_title>Create Short URLs Using U.Nu</post_title>
<guid>https://davidwalsh.name/?p=2218</guid>
</post>
<post>
<post_title>Create Bit.ly Short URLs Using PHP</post_title>
<guid>https://davidwalsh.name/?p=2194</guid>
</post>
<post>
<post_title>Represent Your Repositories Using the GitHub Badge!</post_title>
<guid>https://davidwalsh.name/?p=2178</guid>
</post>
<post>
<post_title>ZebraTable</post_title>
<guid>https://davidwalsh.name/?page_id=2172</guid>
</post>
<post>
<post_title>MooTools Zebra Table Plugin</post_title>
<guid>https://davidwalsh.name/?p=2168</guid>
</post>
<post>
<post_title>SSLmatic: Quality, Cheap SSL Certificates and Giveaway!</post_title>
<guid>https://davidwalsh.name/?p=2158</guid>
</post>
</posts>

البته اطلاعات با توجه به اطلاعات داخل بانک اطلاعاتی شما تعیین خواهد شد.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

فهرست مطالب