I'm trying to post the content of a multidimensional array that I build from a CSV file. I also need to urlencode the individual fields.
The csv file looks like this:
firstname,lastname,addr1,addr2,city,state,postalcode,country,tel,affiliateref,products
sally,Robin,3015 pinetree lane,TORRANCE MARRIOTT SOUTH BAY,STOUGHTON,CA,92604,United States,561-339-7472,110-7222533-9401838,"40310,40310,40310,40310"
Sharon L.,McGough,3165 GRASSLAND DR,Shoreside & Port Services 3213 West Wheeler ST No 600,POWHATAN,VA,90047-4316,United States,804-317-7986,112-6898710-7394643,95272
Erin,Townsend,217 Bridge Street,Apt E8,MIAMI,FLORIDA,33130-4338,United States,+ 55 11 97338-4281,110-2748711-1166643,149843
My PHP script is supposed to take each row, and throw it into an array. Then post each individual row from that csv file to a server.
Here's the script:
<?php
$file = fopen('orders.csv', 'r');
$all_rows = array();
$header = null;
while ($row = fgetcsv($file)) {
if ($header === null) {
$header = $row;
continue;
}
$all_rows[] = array_combine($header, $row);
}
foreach ($all_rows as $rows => $order) {
extract($order);
//set POST variables
$url = 'http://www.example.com/CGI/receive.asp?';
$order = array(
'siteId' => urlencode($siteId),
'firstname' => urlencode($firstname),
'lastname' => urlencode($lastname),
'addr1' => urlencode($addr1),
'addr2' => urlencode($addr2),
'city' => urlencode($city),
'state' => urlencode($state),
'postalcode' => urlencode($postalcode),
'country' => urlencode($country),
'tel' => urlencode($tel),
'affiliateref' => urlencode($affiliateref),
'products' => urlencode($products)
);
//url-ify the data for the POST
// foreach($order as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
// rtrim($fields_string, '&');
//open connection
$ch = curl_init();
// curl
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, count($order));
// curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
}
?>