Help on cURL POST to remote page with javascript
Posted: Sat Aug 07, 2010 4:22 am
Hi all,
I am new here and I really thank God that I found this forum! I have read the materials here on working with cURL on javascript-form-submission pages. However, I can't to get my script to work. Can anybody here please help me out or drop me a hint on where to correct my script?
==Situation==
My company utilizes http://www.myfax.com/free/ to send our company faxes. My task is to write a code that would submit files for faxes electronically.
Note: The site also requires e-mail confirmation but I haven't get to that stage yet. I have run tests on submitting fax requests both by code and manually through the site, and have confirmed that the code doesn't work on the submission level because I managed to receive confirmation e-mails for manual submissions. Also, tried my script with different staff email addresses because I figured out that it blocks only the same e-mail address from sending more than 2 faxes a day.
I am new here and I really thank God that I found this forum! I have read the materials here on working with cURL on javascript-form-submission pages. However, I can't to get my script to work. Can anybody here please help me out or drop me a hint on where to correct my script?
==Situation==
My company utilizes http://www.myfax.com/free/ to send our company faxes. My task is to write a code that would submit files for faxes electronically.
Note: The site also requires e-mail confirmation but I haven't get to that stage yet. I have run tests on submitting fax requests both by code and manually through the site, and have confirmed that the code doesn't work on the submission level because I managed to receive confirmation e-mails for manual submissions. Also, tried my script with different staff email addresses because I figured out that it blocks only the same e-mail address from sending more than 2 faxes a day.
Code: Select all
<?php
//target page url
$strPage_url = 'www.myfax.com/free/';
//create array of data to be posted
$arrPost_data = array (
'ctl00$MainSection$tbRecipientName' => 'I am recipient', //max length = 50
'ctl00$MainSection$tbRecipientCompany' => 'I am recipient company', //max length = 50
'ctl00$MainSection$tbRecipientFaxNumber' => '+1 (206) 202-8273', //recipient fax
'ctl00$MainSection$ddlRecipientCountry' => html_entity_decode ('{"c":{"i":"2","n":"United States","t":"1","s":"US"},"m":{"i":"1","v":"+1 (###) ###-####","d":"","f":"","c":"","r":""}}'),
'ctl00$MainSection$tbSenderName' => 'I am sender', //max length = 50
'ctl00$MainSection$tbSenderCompany' => 'I am sender company', //max length = 50
'ctl00$MainSection$tbSenderEmailAddress' => 'abc@example.com', //email
'ctl00$MainSection$nbAntiSpam$nbAntiSpam_NoBotExtender_ClientState' => '-150', //number drawn from inspecting the packages sent by manual form submission
'ctl00$MainSection$fileUpload' => '@/files/file.pdf', //file
'ctl00$MainSection$tbMessage' => 'hello world', //message
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATEENCRYPTED' => ''
);
//visit the page and get cookies
$curl_connection = curl_init ($strPage_url);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_REFERER, "http://www.myfax.com/free/");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl_connection, CURLOPT_COOKIEJAR, 'CURLCOOKIE');
$strGet_page_contents = curl_exec ($curl_connection);
curl_close ($curl_connection);
//get page to retrieve view state and event validation
if ( preg_match ( '/"__VIEWSTATE"[\s]+?value="([\s\S]+?)"/' , $strGet_page_contents , $arrView_state ) ) {
$strView_state = $arrView_state[1];
$arrPost_data['__VIEWSTATE'] = $strView_state;
}
if ( preg_match ( '/"__EVENTVALIDATION"[\s]+?value="([\s\S]+?)"/' , $strGet_page_contents , $arrEvent_validation ) ) {
$strEvent_validation = $arrEvent_validation[1];
$arrPost_data['__EVENTVALIDATION'] = $strEvent_validation;
}
if ( preg_match ( '/id="ctl00_MainSection_nbAntiSpam_nbAntiSpam_NoBotExtender_ClientState" value="([\s\S]+?)"/' , $strGet_page_contents , $arrAnti_spam ) ) {
$strAnti_spam = $arrAnti_spam[1];
$arrPost_data['ctl00$MainSection$nbAntiSpam$nbAntiSpam_NoBotExtender_ClientState'] = $strAnti_spam;
}
//traverse array and prepare data for posting (key1=value1)
foreach ( $arrPost_data as $key => $value) {
$arrPost_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$strPost_string = implode ('&', $arrPost_items);
//create cURL connection
$curl_connection = curl_init($strPage_url);
//set options
curl_setopt ($curl_connection, CURLOPT_POST, 1);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_REFERER, "http://www.myfax.com/free/");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//set cookie
curl_setopt ($curl_connection, CURLOPT_COOKIEFILE, 'CURLCOOKIE');
unlink ( 'CURLCOOKIE' );
curl_setopt($curl_connection, CURLOPT_COOKIE, session_name() . '=' . session_id());
//set header
$arrHeaders = array ( 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' );
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $arrHeaders );
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $strPost_string);
//perform our request
$strResult = curl_exec($curl_connection);
//show information regarding the request - for debugging
echo "<pre>";
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);
echo "<hr>";
var_dump ($arrPost_items);
echo "</pre>";
//close the connection
curl_close($curl_connection);
?>