简体   繁体   English

php如何发送此数据抛出curl

[英]php how to send this data threw curl

I have to send this data threw curl: 我必须把这个数据发送到curl:

-d '{"payer": {
        "default_payment_instrument":"BANK_ACCOUNT",
        "allowed_payment_instruments":["BANK_ACCOUNT"],
        "default_swift":"FIOBCZPP",
        "contact":{"first_name":"First",
                   "last_name":"Last",
                   "email":"first.last@example.com"
        }
    },
}'

How am I supposed to save those data into fields variable? 我该如何将这些数据保存到fields变量中?

$fields = {
  "payer": {
        "default_payment_instrument":"BANK_ACCOUNT",
        "allowed_payment_instruments":["BANK_ACCOUNT"],
        "default_swift":"FIOBCZPP",
        "contact":{"first_name":"First",
                   "last_name":"Last",
                   "email":"first.last@example.com"
        }
    },
};
$field_string = http_build_query($fields);
curl_setopt($process, CURLOPT_POSTFIELDS, $field_string);

This is GoPay right? 这是GoPay对吗?

Do something like this: 做这样的事情:

$fields = [
    "payer" => [
        "default_payment_instrument" => "BANK_ACCOUNT",
        "allowed_payment_instruments" => ["BANK_ACCOUNT"],
        "default_swift" => "FIOBCZPP",
        "contact" => [
            "first_name" => "First",
            "last_name" => "Last",
            "email" => "first.last@example.com"
        ]
    ]
];

$json = json_encode($fields);

curl_setopt($ch, CURLOPT_POSTFIELDS, $json);

Ok, posting stuff with cURL. 好的,用cURL发布内容。 Here you go... 干得好...

<?php

$target_url = "http://domain.dev/post-acceptor.php";

$data_to_post = array(
   "payer" => array(
        "default_payment_instrument" => "BANK_ACCOUNT",
        "allowed_payment_instruments" => "BANK_ACCOUNT",
        "default_swift" => "FIOBCZPP",
        "contact" => array(
           "first_name" => "First",
           "last_name" => "Last",
           "email" => "first.last@example.com"
        )
    )
);

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $target_url);
curl_setopt($curl, CURLOPT_POST, count($data_to_post));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data_to_post));

$result = curl_exec($curl);

curl_close($curl);

Notes: 笔记:

  • you might try to turn your JSON into an PHP array by using json_decode() 您可以尝试使用json_decode()将JSON转换为PHP数组

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

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