Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Last active March 8, 2020 19:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save macbookandrew/5740526e17a6a93776d5 to your computer and use it in GitHub Desktop.
Save macbookandrew/5740526e17a6a93776d5 to your computer and use it in GitHub Desktop.
PHP cURL proxy for Ajax—Infusionsoft

Introduction

This is a PHP proxy to submit Ajax requests to Infusionsoft.

Usage

  1. Set your Infusionsoft app id, thank-you message, and path to the cacert.pem bundle in infusionsoft-proxy-settings.php.
  2. Add the Infusionsoft form IDs in infusionsoft-proxy.php, creating new lines as necessary
  3. Include the functions.js in your javascript, making sure to replace the $form variable with the selector(s) you need.
$( document ).ready( function() {
// change the selector below to match your needs
$form = $( '#credentials-form' );
$form.submit( function(e) {
e.preventDefault();
// perform simple validation; change fields to match your needs
if ( ( ! inf_field_FirstName ) || ( ! inf_field_LastName ) || ( ! inf_field_Email ) || ( ! inf_field_Phone1 ) ) {
alert( 'Please complete all fields.' );
} else {
// submit data to Infusionsoft via proxy script
$.post( 'infusionsoft-proxy.php', $form.serialize() )
.done( function( data ) {
$form.replaceWith( '<iframe></iframe>' );
$form.( 'iframe' ).contents().find( 'body' ).append( data );
});
}
});
});
<?php
require_once( 'infusionsoft-proxy-settings.php' );
$form_location = $url_base . 'process/' . $_GET['inf_form_xid'];
?>
<form id="infusionsoftFallback" method="post" action="<?php echo $form_location; ?>">
<h1>Processing&hellip;</h1>
<?php
foreach( $_GET as $key => $value ) {
echo '<input type="hidden" name="' . $key . '" value="' . $value . '" />';
}
?>
<script type="text/javascript">document.getElementById( "infusionsoftFallback" ).submit();</script>
<noscript>
<input type="submit" value="Submit" />
</noscript>
</form>
<?php
$debugging = false;
$url_base = 'https://app-id.infusionsoft.com/app/form/';
$thank_you_message = '<h2>Thanks for your interest; we&rsquo;ll get that info over to you right away!</h2>';
$ca_bundle_location = 'D:\home\site\wwwroot\cacert.pem'; // absolute path to the file on your server; download the newest version from http://curl.haxx.se/ca/cacert.pem
$valid_info_form_xids = array(
'FORM-ID-HERE', // add more entries as needed
);
?>
<?php // from http://davidwalsh.name/curl-post
require_once( 'infusionsoft-proxy-settings.php' );
// extract data from the post
extract($_POST);
// set POST variables
$fields = array(
'inf_form_xid' => urlencode( $inf_form_xid ),
'inf_form_name' => urlencode( $inf_form_name ),
'infusionsoft_version' => urlencode( $infusionsoft_version ),
'inf_field_FirstName' => urlencode( $inf_field_FirstName ),
'inf_field_LastName' => urlencode( $inf_field_LastName ),
'inf_field_Email' => urlencode( $inf_field_Email ),
'inf_field_Phone1' => urlencode( $inf_field_Phone1 ),
);
// url-ify the data for the POST
foreach( $fields as $key=>$value ) {
$fields_string .= $key.'='.$value.'&';
}
rtrim( $fields_string, '&' );
// whitelist URLs based on xid
// if this allowed the xid to create any URL, that could be a security hole, allowing anybody to post to whatever URL they want
if ( in_array( $inf_form_xid, $valid_inf_form_xids ) ) { $url = $url_base . 'process/' . $inf_form_xid; } // duplicate this line as needed
// open connection
$ch = curl_init();
// set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, count($fields) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields_string );
// downloaded from http://curl.haxx.se/ca/cacert.pem
curl_setopt( $ch, CURLOPT_CAINFO, $ca_bundle_location );
// debugging
if ( $debugging ) {
$verbose = fopen( 'php://temp', 'rw+' );
curl_setopt( $ch, CURLOPT_VERBOSE, true );
}
// execute post
$result = curl_exec( $ch );
// debugging
if ( $debugging ) {
if ( $result === false ) {
printf( "cURL error (#%d): %s<br/>\n", curl_errno( $ch ) , htmlspecialchars( curl_error( $ch ) ) );
}
rewind( $verbose );
$verboseLog = stream_get_contents( $verbose );
echo "Verbose information:\n<pre>" . htmlspecialchars( $verboseLog ) . "</pre>\n";
echo 'This script is located at: ' . __FILE__;
}
// check response for success message
if ( $result != false ) {
header( 'content-type:application/json' );
return $thank_you_message;
} else {
if ( $debugging ) { var_dump($_POST); }
$fallback_url = "http://" . $_SERVER['SERVER_NAME'] . str_replace( 'infusionsoft-proxy.php', 'infusionsoft-proxy-fallback.php', $_SERVER['REQUEST_URI'] ) . '?' . http_build_query( $_POST );
if ( $debugging ) { echo $get_string; }
header( 'Location:' . $fallback_url );
exit;
}
// close connection
curl_close( $ch );
?>
@trinidado007
Copy link

trinidado007 commented Oct 4, 2016

$form.( 'iframe' ).contents().find( 'body' ).append( data );

throwinG : Uncaught SyntaxError: Unexpected token (

Can't get it to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment