เคยสงสัยไหมครับว่าเครื่องมือบางตัวที่เราใช้ในการค้นหาตำแหน่งของ Keywords ว่าอยู่อันดับไหนแล้วในการทำ SEO นั้นมันทำยังไง
รอบนี้เลยพามาพัฒนาภาษา PHP ซึ่งเป็นภาษาโปรแกรม มาเขียนร่วมกับ Google API อีกครั้งซึ่งตัว API ที่เราจะเล่นในตอนนี้คือ AJAX ของ GoogleAPIs ครับซึ่งเป็น Service ของทาง Google ตัวเดียว กับที่ผมเคยนำเสนอในบทความ “
Google Developer API กับการสร้าง Custom Search ด้วย jQuery” มาแล้วนั่นเอง แต่คราวนี้จะเป็นการจับเอาตำแหน่ง Keywords หรือ Keywords Ranking สำหรับใครที่อยากจะทำงาน SEO แล้วมีเครื่องมือเป็นของตัวเองครับ
เริ่มต้นกันเลยดีกว่า
เปิดโปรแกรม Text Editor ตัวไหนก็ได้ครับแล้วสร้างเอกสาร HTML ธรรมดาๆ เพื่อจะเขียนภาษา PHP แทรกเข้าไป เข้าไปที่ API Console ของ Google Developer Network ครับเพื่อไปเอา API Keys ของเรามาใช้กับงานนี้ ที่
https://code.google.com/apis/console ครับ

ต่อมาให้แทรก Code PHP ตามนี้ครับก่อนจะเปิด Tag HTML ด้วย
<?php
error_reporting(0);
Class RankingChecker {
private $googleApiKey = null;
private $googleBaseUrl = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0';
private $checkPageCount = null;
public function __construct($googleApiKey, $checkPageCount = 5) {
$this->googleApiKey = $googleApiKey;
$this->checkPageCount = $checkPageCount;
}
public function check($keywords, $domains) {
$rankings = array();
if (! is_array($keywords)) { throw new Exception('Keywords array is no array'); }
if (! is_array($domains)) { throw new Exception('Domains array is no array'); }
print_r ($keywords);
foreach($keywords as $keyword) {
$keyword = trim($keyword);
$rows = array();
if ($keyword) {
for($i=0;$i<$this->checkPageCount;$i++) {
$start = $i*8;
$url = sprintf('%s&hl=th&gl=th&q=%s&rsz=8&key=%s&start=%s', $this->googleBaseUrl, urlencode($keyword), $this->googleApiKey,
$start);
if ($result = file_get_contents($url)) {
$result = json_decode($result);
$rows = array_merge($rows, $result->responseData->results);
}
}
foreach($domains as $url) {
$rankings[$keyword][$url] = '-';
foreach($rows as $position => $row) {
if (strpos($row->url, trim($url)) !== false) {
$rankings[$keyword][$url] = $position+1;
break;
}
}
}
}
}
return $rankings;
}
}
?>
|
ส่วนที่ต้องพิจารณามากหน่อยคือส่วนบรรทัดนี้ครับ ผมได้ทำการปรับแต่งเป็น Google Thailand Ranking เรียบร้อยแล้ว
$url = sprintf('%s&hl=th&gl=th&q=%s&rsz=8&key=%s&start=%s', $this->googleBaseUrl, urlencode($keyword), $this->googleApiKey, $start);
|
ต่อมาก็แทรก HTML Code เข้าไปตามนี้ครับ
<html>
<head>
<title>Google Ranking Checker</title>
<style type="text/css">
body { font:12px arial,helvetica,sans-serif; padding:20px; }
#info { margin-bottom:20px; }
form { float:left; background:#f1f1f1; padding:20px; background:#f1f1f1; width:380px; }
form textarea { width:100%; }
.user { background:#fff; padding:5px; list-style-type:none; }
h1 { font-size:20px; margin:0; }
h2 { margin:0 0 10px 0; }
button { position:absolute; right:0; bottom:0; font-size:20px; padding:5px 10px; margin:40px; }
#Campaign {
width: 80%;
/*margin: 0px auto;*/
border-collapse: collapse;
border: 1px solid #acacac;
}
#Campaign .rowa {
background: #ebebeb;
height: 20px;
}
#Campaign .rowb {
background: #fff;
height: 20px;
}
</style>
</head>
<body id="body" onLoad="init()">
<div id="info">
<h1>Google Keywords Position Ranking</h1>
</div>
<div style="float:left; width:25%;"><form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<label>Keywords <small>(max. 5)</small>:</label><br/>
<textarea rows="5" cols="50" name="keywords"><?=$_POST["keywords"]?></textarea>
<!--input type="text" name="keywords" value="<?=$_POST["keywords"]?>"/--><br/>
<label>Domains:</label><br/>
<textarea rows="5" cols="50" name="website"><?=$_POST["website"]?></textarea>
<!--input type="text" name="website" value="<?=$_POST["website"]?>"/--><br/>
<br/>
<input type="submit" name="check" value="Check now" />
</form></div>
<div style="float:left; width:580px;; margin-left:200px;">
<table class="table" id="Campaign" cellspacing="0" cellpadding="0">
<?php
if(isset($_POST["check"]))
{
$keywords = explode(",",$_POST["keywords"]);
$website = explode(",",$_POST["website"]);
$RankingChecker = new RankingChecker(' ใส่ API KEYS ของคุณ', 5);
$rankings = $RankingChecker->check($keywords,$website);
$rowi=1;
while(list($key,$val) = each($rankings))
{
$colvar=$rowi++;
if(($colvar%2)==0){
$tb="rowa";
}else{
$tb="rowb";
}
echo '<tr class="'.$tb.'">';
while(list($key1,$val1) = each($val))
{
echo "<td>".$key."</td>";
echo "<td>".$key1."</td>";
echo "<td>".$val1."</td>";
}
echo "</tr>";
}
}
?>
</table>
</div>
</body>
</html>
|
เป็นอันจบ ลองทดลองเล่นตัวเว็บที่เราพึ่งจะสร้างเสร็จดูนะครับ ใส่ Keywords คั่นด้วย “,” เข้าไปไม่เกิน 5 ตัวครับ ตามด้วย URL ของเว็บของเราที่ทำ SEO
 |
Google Keywords Position Ranking |