简体   繁体   中英

php how to send this data threw curl

I have to send this data threw 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 = {
  "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?

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. 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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