klee1981 Posted October 19, 2014 Share Posted October 19, 2014 (edited) Hello all; I have been banging my head against the keyboard for a couple of days trying to get this PHP code sorted out, and I would really appreciate your help. Here is what I am trying to accomplish, and what is going wrong with my efforts. I am trying to get Gravity forms to pass my form values to a RepairShopr API, that will generate a lead for our business when a customer fills out the form on our website. Yes, we can just use the email that Gravity Forms generates to contact the customer, but we are trying to move toward the RepairShopr Leads API, so we don't need to enter the information manually. Part one of my headache comes from the fact that Gravity Forms, does not yet support native passing of its values to a third party API. This problem was solved for me by using the code below, provided by ZeroTo5ive. Their link to the information I found is below. http://0to5.com/gravity-forms-submitting-forms-to-3rd-party-applications/ I modified their code, which now looks like this, and was pasted into my functions.php add_action("gform_post_submission", "set_post_content", 10, 2); function set_post_content($entry, $form){ // Lets get the IDs of the relevant fields and prepare an email message //$message = print_r($entry, true); // In case any of our lines are larger than 70 characters, we should use wordwrap() //$message = wordwrap($message, 70); // Send //mail('travis@0to5.com', 'Getting the Gravity Form Field IDs', $message); function post_to_url($url, $data) { $fields = ''; foreach($data as $key => $value) { $fields .= $key . '=' . $value . '&'; } rtrim($fields, '&'); $post = curl_init(); curl_setopt($post, CURLOPT_URL, $url); curl_setopt($post, CURLOPT_POST, count($data)); curl_setopt($post, CURLOPT_POSTFIELDS, $fields); curl_setopt($post, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($post); curl_close($post); } $data = array( "name" => $entry["1"], "phone" => $entry["2"], "email" => $entry["3"], "device" => $entry["4"], "description" => $entry["8"], ); post_to_url("http://www.igenierepair.com/shopr.php", $data); } This passes my Gravity Form field values to a PHP file on my server that was based on the sample from RepairShopr; here is that code. <?php if( $_POST["name"] ) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"http://MYSUBDOMAIN.repairshopr.com/api/v1/leads?api_key=MYAPIKEY"); curl_setopt($ch, CURLOPT_POST, 1); // example splitting a name field into first and last name bits $pieces = explode(" ", $_POST['name']); $last_name_parts = array_slice($pieces, 1); //each word $last_name = implode(" ", $last_name_parts); //combined words, with space separators // build up the lead post data here, you can combine fields into the data you are sending // see subject for an example curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('first_name' => $pieces[0], 'last_name' => $last_name, 'email' => $_POST['email'], 'phone' => $_POST['phone'], 'ticket_subject' => $_POST['device'], 'ticket_description' => $_POST['description'], ))); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch); exit(); } ?> As you can see, the Zeroto5ive PHP seems to translate, and then pass my field values to my RepairShopr PHP file, which is then passed to the RepairShopr Leads API, generating a lead for our business in RepairShopr, our Customer Management System. Everything is fine and dandy, the process works, and a lead is generated in our CMS. I am having an issue right here 'ticket_subject' => $_POST['device'], This is the subject line of our ticket, and I am trying to get it to display multiple field values from Gravity Forms. Our form asks our customer who makes their phone, what model of phone it is, and what color it is. I was looking for a way to pass the values from all the relevant fields, and display them like so: Apple - iPhone 5 - Black But I can only get it to pass one value to our API. I have tried the following, as well as various permutations, and arrays, but my knowledge is very limited. I added phonemake to the 0to5 PHP, and associated it with its correlating Gravity Forms field ID, and then added phonemake to the repairshopr php, I know the period concatenates, and I am sure I am using it incorrectly. I have spent hours on Google trying to find a solution to this. 'ticket_subject' => $_POST['device' . 'phonemake'], I know it was wrong, but I am just a copy and paste hack, out of my depth here. You get the idea, I need a way to collect the data from about 6 fields, and pass it to our API. These fields are from a drop down menu, with an empty "Please select one" option. I am hoping that will pass nothing to the API if they leave a field blank. A link to my test page is below, so you can get an idea of what I am dealing with. http://www.igenierepair.com/new-test-page/ Please help me. Edited October 19, 2014 by klee1981 Quote Link to comment https://forums.phpfreaks.com/topic/291919-wordpress-gravity-forms-and-repairshopr-api/ Share on other sites More sharing options...
QuickOldCar Posted October 19, 2014 Share Posted October 19, 2014 (edited) To concatenate the $_POST data you do it like this $ticket_subject = $_POST['device'] . " - " . $_POST['phonemake']; Up higher in the form I would check all these post values and make a string variable. Can even do an array and implode characters between each additional if you wish. $ticket_subject = ''; if(isset($_POST['device']) && trim($_POST['device']) != ''){ $ticket_subject .= trim($_POST['device'] . " - "; } if(isset($_POST['phonemake']) && trim($_POST['phonemake']) != ''){ $ticket_subject .= trim($_POST['phonemake'] . " - "; } $ticket_subject = rtrim(trim($ticket_subject), "-"); Then you have this 'ticket_subject' => $ticket_subject, Edited October 19, 2014 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/291919-wordpress-gravity-forms-and-repairshopr-api/#findComment-1494139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.