Convert a .csv file to URLs

Status
Not open for further replies.

Mike

New member
Jun 27, 2006
6,777
116
0
51
On the firing line
Okay, I'm having a hellish time finding the answer to this using Google. I have a feeling I'm not using the right terminology. So, I'll try to explain what I want to do.

I would like to be able to take a .csv (or tab delimited) document and convert it to URLs to post data using the GET method.

So, I've got the post URL, I need to append the data to it.

Basically, the "&field-name=" would be the header row. Example:

Code:
fname,lname,address,address2
bob,smith,123 main street,apt 4

And I want the output to be:
Code:
http://www.post-url.com/script.php?fname=bob&lname=smith&address=123%20main%20street&address2=apt%204

Can someone please point me in the right direction?

Thanks!
 


Use Access (or MySQL for that matter). Import the CSV into a table. Then, create this query:

Code:
SELECT "http://www.post-url.com/script.php?fname=" & [fname] & "lname=" & [lname] & "address=" & [address] AS MyURL
FROM myImportedCSVTable;
It'll create a URL for every record in your table (i.e. the data that used to be your CSV file)
 
Code:
$FeedFile = 'yoururlfile.txt';
$feed = fopen($FeedFile, 'r');
$data = fgetcsv($feed, 3000, "|")
will put each line and each item (seperated by | ) in to an array, and then you can just use regex or whatever to turn them in to urls.
 
This is what Smaxor gave me to work with:
Code:
 <?php 
 $urls = array();
 $fields = array('field1','filed2','field3');
 $lines = file ('filename.txt');
 foreach ($lines as $line)
     {   $values = explode(",",$line);
         $url = 'http://dest.com/location.html?';
         for($i = 0; $i < count(values); $i++);
             { $url .= $fields[$i]."=".$value[$i];
             } 
         $urls[] = $url;
     }
 ?>

I'm going to try it, and also look at all of your suggestions. Thanks everyone, I appreciate the help, as always. :D
 
Okay, just to update, the code above didn't work as planned. So, I went back to Smaxor and we looked at it together and here is the working code:

Code:
<?php 
$urls = array();
$fields = array('&SubAffiliateID1','&Ipaddress','&Fname','&Lname','&Email','&Address','&Address2','&City','&State','&zip','&dayphone1','&dayphone2','&dayphone3','&Nightphone1','&Nightphone2','&Nightphone3','&Nightphone4','&Contacttime','&TotalDebt','&home');
$lines = file('filename.txt');

foreach ($lines as $line)  
     {
    $values = explode(",",$line);
    $url = 'http://www.posting-url.com/script.php?SourceID=123456'; 
   
      foreach($values as $num => $value)
          {
        $url .= $fields[$num]."=".urlencode($value);
        }
    echo '<a href='.$url.'>Lead</a><br />';
    $urls[] = $url;
     }
 ?>
I works great. Basically, it outputs an HTML page with "Lead" on each line, hyperlinked to the URL I want to submit it to. So, I can just go down the page and click on each "Lead" and submit my leads.

I'm going to try and add some cURL to it to automate it even further.

Thanks again Smaxor!

/edit/ Figured out the cURL part of it. Just replace:
Code:
  echo '<a href='.$url.'>Lead</a><br />';

With this:
Code:
       $user_agent = "Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)";
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_POST,1);
       curl_setopt($ch, CURLOPT_URL,$url);
       curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    
       $result=curl_exec ($ch);
       curl_close ($ch);
    
       echo "Result: ".$result."<br />";

Now, all I have to do, is make sure my 'filename.txt' is formatted correctly and run this script. :D
 
Status
Not open for further replies.