XML to CSV converter using API - api

I want to convert my XML file into CSV file but i want to use any tool who convert it automatically and i can pass input parameter using its API.I don't want to use any script to directly convert XML to csv. Is there any way to perform this?

Use PHP_curl....
Here is the code..
<?php
$filexml="formdata.xml";
if (file_exists($filexml)) {
$xml = simplexml_load_file($filexml);
$f = fopen('invoice.csv', 'w');
createCsv($xml, $f);
fclose($f);
}
function createCsv($xml,$f)
{
$arr1 = array('col1','col2'...);
fputcsv($f, $arr1 ,',','"');
foreach ($xml->record as $item)
{
$hasChild = (count($item->record) > 0)?true:false;
if( ! $hasChild)
{
//item is xml tag name
$put_arr = array($item->company_name);
fputcsv($f, $put_arr ,',','"');
}
else
{
createCsv($item, $f);
}
}
}
?>

Related

laravel 7 pdf-to-text | Could not read pdf file

I installed this laravel package to convert pdf file into text. https://github.com/spatie/pdf-to-text
I'm getting this error:
Could not read sample_1610656868.pdf
I tried passing the file statically by putting it in public folder and by giving path of uploaded file.
Here's my controller:
public function pdftotext(Request $request)
{
if($request->hasFile('file'))
{
// Get the file with extension
$filenameWithExt = $request->file('file')->getClientOriginalName();
//Get the file name
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
//Get the ext
$extension = $request->file('file')->getClientOriginalExtension();
//File name to store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload File
$path = $request->file('file')->storeAS('public/ebutifier/pdf', $fileNameToStore);
}
// dd($path);
$location = public_path($path);
// $pdf = $request->input('file');
// dd($location);
// echo Pdf::getText($location, 'usr/bin/pdftotext');
$text = (new Pdf('public/ebutifier/pdf/'))
->setPdf($fileNameToStore)
->text();
return $text;
}
Not sure why it's not working any help would be appreciated.
You used spatie/pdf-to-text Package.
I also tried to use this package but it gave me this kind of error.
So, I used asika/pdf2text package and it worked properly
Asika/pdf2text pacakge
And this is my code
$path = public_path('/uploads/documents/'. $file_name);
$reader = new \Asika\Pdf2text;
$output = $reader->decode($path);
dd($output);
Hopefully, it will be helpful for you.

PHP telegram bot, extract url in UTF-16 code units

I have a problem with telegram bot api. I'm trying to extract a URL from a message. It is written in the MessageEntity type that the offset and length are specified in UTF-16 code units. I've tried many ways to get a substring from the text (with mb_convert_encoding, iconv, json_encode etc.), but I did not get the correct link. It works for plain text without emoji but not with them.
$output = json_decode(file_get_contents('php://input'), TRUE);
$message = $output['message']['text'];
$entities = $output['message']['entities'];
function getURLs($message, $entities) {
$URLs = [];
//$message_encode = iconv('utf-8', 'utf-16le', $message); //or utf-16
$message_encode = mb_convert_encoding($message, "UTF-16", "UTF-8"); //or utf-16le
foreach ($entities as $entitie) {
if ($entitie['url']) {
$URLs[] = $entitie['url'];
}
if ($entitie['type']=='url') {
$URL16 = substr($message_encode, $entitie['offset']*2, $entitie['length']*2);
//$URLs[] = iconv('utf-16le', 'utf-8', $URL16);
$URLs[] = mb_convert_encoding($URL16, "UTF-8", "UTF-16");
}
}
return $URLs;
}
$URLs = getURLs($message, $entities);
You can use iconv or mb_convert_encoding, UTF-16le or UTF-16.
See also PHP - length of string containing emojis/special chars

Magento2 read uploaded csv file

I have upload CSV file but problem is i don't know how to read data of uploaded csv file in Magento2.
Please help me anyone.
Thanks in advance.
You can do that like you could in Magento 1. In Magento 1 you would use
new Varien_File_Csvbut in Magento 2 you can do the same with \Magento\Framework\File\Csv. You can use the following code.
In your __construct() inject the following classes:
protected $_fileCsv;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Module\Dir\Reader $moduleReader,
\Magento\Framework\File\Csv $fileCsv
) {
$this->_moduleReader = $moduleReader;
$this->_fileCsv = $fileCsv;
parent::__construct($context); // If your class doesn't have a parent, you don't need to do this, of course.
}
Then you can use it like this:
// This is the directory where you put your CSV file.
$directory = $this->_moduleReader->getModuleDir('etc', 'Vendor_Modulename');
// This is your CSV file.
$file = $directory . '/your_csv_file_name.csv';
if (file_exists($file)) {
$data = $this->_fileCsv->getData($file);
// This skips the first line of your csv file, since it will probably be a heading. Set $i = 0 to not skip the first line.
for($i=1; $i<count($data); $i++) {
var_dump($data[$i]); // $data[$i] is an array with your csv columns as values.
}
}

Yii2 - rename file on upload if the file with the same name exist

How can I rename file if the file with the same name exist (prevent overwrite)? This method is used for uploading file:
// Upload user avatar
public function upload()
{
if ($this->validate()):
$this->avatar->saveAs('../web/images/avatars/' . $this->avatar->baseName . '.' . $this->avatar->extension);
return true;
endif;
return false;
}
Test if file exists with php function and then assign a proper name to your file:
$cnt = 1;
while (file_exists(string $filename)) {
$filename = $this->avatar->basename . $cnt;
$cnt++;
}
Then save.

How do I read multiple files after click opened in OpenFileDialog?

I have set a multiselect function in my code to allow me to open multiple files which is in ".txt" forms. And here is the problem, how am I going to read all these selected files after it opened through OpenFileDialog? The following codes and at the "for each" line, when I use System::Diagnostics::Debug, it shows only the data from a file, while data of other files were missing. How should I modify the code after the "for each"? Can anyone provide some suggestions or advice? The files selected are as 1_1.txt, 2_1.txt, 3_1.txt. Appreciate your reply and Thanks in advance.
Here is my written code,
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Title = "open captured file";
openFileDialog1->Filter = "CP files (*.cp)|*.cp|All files (*.*)|*.*|txt files (*.txt)|*.txt";
openFileDialog1->FilterIndex = 2;
openFileDialog1->Multiselect = true;
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
array<String^>^ lines = System::IO::File::ReadAllLines(openFileDialog1->FileName);
for each (String^ line in lines) {
//?????
System::Diagnostics::Debug::WriteLine("",line);
}
}
You need to look at the OpenFileDialog.FileNames property if you allow multiple files to be selected:
if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
{
for each (String^ file in openFileDialog1->FileNames)
{
array<String^>^ lines = System::IO::File::ReadAllLines(file);
for each (String^ line in lines)
{
System::Diagnostics::Debug::WriteLine("",line);
}
}
}
Use the FileNames property.
C# version (should be easy to adapt for C++):
foreach (var file in openFileDialog1.FileNames)
{
foreach (var line in File.ReadAllLines(file)
{
...
}
}
Use openFileDialog1->FileNames. It returns the multiple filenames that you selected
Read here
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.multiselect.aspx
its in C# but, it will be easy to extrapolate to C++.