Search Webmaster Help and Solution

Help on cURL POST to remote page with javascript

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 cant 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 havent 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 doesnt work on the submission level because I managed to receive confirmation e-mails for manual submissions.

PHP Code: <?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 ({&quot;c&quot;:{&quot;i&quot;:&quot;2&quot;,&quot;n&quot;:&quot;United States&quot;,&quot;t&quot;:&quot;1&quot;,&quot;s&quot;:&quot;US&quot;},&quot;m&quot;:{&quot;i&quot;:&quot;1&quot;,&quot;v&quot;:&quot;+1 (###) ###-####&quot;,&quot;d&quot;:&quot;&quot;,&quot;f&quot;:&quot;&quot;,&quot;c&quot;:&quot;&quot;,&quot;r&quot;:&quot;&quot;}}),
    ctl00$MainSection$tbSenderName => I am sender, //max length = 50
    ctl00$MainSection$tbSenderCompany => I am sender company, //max length = 50
    ctl00$MainSection$tbSenderEmailAddress => This e-mail address is being protected from spambots. You need JavaScript enabled to view it , //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=([sS]+?)/ , $strGet_page_contents , $arrView_state ) ) {
        $strView_state = $arrView_state[1];
        $arrPost_data[__VIEWSTATE] = $strView_state;
    }
    if ( preg_match ( /__EVENTVALIDATION[s]+?value=([sS]+?)/ , $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=([sS]+?)/ , $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);
?> 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 cant 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 havent 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 doesnt work on the submission level because I managed to receive confirmation e-mails for manual submissions.

PHP Code: <?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 ({&quot;c&quot;:{&quot;i&quot;:&quot;2&quot;,&quot;n&quot;:&quot;United States&quot;,&quot;t&quot;:&quot;1&quot;,&quot;s&quot;:&quot;US&quot;},&quot;m&quot;:{&quot;i&quot;:&quot;1&quot;,&quot;v&quot;:&quot;+1 (###) ###-####&quot;,&quot;d&quot;:&quot;&quot;,&quot;f&quot;:&quot;&quot;,&quot;c&quot;:&quot;&quot;,&quot;r&quot;:&quot;&quot;}}),
    ctl00$MainSection$tbSenderName => I am sender, //max length = 50
    ctl00$MainSection$tbSenderCompany => I am sender company, //max length = 50
    ctl00$MainSection$tbSenderEmailAddress => This e-mail address is being protected from spambots. You need JavaScript enabled to view it , //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=([sS]+?)/ , $strGet_page_contents , $arrView_state ) ) {
        $strView_state = $arrView_state[1];
        $arrPost_data[__VIEWSTATE] = $strView_state;
    }
    if ( preg_match ( /__EVENTVALIDATION[s]+?value=([sS]+?)/ , $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=([sS]+?)/ , $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);
?>

View Complete Thread with Replies

Related Items

Query failed: connection to localhost:3354 failed (errno=111, msg=Connection refused).