ซื้อขายบ้านมือสอง

วันพุธที่ 30 เมษายน พ.ศ. 2551

การใช้ curl extension สำหรับ ดึงส่วนต่างๆ ของหน้าเว็บ จากที่อื่น มาแสดงผลในหน้าเว็บเรา

บทความนี้นะครับ ก็เป็นการ ใช้ extension cure เพื่อ ติดต่อ กับ URL อื่น และ ตัดข้อความบางส่วนออกมาใช้นะครับ
การใช้ cure นั้น จะมีลักษณะ คล้ายๆ กับ ajax คือ cure สามารถติดต่อ และส่ง ค่าตัวแปร ด้วย get หรือ post ไปยัง url อื่น และรับข้อมูลประมวลผล กลับมาแสดงในหน้าเว็บได้ ซึ่ หากเป้น ajax process การทำงานนั้นจะ อยู่ที่ agent ฝั่ง client นั่นคือ object xmlHTTPRequest นะครับ
แต่หากเป็น cure นั้น agent หรือตัวกลาง จะอยู่ที่ฝั่ง server ภายใต้ environment ของ PHP ที่เปิด extension cure ใน php.ini นะครับ
อันดับแรก ผมอยากจะให้ดูวิธีการใช้ cure ซักนิด ด้วยตัวอย่างง่ายๆ เช่นการ Login
โจทย์ของผมคือ สร้าง ฟอร์ม Login ขึ้นมา และส่งค่า username และ password ไปยัง ไฟล์ curl_service.php ซึ่ง จะให้บริการ ตรวจสอบ username และ password จาก database ซึ่ง ฟอร์ม Login และ curl_service.php นั้น จะอยู่กัน คนละ server และ server ของ formLogin.php นั้นไม่สามารถติดต่อ database ได้

ที่ server 1 (ไม่สามารถติดต่อ Db ได้ แต่เปิด curl) ไฟล์ formLogin.php โคดดังนี้


<?php
$submit=$_POST["Submit"];
$curlurl ="http://server2/curl/curl_service.php"; // url เพื่อเรียก ไปที่ curl_service ที่อยู่ใน server ที่สามารรถติดต่อดาต้าเบสได้



function reg_login($username,$password)
{
global $curlurl;
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$params = "username=$username&password=".$password;



$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST,1); // method ที่เราจะส่ง เป็น get หรือ post
curl_setopt($ch, CURLOPT_POSTFIELDS,$params); // paremeter สำหรับส่งไปยังไฟล์ ที่กำหนด
curl_setopt($ch, CURLOPT_URL,$curlurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$result = curl_exec($ch); // ผลการ execute กลับมาเป็น ข้อมูลใน url ที่เรา ส่งคำร้องขอไป
curl_close ($ch);
return $result;
}
?>



<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>



<BODY>
<form action="<?php $_SERVER["PHP_SELF"];?>" method="post">
<table width="500" border="0" align="center" cellpadding="1" cellspacing="1" bgcolor="#CCCCCC">
<tr bgcolor="#CCFFFF">
<td colspan="2"><strong>Curl Login Form !!</strong></td>
</tr>
<tr>
<td width="127" bgcolor="#FFFFFF"><strong>username</strong></td>
<td width="366" bgcolor="#FFFFFF"><input type="text" name="txtname"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><strong>password</strong></td>
<td bgcolor="#FFFFFF"><input type="text" name="txtpass"></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td bgcolor="#FFFFFF"><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
<?php
// ตรวจสอบ การกดปุ่ม submit
if (isset($submit)){
$username=$_POST["txtname"];
$password=$_POST["txtpass"];
echo reg_login($username,$password);
}
?>
</BODY>
</HTML>




ที่ server 2 (สามารถติดต่อ Db ได้ ไม่ได้เปิด curl) ไฟล์ curl_service.php โคดดังนี้


<?php
// รหัส login
$admin_user="admin";
$admin_pass="123";


// เว็บ service โดยใช้ curl เป็นหลัก
$user=$_POST["username"];
$pass=$_POST["password"];


if ($admin_user==$user && $admin_pass==$pass) {
echo "Login ถูกต้องนะครับ ยินดีต้อนรับคุณ ".$user;
}else{
echo "ไม่ถูกต้องกรุณา Login ใหม่ด้วยนะครับ !!";
}

