parse data from an csv file to a remote server

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
mspace
Forum Newbie
Posts: 1
Joined: Thu Nov 18, 2010 2:23 pm

parse data from an csv file to a remote server

Post by mspace »

hello there greate forum.
I have to read a csv file with records from my database and then post the data to a remote server in according fields that they have give me, explode the pass and user name by getting the session id. I have manage to read the file and then print its contents to a page but i can't post the data to the server so to success the postage of mass storage to remote database. I always get the error that cannot open the file. I think i have custom my php ini correct according directions that i found in some posts.
Can anyone help me to correct steps that i have to follow?
thank you!
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: parse data from an csv file to a remote server

Post by mecha_godzilla »

To send data to the remote server, you'll need to know exactly what format the remote server is expecting the data to be sent in (does it use GET or POST?) and you can then use cURL - if you have it installed - to send the data across.

To give you an example of how this might work:

I recently had to use PHP on my desktop machine to open a CSV file, convert some of the values and then send the data to a remote server (which accepts the data as a POST request). The first thing I did was create a custom function:

Code: Select all

function use_curl($post_data,$URL) {

    $ch = curl_init();   
    curl_setopt($ch, CURLOPT_URL,"http://$URL");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
    curl_exec ($ch);
    curl_close ($ch);
		
}
then I prepared the data to create the POST values:

Code: Select all

$id = 1234; // example value for the record ID
$name = 'John Smith'; // example value to go in the 'name' field for this record

$post_data = "id=$id";
$post_data .= "&";
$post_data .= "name=$name";
then I called the custom function:

Code: Select all

use_curl($post_data,'www.mydomain.com/receive_database_values.php');
		
$post_data = NULL;
		
echo 'Successfully submitted this value: ' . $id . '<br />';
The reason why the $post_data value is NULLed is just to make that when you're looping through a set of records, $post_data is emptied. Also note that when I call my custom function, I don't include the http:// part of the URL because it's hard-coded in the function (this isn't ideal but you can obviously change this). You also need to make sure you escape your values before they're sent, otherwise you might have values with ' or " in them that will stop the remote database's script from working - if the remote server isn't escaping the values correctly, cURL should display the error message.

The code I've provided gives you a very quick overview of cURL, so make sure you consult the manual to find out what options it takes.

If you need any other help please say so.

HTH,

Mecha Godzilla
Post Reply