php如何发送此数据抛出curl

时间:2015-06-19 19:57:44

标签: php arrays object 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"
        }
    },
}'

我该如何将这些数据保存到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);

2 个答案:

答案 0 :(得分:2)

这是GoPay吗?

做这样的事情:

$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);

答案 1 :(得分:1)

好的,用cURL发布内容。你去......

<?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);

注意:

  • 您可以尝试使用json_decode()
  • 将JSON转换为PHP数组