Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将JSON数据发布到外部URL_Php_Javascript_Jquery_Json_Post - Fatal编程技术网

Php 将JSON数据发布到外部URL

Php 将JSON数据发布到外部URL,php,javascript,jquery,json,post,Php,Javascript,Jquery,Json,Post,如何将JSON数据作为url字符串跨域发布到外部url并绕过访问控制 以下是一个jquery.ajax post请求,由于访问控制允许来源,无法发送到外部url: var json = JSON.stringify(object); $.ajax({ type: 'POST', url: externalurl, data: json, dataType: 'json', success: function(data){console.log(data);}, fail

如何将JSON数据作为url字符串跨域发布到外部url并绕过访问控制

以下是一个jquery.ajax post请求,由于访问控制允许来源,无法发送到外部url:

var json = JSON.stringify(object);

$.ajax({
  type: 'POST',
  url: externalurl,
  data: json,
  dataType: 'json',
  success: function(data){console.log(data);},
  failure: function(errMsg) {
      console.log(errMsg);
  },
});

我收到一个建议,将数据发布到同一个域,并将请求“传递”到外部域,尽管这个解决方案对我来说没有意义。我正在寻找最安全的解决方案。任何帮助都将不胜感激。

不久前我用PHP做了这件事。下面是一个传递请求的示例。您需要启用PHP cURL,这对于大多数安装来说都是非常标准的

<?php
    //Get the JSON data POSTed to the page
    $request = file_get_contents('php://input');

    //Send the JSON data to the right server
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://location_of_server.com/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    $data = curl_exec($ch);
    curl_close($ch);

    //Send the response back to the Javascript code
    echo $data;
?>

不久前我用PHP做了这个。下面是一个传递请求的示例。您需要启用PHP cURL,这对于大多数安装来说都是非常标准的

<?php
    //Get the JSON data POSTed to the page
    $request = file_get_contents('php://input');

    //Send the JSON data to the right server
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://location_of_server.com/");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    $data = curl_exec($ch);
    curl_close($ch);

    //Send the response back to the Javascript code
    echo $data;
?>

绕过同源策略的一种方法是使用cURL进行实际传输

我将给出一个使用PHP的示例,但是您可以在任何服务器端语言上轻松地实现这一点

在服务器上设置脚本,例如send.php

首先,将ajax指向send.php

var json = JSON.stringify(object);

$.ajax({
    type: 'POST',
    url: send.php,
    data: json,
    dataType: 'json',
    success: function(data){console.log(data);},
    failure: function(errMsg) {
        console.log(errMsg);
    },
});
然后使用php脚本转发它:

<?php
    // Initialize curl
    $curl = curl_init();

    // Configure curl options
    $opts = array(
        CURLOPT_URL             => $externalscriptaddress,
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_CUSTOMREQUEST   => 'POST',
        CURLOPT_POST            => 1,
        CURLOPT_POSTFIELDS      => 'field1=arg1&field2=arg2'
    );

    // Set curl options
    curl_setopt_array($curl, $opts);

    // Get the results
    $result = curl_exec($curl);

    // Close resource
    curl_close($curl);

    echo $result;
?>

绕过同源策略的一种方法是使用cURL进行实际传输

我将给出一个使用PHP的示例,但是您可以在任何服务器端语言上轻松地实现这一点

在服务器上设置脚本,例如send.php

首先,将ajax指向send.php

var json = JSON.stringify(object);

$.ajax({
    type: 'POST',
    url: send.php,
    data: json,
    dataType: 'json',
    success: function(data){console.log(data);},
    failure: function(errMsg) {
        console.log(errMsg);
    },
});
然后使用php脚本转发它:

<?php
    // Initialize curl
    $curl = curl_init();

    // Configure curl options
    $opts = array(
        CURLOPT_URL             => $externalscriptaddress,
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_CUSTOMREQUEST   => 'POST',
        CURLOPT_POST            => 1,
        CURLOPT_POSTFIELDS      => 'field1=arg1&field2=arg2'
    );

    // Set curl options
    curl_setopt_array($curl, $opts);

    // Get the results
    $result = curl_exec($curl);

    // Close resource
    curl_close($curl);

    echo $result;
?>


您是否尝试过使用JSON-P?我相信在jQuery中,您使用“jsonp”而不是“json”来表示数据类型,但我不是100%确定……当您说“传递请求”解决方案对您来说没有意义时,您的意思是您不理解它并希望得到解释吗,或者你确实理解它,但认为它不适合你目前的情况@MarkOrmston-JSONP确实允许您解决域问题,但只有当外部域设置为处理它并提供适当的响应时,它才会起作用。是的,在这种情况下它不合适,数据必须作为json发送。我对外部服务器也没有任何控制权,因此为什么CORS也不是一个可行的解决方案。@jverban如果您没有对其他域服务的控制权,那么可能无法从该服务器获得响应。我不明白为什么你认为作为JSON发送的数据会阻止你做“传递请求”的事情。jQuery将对您自己的PHP执行一个Ajax请求,您的PHP将调用另一个服务器,然后获取其响应并将其返回到浏览器。无论是否使用JSON,这都是可以做到的……您是否尝试过使用JSON-P?我相信在jQuery中,您使用“jsonp”而不是“json”来表示数据类型,但我不是100%确定……当您说“传递请求”解决方案对您来说没有意义时,您的意思是您不理解它并希望得到解释吗,或者你确实理解它,但认为它不适合你目前的情况@MarkOrmston-JSONP确实允许您解决域问题,但只有当外部域设置为处理它并提供适当的响应时,它才会起作用。是的,在这种情况下它不合适,数据必须作为json发送。我对外部服务器也没有任何控制权,因此为什么CORS也不是一个可行的解决方案。@jverban如果您没有对其他域服务的控制权,那么可能无法从该服务器获得响应。我不明白为什么你认为作为JSON发送的数据会阻止你做“传递请求”的事情。jQuery将对您自己的PHP执行一个Ajax请求,您的PHP将调用另一个服务器,然后获取其响应并将其返回到浏览器。无论是否使用JSON,这都是可以做到的…非常感谢您的解决方案。非常感谢您的解决方案。很高兴提供帮助,如果您对该解决方案感到满意,请将其标记为答案,供其他人将来查看。要接受答案,您可以单击向上投票旁边的勾号,它会将答案标记为正确答案,如果你的第一个问题被标记为答案,它也会给你一些声誉:啊,我不知道该怎么做-对stackoverflow来说还是新鲜事。感谢againGlad的帮助,如果您对解决方案感到满意,请将其标记为答案,以供其他人将来查看。要接受答案,您可以单击upvote旁边的勾号,它将标记答案为正确答案,如果您的第一个问题标记为答案,也会给您一些声誉:啊,不知道该怎么做-对stackoverflow来说还是个新手。再次感谢