0

你好堆栈溢出,

我正在尝试创建一个搜索功能来搜索搜索 API 上的事件。API 可以在这里查看:http ://build.uitdatabank.be/api/events/search?key=AEBA59E1-F80E-4EE2-AE7E-CEDD6A589CA9&q =opera 如果您搜索单词“opera”,这是输出 XML .

我仍然是 PHP 的菜鸟,所以我很难过。我使用本教程/示例作为帮助:http ://designm.ag/tutorials/deviantart-api-instant-search-app-using-ajax -php/

所以我构建了我的代码,每次在文本区域中输入关键字时,我都会收到错误“错误:解析器错误”。谁能帮我解决这个问题?

这是我的 HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Culture Reminder</title>

    <!-- Stylesheets -->
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/style.css">


    <!-- Scripts -->

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <!-- <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> -->
    <script type="text/javascript" src="uitdatabase.js"></script>

    <!-- HTML5 Shiv -->
    <!--[if lt IE 9]>
            <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>

    <header>
        <h1>Search events</h1>
    </header><!-- /header -->

    <div id="container">

        <div id="searchform">
        <h2>Search for culture</h2>
        <input id="s" name="s" placeholder="Type in keyword(s)" size="30" type="text" /> 
        </div><!-- /searchform -->
        <center id="loader"><img src="images/loader.gif" alt="loading..."></center>

    <div id="content">

    </div><!-- /content -->



    </div><!-- /container -->

    <div id="footer">
        <div id="nav">  
                <a href="#" title="Search"><img src="images/searchbutton.gif" /></a>
                <a href="#" title="My List"><img src="images/mylistbutton.gif" /></a>
                <a href="#" title="Settings"><img src="images/settingsbutton.gif" /></a>
         </div>
    </div><!-- /footer -->
</body>
</html>

这是Javascript文件:

    $(document).ready(function(){
      var s = $("#s");
      var wrap = $("#content");
      var delay = 1500;
      var keycode;
      var timer;

      $(s).keydown(function(e){
        // clear old page content and reset timer
        $(wrap).empty();    
        clearTimeout(timer);
      });

        $(s).keyup(function(){
          $("#loader").css("display", "block");
        timer = setTimeout(startSearch, delay); // end timeout() function  
      }); // end keyup() event function

      function startSearch() {
        $.ajax({
              type: 'POST',
              url: 'ajax.php',
              data: "q="+$(s).val(),
              success: function(data){
                // hide the loader and blur focus away from input
                $("#loader").css("display", "none");
                $(s).blur();

                  var code = "<span class=\"results\">Total Results: "+data['total']+"</span>";

                  $.each(data, function(i, item) {
                    if(typeof(data[i].title) !== 'undefined') { // check if data is undefined before setting more variables
                        code = code + '<div class="listing clearfix"><header><h3>'+data[i].title+'</h3><span class="userdata"><span class="date">'+data[i].calendarsummary+'</span>'+'<span class="location">' + data[i].location+'</span>'+'<span class="description">'+ data[i].shortdescription+'</span></span></header></div>';
                        code = code + '<img src="'+data[i].thumbnail+'" class="thumbnail">';
                    } 
                  });
                  $(wrap).html(code);
              },
              error: function(xhr, type, exception) { 
                $("#loader").css("display", "none");
                  $(wrap).html("Error: " + type); 
              }
          }); // end ajax call  
      }
    }); // end ready() function

这是我的 ajax.php 代码:

<?php
header('Content-Type: application/json');

$query = urlencode(stripslashes($_POST['q']));

$keywords = $query;
$xmlurl   = "http://http://build.uitdatabank.be/api/events/search?key=AEBA59E1-F80E-4EE2-AE7E-CEDD6A589CA9&q=".$keywords;

$xmlrss = file_get_contents($xmlurl);
$xmlrss = preg_replace('#&(?=[a-z_0-9]+=)#', '&amp;', $xmlrss);
$object = simplexml_load_string($xmlrss);

// setup return array
$return = array();
$i = 0;

// get total number of results, max 60
$total = count($object->list->item);
$return["total"] = $total;


foreach($object->list->item as $item) {
    $title = (string) $item->title;
    $date   = (string) $item->calendarsummary; //url 

    $loc = (string) $item->location; // authorname
    $desc  = (string) $item->shortdescription;//authorpic
    $thumburl = (string) $item->thumbnail;//authorpic   


    // configure array data for each item
    $return[$i]["title"]  = $title;
    $return[$i]["calendarsummary"]    = $date;
    $return[$i]["location"] = $loc;
    $return[$i]["shortdescription"] = $desc;
    $return[$i]["thumbnail"]  = $thumburl;  
    $i++;
}

$json = json_encode($return);
die($json);
?>

如果有人可以帮助我,我将不胜感激。这是一个学校项目,我仍在学习基本的 PHP。

4

1 回答 1

0

Try this code

<?php

header('Content-Type: application/json');

if(isset($_POST['q']))
{


    $query = urlencode(stripslashes($_POST['q']));

    $keywords = $query;
    $xmlurl   = "http://build.uitdatabank.be/api/events/search?key=AEBA59E1-F80E-4EE2-AE7E-CEDD6A589CA9&q=".$keywords;

    $xmlrss = file_get_contents($xmlurl);
    $xmlrss = preg_replace('#&(?=[a-z_0-9]+=)#', '&amp;', $xmlrss);
    $object = simplexml_load_string($xmlrss);

    // setup return array
    $return = array();
    $i = 0;

    if(isset($object->list->item))
    {   
        // get total number of results, max 60
        $total = count($object->list->item);
        $return["total"] = $total;

        if($total>0)
        {
            foreach($object->list->item as $item) {
                $title = (string) $item->title;
                $date   = (string) $item->calendarsummary; //url 

                $loc = (string) $item->location; // authorname
                $desc  = (string) $item->shortdescription;//authorpic
                $thumburl = (string) $item->thumbnail;//authorpic   


                // configure array data for each item
                $return[$i]["title"]  = $title;
                $return[$i]["calendarsummary"]    = $date;
                $return[$i]["location"] = $loc;
                $return[$i]["shortdescription"] = $desc;
                $return[$i]["thumbnail"]  = $thumburl;  
                $i++;
            }

            $json = json_encode($return);
            die($json);

        }else{
            //count is empty
        }

    }else{
        //object is not ready
    }
}else{
    //no post data found
}
于 2013-05-17T05:24:01.530 回答