Thanks johanafm for your very helpful comments. I followed your instructions and got curl to display the headers of the POST request and fixed the content-type accordingly. I also trimmed the code to handle the cookies like how you did in your example. Many thanks!
However, I still couldn't get the script to work. What I mean by this is that -
when I use a browser to open the page and submit a fax, I would receive a confirmation email with a "confirmation link" in it. This is what I expect would happen if the script runs successfully. However, so far I have received no confirmation emails after using the script to submit my fax. Hence, I am still debugging the script to try to get it to work.
Further details:
(1) my updated script
<?php
//target page url
$strPage_url = 'http://www.myfax.com/free/';
//create array of data to be posted
$arrPost_data = array (
'ctl00$MainSection$tbRecipientName' => 'Recipient', // max length = 50
'ctl00$MainSection$tbRecipientCompany' => 'RecipientCompany', //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' => 'Sender', //max length = 50
'ctl00$MainSection$tbSenderCompany' => 'SenderCompany', //max length = 50
'ctl00$MainSection$tbSenderEmailAddress' => 'abc@example.com', //email
'ctl00$MainSection$fileUpload' => '@/files/file.pdf', //file
'ctl00$MainSection$tbMessage' => 'hello world!', //message
'ctl00$MainSection$ibSendFax.x' => mt_rand ( 1 , 182 ),
'ctl00$MainSection$ibSendFax.y' => mt_rand ( 1 , 40 ),
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATEENCRYPTED' => '',
'ctl00$MainSection$meeRecipientFaxNumber_ClientState' => '',
'ctl00$MainSection$tbFriend1' => '',
'ctl00$MainSection$tbFriend2' => '',
'ctl00$MainSection$tbFriend3' => '',
'ctl00$MainSection$hfRecipientFaxNumber' => 12062028273,
'ctl00$MainSection$hfRecipientFaxNumberMask' => '+1 (999) 999-9999',
'ctl00$MainSection$hfRecipientFaxNumberCountryId' => 2,
'ctl00$MainSection$hfRecipientFaxNumberMaskId' => 1,
'ctl00$MainSection$hfTimeZone' => '-480',
'ctl00$MainSection$hfModalMessage' => '',
'hiddenInputToUpdateATBuffer_CommonToolkitScripts' => 0
);
//several variables unique to each visit
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 ( '/AjaxControlToolkit\.NoBotBehavior, \{"ChallengeScript":"~([\d]+?)"/' , $strGet_page_contents , $arrAnti_spam ) ) {
$strAnti_spam = $arrAnti_spam[1];
$intAnti_spam_value = ~intval($strAnti_spam);
$arrPost_data['ctl00$MainSection$nbAntiSpam$nbAntiSpam_NoBotExtender_ClientState'] = $intAnti_spam_value; //
}
//preparing data for posting
foreach ( $arrPost_data as $key => $value ) {
//1. escape the $'s
$strNew_key = str_replace ( '$' , '\$' , $key );
$strNew_value = str_replace ( '$' , '\$' , $value );
//2. urlencode
$strNew_key = urlencode ( $strNew_key );
$strNew_value = urlencode ( $strNew_value );
$arrPost_data[$strNew_key] = $strNew_value;
if ( $strNew_key != $key ) {
unset ( $arrPost_data[$key] );
}
}
//set page url
$curl_connection = curl_init ($strPage_url);
//set curl options
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729)");
curl_setopt($curl_connection, CURLOPT_REFERER, "http://www.myfax.com/free/");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, 'CURLCOOKIE');
curl_setopt($curl_connection, CURLOPT_HEADER, true);
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
//visit page to get cookies
$strGet_page_contents = curl_exec ($curl_connection);
//log page
$fhGet_page = fopen ( 'Get_page.html' , 'w' );
fwrite ( $fhGet_page , $strGet_page_contents );
fclose ( $fhGet_page );
//2nd curl connection
//set headers: mimic a firefox connection
$arrHeaders = array (
'Host: www.myfax.com',
'Origin: http://www.myfax.com',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-us,en;q=0.5',
'Accept-Encoding: gzip,deflate',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Keep-Alive: 115',
'Connection: keep-alive'
);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $arrHeaders );
//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $arrPost_data);
//display headers
curl_setopt($curl_connection, CURLINFO_HEADER_OUT, true);
//post to page
$strPost_page_contents = curl_exec($curl_connection);
//log the page
$fhPost_page = fopen ( 'Post_page.html' , 'w' );
fwrite ( $fhPost_page , $strPost_page_contents );
fclose ( $fhPost_page );
//show information regarding the request
echo "<pre>";
print_r(curl_getinfo($curl_connection, CURLINFO_HEADER_OUT));
echo curl_errno($curl_connection) . '-' . curl_error($curl_connection);
//close the connection
curl_close($curl_connection);
?>
(2) output from my script
POST /free/ HTTP/1.1
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729)
Referer: http://www.myfax.com/free/
Cookie: ProtusIPSolutions=4211124416.20992.0000; ASP.NET_SessionId=yez3aw55s03jsv45412qbb45
Host: www.myfax.com
Origin: http://www.myfax.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Length: 3737
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------9add31cf2d64
0-