简体   繁体   English

进行Ajax GET请求以将数据返回到PHP

[英]Doing an Ajax GET request to return data to PHP

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. 我想再次提出一个Ajax搜索请求并获得一个API,并将数据返回到我的PHP文件中,现在我正在使用Javascript和jQuery来完成这项工作。 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. 但是我想让PHP来完成所有工作,只是因为我不想公开我的API密钥,而我可能想进一步兑现数据库中的数据。 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. 看起来应该很简单,但是我只是想不通如何清理,调用javascript并返回或者如何将其与PHP“集成”。

I am doing my PHP in the MVC pattern, like so: 我正在以MVC模式进行PHP,如下所示:

Controller called from "mastercontroller/index": 从“ 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. 我的视图正在返回HTML表单,检查是否按下了Submit,并且还返回了要发送到上面的Model中的searchstring。

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. 我只是不知道该怎么办。

I'm not entirely sure, but you seem to be asking how to perform an AJAX request without JavaScript. 我不确定,但是您似乎在问如何在没有JavaScript的情况下执行AJAX请求。 You can't do that -- it's not possible to use the XmlHttpRequest object without JavaScript. 您无法做到这一点-如果没有JavaScript,就无法使用XmlHttpRequest对象。 That's, according to legend, the origin of the "J" in the AJAX name. 根据传说,这就是AJAX名称中“ J”的来源。

It sounds like you need to use REST to call specific API's. 听起来您需要使用REST来调用特定的API。 RESTful state allows you to use web services to return specific data according to a predefined API. RESTful状态允许您使用Web服务根据预定义的API返回特定数据。 Data can be returned in XML or JSON. 数据可以XML或JSON返回。

You can do this very easily with PHP's cURL implementation using whatever keys Google gives you. 您可以使用Google为您提供的任何键,通过PHP的cURL实现非常轻松地完成此操作。

See Google's Google Books API Family page for links to PHP API and sample code. 请参阅Google的Google Books API系列页面,以获取指向PHP API和示例代码的链接。

Would the below practice be useful to you? 以下做法对您有用吗? If you just want to implement ajax functionality in your code. 如果您只想在代码中实现Ajax功能。

Simple AJAX - PHP and Javascript 简单的AJAX-PHP和Javascript

I am sorry for mistaking the content. 很抱歉误认了内容。 How about the two below: 下面的两个怎么样:

simple http request example in php PHP中的简单HTTP请求示例
PHP HTTP-Request * from another stackoverflow question PHP HTTP请求 *来自另一个stackoverflow问题

But I think ajax is main in client program meaning. 但是我认为ajax是客户端程序含义中的主要内容。 At the server-side, just call it http request. 在服务器端,只需将其称为http请求即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM