Jump to content

  • You cannot start a new topic
  • You cannot reply to this topic

How To Save As Csv File In Server Rate Topic   - - - - -

 
  • muthu.amr
  • Member
  • Members
  • Join Date: 05-Jul 20
  • 20 posts

Posted 16 November 2020 - 03:31 AM #1

Hi All,

 

I would like to retrieve some zipcode data from the API which responses as CSV file format. I can retrieve data from the API successfully and able to save the file in the server by using the following code, but it doesn't save as a proper CSV file in the server. Thus, when read the CSV file programmatically, it throws an error. 

 

When I download the generated file from the server and open the file in excel, there is a warning message as attached in the screenshot, if press "Yes", I can read the data manually without any issue and refer the attached "pincode_data.csv" file that has been generated by the below code.

 

How to fix to save a file as CSV in the server? kindly help and advise.

 

Please refer my coding as below;

 

       $auth_token='36af5c05fa1317700ad50ccb3921a8c65544';

       $file_path = '/home/oisiacom/public_html/pincode_data.csv';   
        $params='auth_token'.'='.$auth_token.'&'.'source_pincode'.'='.'623308';
        $url = 'http://pickrr.com/ap...'.'?'.$params;        
       
        //echo $url;
        $start = curl_init();
        curl_setopt($start, CURLOPT_URL, $url);
        curl_setopt($start, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($start, CURLOPT_SSLVERSION, 3);       
        $file_data = curl_exec($start);
        curl_close($start);
 
       $file = fopen($file_path, 'w+');
       fputs($file, $file_data);
       fclose($file);

Attached Thumbnails

  • CSV_error.JPG

Attached Files



 
  • muthu.amr
  • Member
  • Members
  • Join Date: 05-Jul 20
  • 20 posts

Posted 16 November 2020 - 07:57 AM #2

I got a solution that after converting the xls file to csv file by using the following code, I am able to access the CSV file programmatically. Thanks all!
 
require_once('lib/phpexcel/Classes/PHPExcel/IOFactory.php');         
$inputFileType = 'Excel5';
            
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcelReader = $objReader->load($xls_file_path);
            
            $loadedSheetNames = $objPHPExcelReader->getSheetNames();
            
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
            
            foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
                $objWriter->setSheetIndex($sheetIndex);
                $objWriter->save($value['zipcode'].'.csv');
            }