简体   繁体   English

如何从另一个域(跨域)获取由php脚本生成的html

[英]how can i get html generated by a php script from another domain(cross-domain)

i am wondering how can get the HTML code which is generated by a cross-domain php script? 我想知道如何获得由跨域PHP脚本生成的HTML代码?

Normally if i'm on the same domain , i would use Ajax as follows: 通常,如果我在同一个域上,我会使用Ajax,如下所示:

$.ajax({
    type: 'GET',
    url: 'user.php',
    data: 'user_id=user_id', //assuming user_id value was already set.
    success: function(html)
    {
        $('#info').empty().html(html);
    }  
});

But i am now working on a different domain than my server domain. 但我现在正在开发一个与我的服务器域不同的域。 Which means i use JSON to send data back and to my server php scripts. 这意味着我使用JSON将数据发送回我的服务器php脚本。 However , i know that JSON only send data but not a complete HTML CODE(or am i missing out some point here?) 但是,我知道JSON只发送数据而不是完整的HTML代码(或者我错过了一些点吗?)

So , how can i get the html code generated by a cross-domain php script(server) to my web page(another domain). 那么,我怎样才能将跨域php脚本(服务器)生成的html代码添加到我的网页(另一个域)。

using javascript you can do the same as if it was JSON, it's called JSONP the P is with padding . 使用javascript你可以像JSON那样做,它叫做JSONP,P是with padding

Or you can call it JSON with callback: 或者你可以用回调来称它为JSON:

// Request Page //请求页面

myCallback("Some string or Object to parse to your site");

// Your Page //你的页面

window["myCallback"] = function(string_or_object) {
    // Here you can do everything with the parsed data
}

Create a script-tag and include the request page. 创建脚本标记并包含请求页面。 Make sure to define your callback before including the script-tag 确保在包含script-tag之前定义回调

or you can use jQuery's ajax method with dataType set to jsonp : 或者你可以使用jQuery的ajax方法,将dataType设置为jsonp

$.ajax({
    "url": "requst_page.php",
    "dataType": "jsonp",
    "success": function(string_or_object) {
        // Here you can do everything with the parsed data
    }
})

Look at http://remysharp.com/2007/10/08/what-is-jsonp/ 请访问http://remysharp.com/2007/10/08/what-is-jsonp/

EDIT TO COMMENT: 编辑评论:

JSON is right an object normally starts with braces {} . JSON是正确的对象通常以大括号{}开头。

Demo JSON: 演示JSON:

{
    "myString": "myValue",
    "myOtherString": ["My", "Other", "Value", "This", "Is", "An", "Array"]
}

But using the same method as JSONP you can parse an string instead of the weird looking thing starting and ending with {} . 但是使用与JSONP相同的方法,您可以解析字符串而不是以{}开头和结尾的怪异外观。

as myCallback in my example 1: myCallback("HERE I PASS A STRING INSTEAD OF AN OBJECT") . myCallback在我的例子1: myCallback("HERE I PASS A STRING INSTEAD OF AN OBJECT") See the "" . "" "STRING GOES IN HERE"

if it was JSON and taken usage of my DEMO JSON it would look like this: 如果它是JSON并使用我的DEMO JSON,它将如下所示:

myCallback({
    "myString": "myValue",
    "myOtherString": ["My", "Other", "Value", "This", "Is", "An", "Array"]
})

JS: JS:

$.ajax({
    type: 'GET',
    url: 'curl.php',
    data: 'user_id=user_id', //assuming user_id value was already set.
    success: function(html)
    {
        $('#info').empty().html(html);
    }  
});

curl.php; curl.php;

function get_data($url){
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$user_id = $_GET['user_id'];
$url= "http://www.example.com/user.php?user_id=".$user_id;
echo get_data($url);

This can be done through jsonp. 这可以通过jsonp完成。 Following is an example. 以下是一个例子。

$.ajax({
   url: "example.com/respond.php",
   data: {id: id},
   dataType: "jsonp",
   jsonp : "callback",
   jsonpCallback: "jsonpcallback"
}); 

respond.php respond.php

<?php
header("content-type: application/json"); 

if (isset($_GET['id'])) $rtnjsonobj->id = $_GET['id']; 
$rtnjsonobj->message = "This is the message from cross domain";


echo $_GET['callback']. '('. json_encode($rtnjsonobj) . ')';    
?>

You can set up a proxy on your domain and use CURL to get the json from other domain. 您可以在域上设置代理,并使用CURL从其他域获取json。 You then send request to this proxy. 然后,您将请求发送到此代理。

Alternately you would need to set up the other domain to process request as jsonp in order to be able to access directly with ajax 或者你需要设置另一个域来处理请求为jsonp,以便能够直接使用ajax访问

您必须使用JSONP或告诉其他站点的网站所有者添加Access-Control-Allow-Origin标头。

I also get same problem when access cross domain using ajax. 使用ajax访问跨域时,我也遇到同样的问题。 Then I solve it by make the ajax call to same domain. 然后我通过对同一域进行ajax调用来解决它。 Then on that php script I apply following code block. 然后在那个PHP脚本上我应用以下代码块。 This will get the content from cros domain and out put the same to ajax call. 这将从cros域获取内容,并将其与ajax调用相同。

$content = file_get_contents('http://crossdomain.com');
    echo $content;

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

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