1

I have a Google Instant style search script written in jQuery which pulls results from the JSON BingAPI. How can I make my script pull content from a PHP script rather than the BingAPI?

Here is my code:

$(document).ready(function(){
    $("#search").keyup(function(){
        var search=$(this).val();
        var keyword=encodeURIComponent(search);
        var yt_url='http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=?&Appid=642636B8B26344A69F5FA5C22A629A163752DC6B&query='+keyword+'&sources=web';
        window.location.hash=keyword;

        $.ajax({
            type:"GET",
            url:yt_url,
            dataType:"jsonp",
            success:function(response){
                $("#result").html('');
                if(response.SearchResponse.Web.Results.length){
                    $.each(response.SearchResponse.Web.Results, function(i,data){
                        var title=data.Title;
                        var dis=data.Description;
                        var url=data.Url;
                        var final="<div class='webresult'><div class='title'><a href='"+url+"'>"+title+"</a></div><div class='desc'>"+dis+"</div><div class='url'>"+url+"</div></div>";
                        $("#result").append(final);
                    });
                }
            }
        });
    });
});
1
  • Do you still want to use Bing API for actual search or you're asking how to invoke your own custom script that does searches based on your own database? If you just want to be able to query something of your own, you change the URL, but if you want your PHP script to query the API of the 3rd party - you need to create a proxy PHP script. Commented Jun 4, 2011 at 19:42

1 Answer 1

2

Replace this:

var yt_url='http://api.search.live.net/json.aspx?JsonType=callback&JsonCallback=?&Appid=642636B8B26344A69F5FA5C22A629A163752DC6B&query='+keyword+'&sources=web';

With your json suggest url

var yt_url='http://yourwebsite/json.php?query='+keyword;
7
  • explain does not work, gosh. Also just see the output of bing json and compare with yours too see if there are any differences. Commented Jun 4, 2011 at 19:52
  • see the second part of my comment Commented Jun 4, 2011 at 19:53
  • look at the html. (view-source) Commented Jun 4, 2011 at 19:56
  • @callum yes you are right because they accept requests only coming from AJAX. Anyway the problem could be because you are outtputting the wrong json. You should see from the console if there are any errors Commented Jun 4, 2011 at 20:02
  • @callum: that's the problem! acutally on bing the file is called json.ajax because outputs json! :) not html. You should output json too. see json_encode(); Commented Jun 4, 2011 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.