0

I want to make an Ajax search request agains an API and get the data returned to my PHP file, right now I'm using Javascript and jQuery to do the job. But I want to let PHP do all the job, simply because I don't want my API key to be public and I may want to cash the data in a database further on. It seems that it should be simple, but I just can't figure out how to do it clean, call javascript and return or how to "integrate" it with PHP.

I am doing my PHP in the MVC pattern, like so:

Controller called from "mastercontroller/index":

class SearchController {

    public function DoControl($view, $model) {
        $ret = "";

        $ret .= $view->GetSearchForm();

        if($view->TriedToSearch()) {
            if($view->GetSearchString()) {
                $ret .= $model->CheckSearchString($view->GetSearchString());
            } else {
                // Didn't search for anything
            }
        } else {
            // Didn't press the search button
        }

        return $ret;
    }
}

My view is returning an HTML form, checking if submit is pressed and also returning the searchstring, that I am sending in to my Model above.

Model:

class SearchModel {

    public function CheckSearchString($searchString) {
        // 1. Call Googlebooks api with the searchstring
        // 2. Get JSON response to return to the controller
        // 3. The controller sends the data to the View for rendering
    }
}

I just can't figure out how I should do it.

oskarno
  • 126
  • 1
  • 2
  • 8

2 Answers2

1

I'm not entirely sure, but you seem to be asking how to perform an AJAX request without JavaScript. You can't do that -- it's not possible to use the XmlHttpRequest object without JavaScript. That's, according to legend, the origin of the "J" in the AJAX name.

It sounds like you need to use REST to call specific API's. RESTful state allows you to use web services to return specific data according to a predefined API. Data can be returned in XML or JSON.

You can do this very easily with PHP's cURL implementation using whatever keys Google gives you.

See Google's Google Books API Family page for links to PHP API and sample code.

Ian Atkin
  • 6,302
  • 2
  • 17
  • 24
  • I've seen this before, but I thought it could be made without cURL. Is there any downside doing it all i js besides what I've prompt above? What are the "recommendations" of AJAX usage? – oskarno Jan 06 '13 at 13:14
  • 1
    The recommendations are: don't use `eval` to parse JSON, there are safer ways to do that. Better yet, use a library or framework that has AJAX support. jQuery is good for this, and very easy to use. Downsides to using JavaScript: 1. as you say, your code is available for anyone to copy, but using minifying and munging techniques makes it very hard for anyone to re-use your JavaScript easily; 2. the cross-site scripting limitation means that you can't use AJAX across different domains. That's why using a web service is your only option in most cases. – Ian Atkin Jan 06 '13 at 19:15
-1

Would the below practice be useful to you? If you just want to implement ajax functionality in your code.

Simple AJAX - PHP and Javascript

I am sorry for mistaking the content. How about the two below:

simple http request example in php
PHP HTTP-Request * from another stackoverflow question

But I think ajax is main in client program meaning. At the server-side, just call it http request.

Community
  • 1
  • 1
tech_me
  • 540
  • 2
  • 7
  • 20
  • Better links than the first one, thanks. So it's HTTP Request on server-side, or Ajax on client-side? Is there any downside doing all this in Javascript? Except for caching heavy data and "hide" your API keys? – oskarno Jan 06 '13 at 13:07
  • I think @IanAtkin gives your the just right advice. But for what you are doing now(by using ajax in client), maybe call it web2.0 would be suitable, as in the web2.0, ajax is used in many cases if you designed your program in a right way. – tech_me Jan 06 '13 at 13:30
  • I'll give him the right answear then. But yes, I'm not sure if I need to store anything heavy actually, I think Web Storage will do. I also wrote the js so that I could bookmark, so then it's just the API key that is public... Seems that if I could hide it somehow, the disadvantages of Ajax is reduced. – oskarno Jan 06 '13 at 13:44
  • Well, if you could implement the most functionality at the server-side, you just need one call for each specified task(or module else). Compare to older technique way, I think the main advantage of Ajax is end user experience(updating without page refresh). – tech_me Jan 06 '13 at 13:54