簡體   English   中英

php如何發送此數據拋出curl

[英]php how 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"
        }
    },
}'

我該如何將這些數據保存到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);

這是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);

好的,用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數組

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM