<?php
/*Example Data to process the delimiter*/
$input = [
'title' => 'Marathon Run',
'category' => 2,
'partner_code' => 'AYB12345',
'signature_key' => '01b2eb96f63a74f46ccc2431952b39b6704dce330ad611d9ede62e70c27a954e696a503941ae4cbf0262686825cd004a1afa624d3fe6a1513910dc2b5cd4b81e',
//etc
];
$file['campaign_image'] = [
'filename' => "image.jpg",
'type' => "jpg",
'content' => utf8_decode($filepath)
];
$file['campaign_banner'] = [
'filename' => "banner.jpg",
'type' => "jpg",
'content' => utf8_decode($filepath)
];
$delimiter = '----WebKitFormBoundary'.uniqid();
$data = $this->create_post($delimiter, $input, $file);
/*end of example data*/
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => {base_url}'/api/v1/partner/campaign'tml",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer 88A4B7FEADD1667B',
'Content-Type: multipart/form-data; boundary=' . $delimiter,
'Content-Length: ' . strlen($data)
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
/*Function*/
function create_post($delimiter, $postFields, $fileFields = array()){
// form field separator
$eol = "\r\n";
$data = '';
// populate normal fields first (simpler)
foreach ($postFields as $name => $content) {
$data .= "--$delimiter" . $eol;
$data .= 'Content-Disposition: form-data; name="' . $name . '"';
$data .= $eol.$eol; // note: double endline
$data .= $content;
$data .= $eol;
}
// populate file fields
foreach ($fileFields as $name => $file) {
$data .= "--$delimiter" . $eol;
// fallback on var name for filename
if (!array_key_exists('filename', $file))
{
$file['filename'] = $name;
}
// "filename" attribute is not essential; server-side scripts may use it
$data .= 'Content-Disposition: form-data; name="' . $name . '";' .
' filename="' . $file['filename'] . '"' . $eol;
// this is, again, informative only; good practice to include though
$data .= 'Content-Type: ' . $file['type'] . $eol;
// this endline must be here to indicate end of headers
$data .= $eol;
// the file itself (note: there's no encoding of any kind)
if (is_resource($file['content'])){
// rewind pointer
rewind($file['content']);
// read all data from pointer
while(!feof($file['content'])) {
$data .= fgets($file['content']);
}
$data .= $eol;
}else {
// check if we are loading a file from full path
if (strpos($file['content'], '@') === 0){
$file_path = substr($file['content'], 1);
$fh = fopen(realpath($file_path), 'rb');
if ($fh) {
while (!feof($fh)) {
$data .= fgets($fh);
}
$data .= $eol;
fclose($fh);
}
}else {
// use data as provided
$data .= $file['content'] . $eol;
}
}
}
// last delimiter
$data .= "--" . $delimiter . "--$eol";
return $data;
}