如何在 php 和 javascript 中创建一个小部件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8189079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:35:30  来源:igfitidea点击:

How to create a widget in php & javascript?

phpjavascriptmysqlwidget

提问by cjbs

I have a website for a client offering information from a database. But other websites want to show that information in their website, so my client ask me for it.

我有一个客户网站,提供来自数据库的信息。但是其他网站想在他们的网站上显示这些信息,所以我的客户向我索要。

Since the begining I thought it might be something similar to the twitter widget. As I want to give out a code similar to this:

从一开始我就认为它可能类似于 twitter 小部件。因为我想给出一个类似于这样的代码:

<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script>
new TWTR.Widget({ ......

And other websites will show the information from my database.

其他网站会显示我数据库中的信息。

But I cannot find a exactly example, I found this: http://tutorialzine.com/2010/03/who-is-online-widget-php-mysql-jquery/

但我找不到一个确切的例子,我发现了这个:http: //tutorialzine.com/2010/03/who-is-online-widget-php-mysql-jquery/

But it is not exactly what I want.

但这并不完全是我想要的。

My deployment is the following: In the server I've got a mySQL database and a website, I would like to create php and javascript code (or even jquery, but I'm not very expert with it) so other website could incorporate the information from the database in a secure mode.

我的部署如下:在服务器中,我有一个 mySQL 数据库和一个网站,我想创建 php 和 javascript 代码(甚至是 jquery,但我不是很擅长),以便其他网站可以合并以安全模式从数据库中获取信息。

Could anybody give a hint?

有人可以给点提示吗?

采纳答案by LoveAndCoding

You'll need a RESTful serviceon your server which other sites can ping. You will use an AJAX request to get the information from that service.

您的服务器上需要一个RESTful 服务,其他站点可以 ping 通。您将使用 AJAX 请求从该服务获取信息。

The big piece of this for you will be creating a JavaScript object that has all the functionality you want. It is much easier to give people directions on how to use your REST API and let them implement it via AJAX on their own pages. If you really do want a full widget, you'll want to check out a lot of things. First is closures in JSto ensure you don't conflict with any of their variables. Also make sure you are good at developing cross-browser Javascript independent of libraries. And finally, you'll want to make sure your server is configured for cross-domain AJAX requests. Again, my recommendation is to set up a REST API for them, and let them do the dirty work.

对您来说,最重要的部分是创建一个具有您想要的所有功能的 JavaScript 对象。向人们提供有关如何使用 REST API 的指导并让他们通过 AJAX 在他们自己的页面上实现它要容易得多。如果你真的想要一个完整的小部件,你会想要查看很多东西。首先是JS中的闭包,以确保您不会与它们的任何变量发生冲突。还要确保您擅长开发独立于库的跨浏览器 Javascript。最后,您需要确保您的服务器配置为跨域 AJAX 请求。同样,我的建议是为他们设置一个 REST API,让他们做一些肮脏的工作。

回答by alessioalex

There is an interesting tutorial about how to create a twitter widget using PHP and JavaScript on the nettutswebsite, I think you may find it useful.

nettuts网站上有一个关于如何使用 PHP 和 JavaScript 创建 Twitter 小部件的有趣教程,我认为您可能会发现它很有用。

回答by Lulu

Well, although it's not specific for PHP, this is by far the best resource I could find to this subject: http://alexmarandon.com/articles/web_widget_jquery/

好吧,虽然它不是特定于 PHP 的,但这是迄今为止我能找到的关于该主题的最佳资源:http: //alexmarandon.com/articles/web_widget_jquery/