Parsing a received csv file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Parsing a received csv file

Post by fastfingertips »

:?:
I'm using CURL to get a balance history, and i'm trying to print the results. Also because is not my web server i cannot set the default page si i must avoid that "Virtual Directory message".
Unfortnatelly nothing haoppens

Code: Select all

<?php
function myHistory()
{   
  $link="https://www.e-gold.com/acct/historycsv.asp";
  $pass="************";
  $account="**********"; 
  $batch="********* ";
  $theString ="historycsv.asp?batchfilter=$batch";
  $theString .="&PassPhrase=$pass";
  $theString .="&AccountID=$account";  
  $ch = curl_init("https://www.e-gold.com/acct/");
  $fp = fopen($theString, "r");
  
  curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_setopt($ch, CURLOPT_HEADER, 0);  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  curl_exec($ch); 
  curl_close($ch);
  while ($data = fgetcsv($fp, 1000, ",")) 
  {
    $num = count($data);
    echo "<p> $num fields in line $row: <br />\n";
    $row++;
    for ($c=0; $c < $num; $c++) 
    {
        echo $data[$c] . "<br />\n";
    }
  }
  
  fclose($fp);
}
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

try the following:

Code: Select all

//...skipped......
//$fp = fopen($theString, "r"); 
$fp = fopen($theString, "w+"); 
//...skipped.......
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
//...skipped.........
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

w+ means to write the file and i cannot change it :(. I must take only what is inside, so how can i do that?

Anyway i have tried as you said and still not working.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

1. You need to pass to curl "writable" file pointer.
2. w+ means "read and write, truncate if exists, create if not exist"
3. To read from that pointer you need to [php_man]rewind[/php_man]($fp) after the curl_exec call.
4. Don't set CURLOPT_RETURNTRANSFER unless you want to get the response into some variable. To store response in the file you don't need this option.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

I've tried also to rewind but same results, a blank page:
you may take a look and perhaps give me an ideea: http://tests.e-netarts.com/batching.php
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

My code looks now:

Code: Select all

<?php
function myHistory()
{   
  $link="https://www.e-gold.com/acct/historycsv.asp";
  $pass="***********";
  $account="***********"; 
  $batch="********";
  $theString ="historycsv.asp?batchfilter=$batch";
  $theString .="&PassPhrase=$pass";
  $theString .="&AccountID=$account";  
  $ch = curl_init("https://www.e-gold.com/acct/"); 
  $fp = fopen($theString, "r");
  $fp = fopen($theString, "w+");
  
  curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_setopt($ch, CURLOPT_HEADER, 0);  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  curl_exec($ch); 
  rewind($fp);
  curl_close($ch);
  while ($data = fgetcsv($fp, 1000, ",")) 
  {
    $num = count($data);
    echo "<p> $num fields in line $row: <br />\n";
    $row++;
    for ($c=0; $c < $num; $c++) 
    {
        echo $data[$c] . "<br />\n";
    }
  }
  
  fclose($fp);
}

?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

<?php
function myHistory()
{   
  $link="https://www.e-gold.com/acct/historycsv.asp";
  $pass="***********";
  $account="***********"; 
  $batch="********";
  $theString ="historycsv.asp?batchfilter=$batch";
  $theString .="&PassPhrase=$pass";
  $theString .="&AccountID=$account";  
  $ch = curl_init("https://www.e-gold.com/acct/"); 
 // $fp = fopen($theString, "r");
  $fp = fopen($theString, "w+");
  
  curl_setopt($ch, CURLOPT_FILE, $fp);
  curl_setopt($ch, CURLOPT_HEADER, 0);  
//  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  curl_exec($ch); 
  rewind($fp);
  curl_close($ch);
  while ($data = fgetcsv($fp, 1000, ",")) 
  {
    $num = count($data);
    echo "<p> $num fields in line $row: <br />\n";
    $row++;
    for ($c=0; $c < $num; $c++) 
    {
        echo $data[$c] . "<br />\n";
    }
  }
  
  fclose($fp);
}

?>
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

Yes but instead to display me the results gives me "This Virtual Directory does not allow contents to be listed. " And because is a sub-domain i caanot set the default file.

Any suggestion?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

change this:

Code: Select all

$ch = curl_init("https://www.e-gold.com/acct/");
to this:

Code: Select all

$ch = curl_init("https://www.e-gold.com/acct/".$theString);
and try.
I think it's not issue with your script, but instead is how the e-gold works (which I know nothing about ;) ).
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

The e-gold will give me a CSV file.

I was thinking to take him like a string, that using that csv function provided by PHP to format data and to display them in my page. My problem is that i cannot display the received file because it gives me that error.

I know that is a tricky problem, but i'm a curl new user :(.

Any ideas?
Post Reply