Blog

PHP, Tutorial

Post to Form with PHP and Curl

Part 3: Import CSV file to a FORM via POST using PHP and Curl.

Please Note: There is many options that I could of chosen to post the data from the CSV file into our Sales System. We could of done mysql import, excel import etc. For this particular case I chose to post directly to our sales form to avoid some nuances within the MySQL system that we had. Importing from a CSV to FORM using POST method can create your own DDos Attack (Denial of Service). Basically you can overwhelm your server that is taken the the new form request. Not a recommended way to import thousands of leads.

After harvesting the leads for my client I needed to implement them into their proprietary sales system. I choose to use their current Form using POST with PHP and Curl. 

This sample script will need to be modified to meet your own needs and environment but this will at least give you an idea of what the code and process looks like to do.

Comments and Explanation have been Bold & Italicized. If you understand php you know that these characters: # or // is commented sections of the code. Code does not execute, used to read comments. 

<?php
#########################################################
# Run This Script on command line php postleads.php
#########################################################

$fid = ‘61986’;//The FORM ID associated with this client leads. Need to post to the correct form

// initial Form Page
$URL = ‘https://sales.example.io/n/view.php?id=’.$fid;//FORM ID is added by the $fid assuring it post to the correct form. 

//Initiate the CURL session by calling the CURL object assigned to $ch
$ch = curl_init();

#####################################################################################################
# First Curl Session is to Set the Cookies and appear like a user accessing the form to initiate the form to load correctly
#################################################################################################

curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_COOKIEJAR, ‘cookiesale.txt’);//Get Current Cookies Set
curl_setopt($ch, CURLOPT_COOKIEFILE, ‘cookiesale.txt’);//SET Cookies returned by the site
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0’);//Used to spoof the Browser to show up as Mozilla/Firefox browser instead of a Linux Curl Browser.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen(‘php://stdout’, ‘w’));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$page = curl_exec($ch);//Execute Curl Command and put the HTML Source to the $page variable. 

// GET CSV File and loop through while submitting

$csvr = array_map(‘str_getcsv’, file(‘leads.csv’));//Convert the CSV file into an Array
$csvf = array_reverse($csvr);//Using Array Reverse to put the Oldest leads in First so they will show up the same way in the new system. 
$hf[‘form_id’] = $fid;//Submit to the correct form the FORM ID that was set above

//Setting some hidden fields required to submit correctly
$hf[‘submit_form’] = 1;
$hf[‘page_number’] = 1;

//Iterate through each Record Stored in the CSV File. Setting up the data to submit each record to the form
foreach($csvf as $csv){
$name = $csv[2];//Set Full Name of the Lead
$date = $csv[4];//Setting the Date Field
$sdata = ”;
foreach($csv as $v){//Storing all additional details into the Note Field
$val = explode(‘:’,$v);
if ($val){
$sdata .= $val[0].”: “.$val[1].”\n\r”;
}else{
$sdata .=$v.”\n\r”;
}
}
print_r($csv);

$form = $form[0];

$URL2 = ‘https://sales.exmaple.io/n/view.php’; // this is our new post url. WHere the Form Data is posted to via PHP and Curl.

// find all hidden fields which we need to send with our login, this includes security tokens
$count = preg_match_all(‘/<input type=”hidden”\s*name=”([^”]*)”\s*value=”([^”]*)”/i’, $form, $hiddenFields);

$postFields = array();

// add our submitted Fields
$postFields[‘element_27’] = $name;
$postFields[‘element_30’] = $date;
$postFields[‘element_28’] = $sdata;
foreach($hf as $k=>$v){
$postFields[$k] = $v;
}

$post = ”;

// Convert Array to a String. Form will not accept multipart/form-data, only application/x-www-form-urlencoded
foreach($postFields as $key => $value) {
$post .= $key . ‘=’ . urlencode($value) . ‘&’;
}

$post = substr($post, 0, -1);

// set additional curl options using our previous options
curl_setopt($ch, CURLOPT_URL, $URL2);
curl_setopt($ch, CURLOPT_REFERER, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

$page = curl_exec($ch); //Form Should of successfully submitted the data to our sales system.

}
?>

 

?>
Need a Similar Solution programmed for you? Check out MyAppBuilder Developement as a Subscription plans that include up to 1 hour of code a day for only $129 a month!