简体   繁体   English

Jquery,PHP将Json数据发布到外部URL

[英]Jquery ,PHP Post Json data to an external url

I have the following jquery function which is supposed to post data to a php function which in the process it passes it to the external script for processing. 我有以下jquery函数,应该将数据发布到php函数,在此过程中它将数据传递给外部脚本进行处理。 Below is my jquery function : 下面是我的jQuery函数:

  function order_confirmation(json) {
                    $.ajax({
                        type: 'POST',
                        url: "<?php echo base_url(); ?>home/confirm_order_received/",
                        data: json,
                        contentType: 'application/json',
                        dataType: 'json',
                        success: function (data) {
                            console.log(data);
                        },
                        failure: function (errMsg) {
                            console.log(errMsg);
                        }
                    });
                }

This function is supposed to pass the json data to my php function which is below here : 这个函数应该将json数据传递给我的php函数,如下所示:

  function confirm_order_received() {
        $json = $this->input->post('json');
        // Initialize curl
        $curl = curl_init();
        $externalscriptaddress = "http://197.237.132.178/api/order/order_upsert";
        // Configure curl options
        $opts = array(
            CURLOPT_URL => $externalscriptaddress,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POST => 1,
            CURLOPT_POSTFIELDS => $json
        );

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

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

        // Close resource
        curl_close($curl);

        echo $result;
    }

I have a problem of how I am supposed to access the data from the php side, I have tried accessing it through the post : $json = $this->input->post('json'); 我有一个我应该如何从php端访问数据的问题,我尝试通过帖子访问它: $json = $this->input->post('json'); and passed it to the CURLOPT_POSTFIELDS => $json but I get an error since nothing has been passed. 并将其传递给CURLOPT_POSTFIELDS => $json但由于未传递任何内容,因此出现错误。 Please advise how can i pass the json data to an external script? 请告知我如何将json数据传递到外部脚本?

要获取原始的帖子数据(在您的情况下),请使用file_get_contents("php://input")

Instead of data: json, you should use data: "json:json", 您应该使用data: "json:json",代替data: json, data: "json:json",

Using just data: json, will post the data, but it will not be assigned to a key. 仅使用data: json,将发布数据,但不会将其分配给键。 To access the data ou need to assign it to a key. 要访问数据,您需要将其分配给密钥。 By using data: "json:json", , you can access it with $json = $this->input->post('json'); 通过使用data: "json:json",您可以使用$json = $this->input->post('json');访问它$json = $this->input->post('json');

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

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