index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI 自动完成(Autocomplete) - 默认功能</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
<script>
$(function() {
$("#q").autocomplete({source: "search.php" });
});
</script>
</head>
<body>
<div class="ui-widget">
<input id="tags">标签:
<input id="q" name="q" />
</div>
</body>
</html>
search.php
<?php
if(isset($_GET['term']))
{
//在百度里输入a http://suggestion.baidu.com/su?wd=a&action=opensearch
//得到 ["a",["爱奇艺","acfun","安居客","阿里巴巴","a站","阿里云","adobe flash player","阿里巴巴批发网","爱情公寓","爱奇艺网"]] 转换
//["爱奇艺","acfun","安居客","阿里巴巴","阿里云","阿里巴巴批发网","爱情公寓","爱奇艺网","爱乐之城","阿卡丽的神秘商店"]
$wd=$_GET['term'];
//$wd=iconv('UTF-8', 'GB2312//IGNORE', $_GET['term']);
$url='http://suggestion.baidu.com/su?wd='.$wd.'&action=opensearch';
$html = file_get_contents($url);
$html=iconv('GB2312', 'UTF-8//IGNORE', $html); //将字符串的编码从GB2312转到UTF-8
$html=substr($html,4+strlen($_GET['term']),strlen($html)-1-(4+strlen($_GET['term'])) ); //转换一下
echo $html;
// $json = '{"foo": 12345}';
// $obj = json_decode($json);
// print $obj->{'foo'}; // 12345
}
?>