如何在不使用外部php文件的情况下执行ajax(jquery)功能

时间:2021-05-05 09:51:16

Current scenario:

目前的情况:

I'm using the gmail oauth api to receive emails on a page. It's slow to load many, so I want to post each email on the page as it loads giving the user a chance to do other things on the site while the emails are still loading.

我正在使用gmail oauth api在页面上接收电子邮件。加载很多很慢,所以我想在页面上发布每封电子邮件,因为它会加载,让用户有机会在电子邮件仍在加载时在网站上做其他事情。

There are a few files required

需要一些文件

require_once 'common.php';
require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Crypt/Rsa/Key/Private.php'; 
require_once 'Zend/Mail/Protocol/Imap.php';
require_once 'Zend/Mail/Storage/Imap.php';
require_once 'Zend/Mail.php';

And a few functions on the main page that helps the php run. I am familiar with using the ajax call on jquery to call an external php file. I would like to be able instantiate php code on this page to function using ajax functionality so I don't need to worry about calling these required files and functions each time I check a new email. Is there a way to do this?

主页上的一些函数可以帮助php运行。我熟悉在jquery上使用ajax调用来调用外部php文件。我希望能够在此页面上实例化php代码以使用ajax功能运行,因此每次检查新电子邮件时我都不必担心调用这些必需的文件和函数。有没有办法做到这一点?

  for ($i = $storage->countMessages(); $i >= ($storage->countMessages()-30); $i-- ){ 
 { 
  echo '<li>' . $storage->getMessage($i)->subject . '</li>'; 
 }

  } 

Is the function I would like to function on the fly and return each subject one at a time to load on the screen. I assume I'll need to create a for loop using javascript, but the main issue is being able to use the php on the page so that I don't have to recall the includes each time. Maybe I'm curious about changing the scope of these variables, maybe the solution is being able to operate the ajax from this page without an external script - I'm not sure, but any help would be appreciated.

我希望功能是在运行中并一次返回一个主题以加载到屏幕上。我假设我需要使用javascript创建一个for循环,但主要问题是能够在页面上使用php,这样我就不必每次都回忆一下include。也许我很想改变这些变量的范围,也许解决方案是能够在没有外部脚本的情况下从这个页面操作ajax - 我不确定,但任何帮助都会受到赞赏。

1 个解决方案

#1


0  

Firstly, don't worry about the performance of 6 includes. This something you start to worry about when your site is getting massive. Having said that, utilizing an opcode cache such as APC can reduce the performance cost of including large numbers of files.

首先,不要担心6包括的性能。当您的网站变得庞大时,您会开始担心这个问题。话虽如此,利用APC等操作码缓存可以降低包含大量文件的性能成本。

To your question: every request to PHP is it's own entity and cannot reference code included in another request. Your original request and each subsequent AJAX request are all separate and distinct requests that know nothing of each other and will all require PHP to load all files to load emails.

对于您的问题:对PHP的每个请求都是它自己的实体,不能引用另一个请求中包含的代码。您的原始请求和每个后续的AJAX请求都是彼此无关的独立且不同的请求,并且都需要PHP加载所有文件以加载电子邮件。

I suggest you look at other ways to improve performance. Given loading emails from gmail is slow, perhaps you could investigate using a daemon (a service that runs continually in the background on your server) that synchronizes the emails into a database. Your page can then just get everything it needs from the database which will be much faster for the user. You can use AJAX to check the database periodically for new data. Meanwhile your daemon will be doing the hard-work in the background.

我建议你看看提高性能的其他方法。鉴于从gmail加载电子邮件的速度很慢,您可以使用守护程序(在服务器后台持续运行的服务)进行调查,以便将电子邮件同步到数据库中。然后,您的页面可以从数据库中获取所需的一切,这对用户来说会更快。您可以使用AJAX定期检查数据库以获取新数据。与此同时,你的守护进程将在后台进行艰苦的工作。

#1


0  

Firstly, don't worry about the performance of 6 includes. This something you start to worry about when your site is getting massive. Having said that, utilizing an opcode cache such as APC can reduce the performance cost of including large numbers of files.

首先,不要担心6包括的性能。当您的网站变得庞大时,您会开始担心这个问题。话虽如此,利用APC等操作码缓存可以降低包含大量文件的性能成本。

To your question: every request to PHP is it's own entity and cannot reference code included in another request. Your original request and each subsequent AJAX request are all separate and distinct requests that know nothing of each other and will all require PHP to load all files to load emails.

对于您的问题:对PHP的每个请求都是它自己的实体,不能引用另一个请求中包含的代码。您的原始请求和每个后续的AJAX请求都是彼此无关的独立且不同的请求,并且都需要PHP加载所有文件以加载电子邮件。

I suggest you look at other ways to improve performance. Given loading emails from gmail is slow, perhaps you could investigate using a daemon (a service that runs continually in the background on your server) that synchronizes the emails into a database. Your page can then just get everything it needs from the database which will be much faster for the user. You can use AJAX to check the database periodically for new data. Meanwhile your daemon will be doing the hard-work in the background.

我建议你看看提高性能的其他方法。鉴于从gmail加载电子邮件的速度很慢,您可以使用守护程序(在服务器后台持续运行的服务)进行调查,以便将电子邮件同步到数据库中。然后,您的页面可以从数据库中获取所需的一切,这对用户来说会更快。您可以使用AJAX定期检查数据库以获取新数据。与此同时,你的守护进程将在后台进行艰苦的工作。