dpir3950
2010-03-23 18:00
浏览 437

如何将网页结果嵌入到另一个网站的另一个网页上?

I would like to allow users to call my ruby on rails app as a service which returns a 'div' with html content in it, and embed that div into their app (which will not be a rails application).

For example, assume someone has their own php website that has a header/footer template that gets rendered, and a content area of the page that they need to fill based on some html I generate in my rails app. I would like to allow them, from php, to call to my website, get the 'div' I generate, and embed that as html in their php page.

What I'm trying to do is host a service on my site that returns some html content, but actually show that content as part of another site, so that the end user only sees the other site and never really knows about mine.

Also, I can use javascript on the client to do this if that is the only way, but I would prefer the php app to handle this at the server if possible so the client gets the html embedded from the original server and it looks like it all was generated by the php script that generated the entire page. I also want to avoid using an iframe.

Is this possible?

图片转代码服务由CSDN问答提供 功能建议

我想允许用户在rails应用程序上调用我的ruby作为服务返回带有html的'div' 其中的内容,并将该div嵌入他们的应用程序(这将不是一个rails应用程序)。

例如,假设有人拥有自己的php网站,其中包含要呈现的页眉/页脚模板,以及根据我生成的某些html需要填充的页面内容区域 在我的rails应用程序中。 我想允许他们,从php,调用我的网站,获取我生成的'div',并将其作为html嵌入他们的php页面。

我正在尝试 要做的是在我的网站上托管一个返回一些html内容的服务,但实际上将该内容显示为另一个网站的一部分,以便最终用户只能看到另一个网站而从未真正知道我的内容。 \ n

此外,如果这是唯一的方法,我可以在客户端上使用javascript来执行此操作,但我希望php应用程序在服务器上处理这个,如果可能的话,客户端从原始服务器获取嵌入的html 看起来这一切都是由生成整个页面的php脚本生成的。 我也想避免使用iframe。

这可能吗?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doz22551 2010-03-23 18:06
    已采纳

    Assuming you need to make a cross domain request of the data, and you can't just use an iframe, you could try using JSONP to fetch the html wrapped up in JS.

    已采纳该答案
    打赏 评论
  • douju2012 2010-03-23 18:05

    Yes it's possible. The easiest way is made a partial to this embedded part.

    Add a new action or a parameter on your action. When you detect it's an embedded render just your partial

    render :partial => 'my_embedded'
    
    打赏 评论
  • dongwa3808 2010-03-23 19:41

    What you're essentially looking to do is implement a very limited RESTful API. If you are the only one who should have access, you should also implement some kind of basic security such as passing a 'secret' parameter to the Rails app to verify the requester. Simply provide a route in your Rails Application something like this (in Rails 2.3.x):

    map.connect '/php/embed', :controller => 'some_controller', :action => 'embed'
    

    Then in your controller:

    class SomeController < ApplicationController
      before_filter :verify_authenticity
      def embed
        render :partial => "some_rails_partial", :layout => false
      end
    
      protected
    
      def verify_authenticity
        render(:text => 'Unauthorized', :status => 401) unless params[:secret] == 'somesecretkey'
      end
    end
    

    Then, from your PHP app, you simply need to make a call to download the contents of the URL at http://your.railsapp.com/php/embed?secret=somesecretkey.

    打赏 评论

相关推荐 更多相似问题