จะเห็นได้ว่า ใน โคดที่ curl_service.php คือการเขียนโคด ตรวจสอบ login ธรรมดา ทั่วไป และมีการ echo ข้อความเท่านั้น
ซึ่งทั้งหมดนี้ process ของการ ส่งค่า และรับค่าไปแสดงผล จะอยู่ที่กระบวนการทางฝั่งของ formLogin.php โดยผ่านตัวกลาง agent ก็คือ curl นั่นเอง

ที่กล่าวมานี้ จะเป็น แค่ตัวอย่าง การใช้ curl เป็นตัวกลางในการส่งค่า ไปยังเว็บอื่น เพื่อ แสดงผลข้อมูล ต่อไป เราจะ ได้ทดลอง การตัดต่อ ข้อความเฉพาะส่งจาก เว็บไซต์อื่นนะครับ ด้วย ตัวอย่าง การ ดึง บทความจากเว็บไซต์ เมืองนอก มาแสดงผลในหน้าเว็บเรา โดยการ ตัดเอาเฉพาะ ข้อมูล ตรง ส่วน main บทความเท่านั้น นะครับ


<?php
$url="http://www.4guysfromrolla.com/webtech/";
if ($viewcat<>"")
$url.=$viewcat;
else
$url.="index_asp.shtml";




function replaceLink($data){
$data = ereg_replace('<a href="/webtech/','<a href="aspnet_article.php?viewcat=',$data);
return $data;
}



