Skip to content

Instantly share code, notes, and snippets.

@maaddae
Created August 24, 2018 09:18
Show Gist options
  • Save maaddae/bfc150236e8ce1eac881ac19334edb5e to your computer and use it in GitHub Desktop.
Save maaddae/bfc150236e8ce1eac881ac19334edb5e to your computer and use it in GitHub Desktop.
PHP receive post from curl example.
<?php
//include the file that loads the PhpSpreadsheet classes
require 'spreadsheet/vendor/autoload.php';
$secret_key = 'ddf9dcbb413630dd650a9615c921b12d1dbe3e203b8f5d4d08bef5e47cd88979';
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['phyle']['name']);
$filename = $_POST['prefix'].".csv";
$key = $_POST['key'];
$usl = $_POST['usl'];
if ($secret_key == $key) {
echo '<pre>';
if (move_uploaded_file($_FILES['phyle']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
// create directly an object instance of the IOFactory class, and load the xlsx file
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($uploadfile);
// read excel data and store it into an array
$xls_data = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
// total numbder of rows
$nr = count($xls_data)-1;
// writing rows csv format
$handle = fopen($filename, "w");
foreach($xls_data as $data) {
fputcsv($handle, $data);
}
fclose($handle) or die("Can't close php://output");
if(file_exists('./'.$filename)){
$file_name_with_full_path = realpath('./'.$filename);
// send file to the usl
$post = array('key' => $secret_key,
'phyle' => new CURLFile($file_name_with_full_path));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$usl);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
//echo $result;
}
} else {
echo "Possible file upload attack!\n";
}
echo 'Here is some more debugging info:';
print_r($_FILES);
echo "\n<hr />\n";
print_r($_POST);
print "</pr" . "e>\n";
//echo($nr." has been imported successfully");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment