Skip to content

Instantly share code, notes, and snippets.

@jybaek
Last active May 25, 2016 07:39
Show Gist options
  • Save jybaek/85bce6d10a54bfdf73e619cde628ded7 to your computer and use it in GitHub Desktop.
Save jybaek/85bce6d10a54bfdf73e619cde628ded7 to your computer and use it in GitHub Desktop.
실시간 버스 도착 정보 크롤링
<?php
/* FIXME. change strBusNumber !! */
$url = "http://bus.go.kr/xmlRequest/getStationByUid.jsp?strBusNumber=23248";
$ref_url = "";
$data = array();
function curl($url, $ref_url, $data)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $ref_url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
$res = curl_exec ($ch);
$result = explode("\n", $res); // 7
echo " ---------------------------------\n";
for ($i = 0; $i < count($result); $i++) {
if (strstr($result[$i], "rtNm")) {
sscanf($result[$i], "\t\t<rtNm>%d</rtNm>", $bus);
//echo " | Bus : $bus \n";
printf("| %s \n", " Bus : $bus ");
} else if (strstr($result[$i], "arrmsg1")) {
sscanf($result[$i], "\t\t<arrmsg1>%[^<]</arrmsg1>", $msg1);
printf("| %s \n", " 1) $msg1 ");
} else if (strstr($result[$i], "arrmsg2")) {
sscanf($result[$i], "\t\t<arrmsg2>%[^<]</arrmsg2>", $msg2);
printf("| %s \n", " 2) $msg2 ");
} else if (strstr($result[$i], "gpsX")) {
echo " ---------------------------------\n";
}
}
system("date");
return 1;
ob_end_clean();
curl_close($ch);
unset($ch);
}
$result = curl($url, $ref_url, $data);
?>
@jybaek
Copy link
Author

jybaek commented May 25, 2016

run e.g.)
while [ 1 ] ;do clear;php bus_crawler.php;sleep 5;done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment