Skip to content

Instantly share code, notes, and snippets.

@landzz
Created June 8, 2018 06:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save landzz/99e3e07955fe889046113a0e90e1acb1 to your computer and use it in GitHub Desktop.
Save landzz/99e3e07955fe889046113a0e90e1acb1 to your computer and use it in GitHub Desktop.
php curl post with file (multipart)
<?php
if ( ! function_exists('getCurlPostwithFile')){
function getCurlPostwithFile($_url='', $_param=array(), $_file_name=array()){
if($_url !=''){
ini_set("memory_limit", "512M");
$_file_data = array();
foreach ($_file_name as $_key => $_val){
$_file_data[] = array(
'name' => $_key
,'filename' => basename($_val)
//,'binary_data' => '바이너리'
,'binary_data' => file_get_contents($_val)
);
}
$_data = $_param;
$boundary = uniqid();
$delimiter = '-------------' . $boundary;
//make multipart data
$_post_data = build_data_files($boundary, $_data, $_file_data);
$_headers = array(
"Content-Type: multipart/form-data; boundary=" . $delimiter
,"Content-Length: " . strlen($_post_data)
);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $_url );
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlObj, CURLOPT_SSLVERSION, 1);
curl_setopt($curlObj, CURLOPT_POST, true);
curl_setopt($curlObj, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curlObj, CURLOPT_HEADER, false);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, $_headers);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $_post_data);
$response = curl_exec($curlObj);
$_json = array();
$_json = json_decode($response,true);
curl_close($curlObj);
return array(
'response' => $response
,'json' => $_json
,'data' => $_post_data
);
}else{
return false;
}
}
}
if ( ! function_exists('build_data_files')){
// build multipart/formdata , multipart 폼데이터 만들기
function build_data_files($boundary, $fields, $files){
$data = '';
$eol = "\r\n";
$delimiter = '-------------' . $boundary;
foreach ($fields as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . "\"".$eol.$eol
. $content . $eol;
}
foreach ($files as $_key => $_val) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $_val['name'] . '"; filename="' . $_val['filename'] . '"' . $eol
//. 'Content-Type: image/png'.$eol
. 'Content-Transfer-Encoding: binary'.$eol;
$data .= $eol;
$data .= $_val['binary_data'] . $eol;
}
/*
foreach ($files as $name => $content) {
$data .= "--" . $delimiter . $eol
. 'Content-Disposition: form-data; name="' . $name . '"; filename="' . $name . '"' . $eol
//. 'Content-Type: image/png'.$eol
. 'Content-Transfer-Encoding: binary'.$eol;
$data .= $eol;
$data .= $content . $eol;
}
*/
$data .= "--" . $delimiter . "--".$eol;
return $data;
}
}
//sample
$_url = 'url';
$_send_param = array(
'param1' => 'param1'
,'param2' => 'param2'
);
$_file_name = array( 'file1' => 'file path(absolute path)');
$_tmp = getCurlPostwithFile($_url, $_send_param, $_file_name);
$_response = $_tmp['response'];
//$_response = null;
$_json = $_tmp['json'];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment