Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// data fields for POST request
$fields = array(
"token" => "2c4b4545a9b80dbac64f490f48cceca4",
"firstname" => "firstname",
"lastname" => "lastname",
"cardnumber" => "4321432143214321",
"cardmonth" => "03",
"cardyear" => "2020",
"cvv" => "123",
"currency" => "JPY",
"customer_id[0]" => 1,
"contents_id[0]" => 1,
"contents_name[0]" => 1,
"amount[0]" => "1980",
"loginname" => "sang4",
"charge_type" => "6",
"site_id" => "258"
);
// URL to upload to
$url = "http://api.some-company.com:8081/payment2.php";
// curl
$curl = curl_init();
$url_data = http_build_query($fields);
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
$post_data = build_data_files($boundary, $fields);
var_dump($post_data);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => array(
"Content-Type: multipart/form-data; boundary=" . $delimiter,
"Content-Length: " . strlen($post_data)
),
));
//
$response = curl_exec($curl);
$info = curl_getinfo($curl);
$err = curl_error($curl);
$result = json_decode($response, true);
curl_close($curl);
function build_data_files($boundary, $fields){
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($fields as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
. $content . $eol;
}
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.