function callArticle(){
global $url;
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
// $params = "";
// $url .="?".$params;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST,0);
//curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);

$result = curl_exec($ch);
$start_result = strpos($result,"<!--content_start-->");



$end_result = strpos($result,"<P><CENTER>",$start_result+11);
$substring .= substr($result,$start_result,$end_result-$start_result);
curl_close ($ch);
return replaceLink($substring);
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
</HEAD>



<BODY>
<h2>การตัดต่อ Article http://www.4guysfromrolla.com/webtech/index_asp.shtml <br>ผลลัพธ์ที่ได้คือ</h2><hr>
<?php
echo callArticle();
?>
</BODY>
</HTML>




จะเห็นว่า เมื่อ curl ไปดึงบทความจากเว็บไซต์ตัวอย่างมาแล้ว ผมจะทำการ ใช้ function strpos ของ php เพื่อ ค้นหา เอา tag ที่ เป็นส่วน content ที่ผมต้องการ โดยการ find หา จุด string start และ หา string end ก็คือ บริเวณที่ tag ของ content สิ้นสุด ที่เราต้องการ
เราก็จะได้ข้อมูล บริเวณ ภายใต้แทก start และ end ของข้อมูลครับ
ทั้งนี้ ก่อนที่เราจะหา content start ได้ เราจำเป็นจะต้อง มอง content ของเว็บที่เราต้องการ ให้เป็น Dom เพื่อหา บริเวณ แทก ที่เป็นจุดเริ่มของ content ที่เราต้องการ มาครับ และ มองหา บริเวณ ของ dom จุดที่เป็น จุดสิ้นสุด ของ content ที่เราต้องการ เพื่อมาตัดต่อเอา ข้อมูลเฉพาะนะครับ

full screen browser, javascript close window opener not show Confirmation Boxes

การใช้ javascript เพื่อให้หน้าเว็บ เต็มหน้า โดยใช้ window.open และ ให้ทำการ ปิดหน้าต่าง window หลัก ซึ่ง ปกติ การปิดหน้าต่าง window หลัก นั้นจะพบว่า จะเกิด confirmation Box ขึ้นมา ให้เราตอบ yes ,no นะครับ
สำหรับ ในบทความนี้ จะเป็นการ สั่งให้ทำการปิดหน้าต่าง window หลัก โดยที่ไม่ต้องให้ แสดง confirmation Box ให้ ลำคาญใจ นะครับ (ใช้ได้ดีใน IE)

<script language="javascript">
<!--
function fnCloseWindow() {
if (navigator.appName != "Microsoft Internet Explorer")
{ // หากไม่ใช่ IE
window.open('','_parent','');
window.close();
}else{ // หากเป็น IE
window.opener=null;
window.close();
}
}
//-->
</script>

และเราสามารถ เขียนออกมา ได้ดังนี้


<html>
<script>
function openMainWindow(url) {
var options = 'resizable=1,scrollbars=1,menubar=0,titlebar=0,status=1,copyhistory=0,directories=0,location=0';
options += ',width=' + (screen.availWidth) + ',height=' + (screen.availHeight);
options += ',screenX=0,screenY=0,top=0,left=0';
var randomNumber=Math.floor(Math.random()*10000);
var win = window.open(url, randomNumber, options);
win.focus();
}


function fnCloseWindow() {
if (navigator.appName != "Microsoft Internet Explorer")
{ // หากไม่ใช่ IE
window.open('','_parent','');
window.close();
}else{ // หากเป็น IE
window.opener=null;
window.close();
}
}
</script>
<body onload="openMainWindow('http://www.thaieasy2home.com');fnCloseParentWindow();">


</body>
</html>


javascript สำหรับ random banner มาแสดง แบบง่ายๆ

บทความนี้ พอดีเพื่อนผม เค้าอยากจะ ได้ script random banner มาแสดง แบบง่ายๆ นะครับ ซึ่งผม ก็เลือกใช้ javascript ในการแสดงผล random นะครับ
script นั้นก็มีเพียงสั้นๆ ดังนี้นะครับ

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="javascript">
// ฟังชั่น random
function get_random()
{
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}
// ฟังชั่น จัด queue
function getQueue(frameId)
{
var frame = document.getElementById(frameId);
var whichQuote=get_random();
var quote=new Array(5)
quote[0]="http://www.sanook.com/"; // ไฟล์ แบนเนอร์ ที่ 1
quote[1]="http://www.kapook.com/"; // ไฟล์ แบนเนอร์ ที่ 2
quote[2]="http://www.thaieasy2home.com/"; // ไฟล์ แบนเนอร์ ที่ 3
quote[3]="http://www.google.com/"; // ไฟล์ แบนเนอร์ ที่ 4
quote[4]="http://www.yahoo.com/"; // ไฟล์ แบนเนอร์ ที่ 5

frame.src=quote[whichQuote]; // แสดงผล Iframe
}
// ปรับขนาด Iframe
function autoIframe(frameId){
try{
frame = document.getElementById(frameId);
innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
objToResize = (frame.style) ? frame.style : frame;
objToResize.height = innerDoc.body.scrollHeight + 30;
}
catch(err){
window.status = err.message;
}
}
</script>
</HEAD>


<BODY>
<iframe id="frame" name="frame" frameborder="0" scrolling="no" height="60" width="468" marginwidth="0" marginheight="0" onload="getQueue('frame');autoIframe('frame');"></iframe>
</BODY>
</HTML>


หากต้องการเพิ่ม ให้ script สามารถ ตั้งเวลาในการ random เองอัตโนมัติ ก็เพียงแค่ เพิ่ม setTimeout ลงไปนะครับ

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script language="javascript">
// ฟังชั่น reload มี parameter 2 ตัวคือ t=วินาที , u = URL
function reload_view(t,frame) {

setTimeout("getQueue('frame');",(t * 1000));
}

// ฟังชั่น random
function get_random()
{
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}


// ฟังชั่น จัด queue
function getQueue(frameId)
{
var frame = document.getElementById(frameId);
var whichQuote=get_random();
var quote=new Array(5) // ใส่จำนวน ของ banner ที่ต้องการนำมา random
quote[0]="http://www.sanook.com"; // ไฟล์ แบนเนอร์ ที่ 1
quote[1]="http://www.kapook.com"; // ไฟล์ แบนเนอร์ ที่ 2
quote[2]="http://www.thaieasy2home.com"; // ไฟล์ แบนเนอร์ ที่ 3
quote[3]="http://www.google.com"; // ไฟล์ แบนเนอร์ ที่ 4
quote[4]="http://www.yahoo.com"; // ไฟล์ แบนเนอร์ ที่ 5

frame.src=quote[whichQuote]; // แสดงผล Iframe
}
// ปรับขนาด Iframe
function autoIframe(frameId){
try{
frame = document.getElementById(frameId);
innerDoc = (frame.contentDocument) ? frame.contentDocument : frame.contentWindow.document;
objToResize = (frame.style) ? frame.style : frame;
objToResize.height = innerDoc.body.scrollHeight + 30;
}
catch(err){
window.status = err.message;
}
}
</script>
</HEAD>


<BODY>
<iframe id="frame" name="frame" frameborder="0"
scrolling="no" height="60" width="468" marginwidth="0" marginheight="0"
onload="reload_view(10,'frame');autoIframe('frame');"></iframe>
</BODY>
</HTML>