1 2 Previous Next 18 Replies Latest reply on Oct 6, 2017 11:02 PM by Devon Guerrero

    Form Submission Via REST API Using PHP Request Library

    ekazda

      Can someone please post how to use the PHP Request Library found at fredsakr/eloqua-php-request · GitHub to post form data to Eloqua?

       

      So far, I have to following:

       

      // include the Eloqua REST client 
      require_once('eloquaRequest.php');  
      
      
      // define the Contact class  
      class ContactForm  
      {
        public $FirstName;  
        public $LastName;  
        public $PhoneNumber;
        public $Institution;  
        public $ContactComment;  
        public $EmailAddress; 
      } 
      
      $client = new EloquaRequest('SITE', 'USERNAME', 'PASSWORD', 'https://secure.eloqua.com/API/REST/1.0');
      
      // instantiate a new instance of the ContactForm class 
      $contact = new ContactForm();  
      $contact->FirstName = 'Sample';  
      $contact->LastName = 'Import'.date("Y-m-d-H-i-s");  
      $contact->PhoneNumber = '111-111-1111';
      $contact->Institution = 'Company Name';
      $contact->ContactComment = 'This is a test of the API';
      $contact->EmailAddress = 'test@test.com'; 
      
      // invoke a POST request to create the contact 
      $response = $client->post('/data/form/45', $contact);
      
      


      However, this returns the following:


      array(1) { [0]=> object(stdClass)#318 (4) { ["type"]=> string(21) "ObjectValidationError" ["property"]=> string(11) "fieldValues" ["requirement"]=> object(stdClass)#316 (1) { ["type"]=> string(23) "NoDuplicatesRequirement" } ["value"]=> string(6) "" } }


      Can somebody please give me a step by step guide on how to do this correctly?

        • 1. Re: Form Submission Via REST API Using PHP Request Library
          ekazda

          Any help with this problem would be appreciated. I'm up against a project deadline and this integration is starting to hold things up.

          • 3. Re: Form Submission Via REST API Using PHP Request Library
            freejung

            Maybe I'm missing something, but why not just post the form directly? You could use cURL to just post the data as an array to the action URL of the form. You wouldn't even have to authenticate, since the form processor should accept post data from any source.

             

            I could post code for that if you like.

            • 4. Re: Form Submission Via REST API Using PHP Request Library
              ekazda

              I would appreciate that freejung. That will at least be an alternative to the other approach.

              • 5. Re: Re: Form Submission Via REST API Using PHP Request Library
                freejung

                OK. This is based on code I use to submit form field values to an Eloqua form. You will need to look at the HTML code for the actual form to get the action URL, the form name, and your site id.

                 

                $url = 'http://your.form.action.url';
                
                $formFields = array();
                
                $formFields['elqFormName'] = 'your_eloqua_form_name';
                $formFields['elqSiteId'] = 'your_eloqua_site_id_number';
                $formFields['elqCampaignId'] = 'your_campaign_id'; /*optional*/
                    
                $formFields['FirstName']  = 'Sample';    
                $formFields['LastName']  = 'Import'.date("Y-m-d-H-i-s");    
                $formFields['PhoneNumber']  = '111-111-1111';  
                $formFields['Institution']  = 'Company Name';  
                $formFields['ContactComment']  = 'This is a test of the API';  
                $formFields['EmailAddress']  = 'test@test.com';
                
                $postItems = array();
                foreach ($formFields as $key => $value) {
                    $postItems[] = $key . '=' . $value;
                }
                $postString = implode ('&', $postItems);
                $curlConnection = curl_init($url);
                curl_setopt($curlConnection, CURLOPT_CONNECTTIMEOUT, 30);
                curl_setopt($curlConnection, CURLOPT_USERAGENT,
                  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
                curl_setopt($curlConnection, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curlConnection, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curlConnection, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($curlConnection, CURLOPT_POSTFIELDS, $postString);
                $result = curl_exec($curlConnection);
                curl_close($curlConnection);
                
                1 person found this helpful
                • 6. Re: Form Submission Via REST API Using PHP Request Library
                  freejung

                  Eric - I poked around in the documentation to see if I could figure out how to do a form submission via the REST API as you specified. My conclusion is, don't do that unless for some reason you really have to. The method I posted is much simpler.

                   

                  I haven't tried this, so I'm not certain this is how it will work (I'm sure Fred could elaborate), but from my understanding you would have to do quite a bit of groundwork before actually creating the form submission.

                   

                  First, you need the contact ID for this contact. So you'll have to look up the contact by email address, and create a new contact if it's not already in the database, then get the ID of the new contact.

                   

                  Once you have the contact ID, you'll also need to get all of the data on the form itself - in particular, you need the id numbers of each form field. So you would use GET /assets/form/{id} as in this post http://topliners.eloqua.com/docs/DOC-3406 to pull the form data and get the id numbers for each field.

                   

                  Then you'll have to create an object to submit to the form. The object can't just be the form field values. It will have to have the properties submittedAt, submittedByContactId, and fieldValues which itself is an array of objects of type fieldValue, each of which will need to have the properties type, id and value.

                   

                  Anyway, that's quite a bit more complicated than it needs to be, unless you have a very strong reason for doing it that way. If you just submit the form using the code I posted, it handles all of that for you just as it would if someone actually fills out the form on a landing page.

                  • 7. Re: Form Submission Via REST API Using PHP Request Library
                    fsakr

                    Hi everybody,

                     

                    We've added a 'samples' folder to the PHP request library repository on Github. The first sample demonstrates how to submit form data using the API :

                     

                    Please note that you can describe the Form fields and other metadata using the following endpoint :

                     

                    Hope this helps and please let me know if you need more information.

                     

                    Thanks,

                    Fred

                    • 8. Re: Form Submission Via REST API Using PHP Request Library
                      ekazda

                      This is the solution I actually went with since it was, as you said, much easier than using the REST API. However, I did mark fsakr's answer as the correct answer because it directly answered my original request. Thank you for helping me out.

                      • 9. Re: Form Submission Via REST API Using PHP Request Library
                        ekazda

                        Thanks for adding the samples. Those are very helpful.

                        • 10. Re: Form Submission Via REST API Using PHP Request Library
                          freejung

                          Excellent, I'm glad you found it useful.

                          • 11. Re: Form Submission Via REST API Using PHP Request Library
                            Md. Shahid

                            Hi ,

                             

                            This was very helpful for me and thank you so much for this post, I used google captcha and post the data using this code.

                             

                            Thanks & Regards

                            Md.Shahid

                            • 13. Re: Form Submission Via REST API Using PHP Request Library
                              erickazda

                              fsakr, I've returned to this project because the curl approach suggested by freejung no longer works.

                               

                              I have the following code, but curl_exec($this->ch) [line 86 of eloquaRequest.php] always returns false and the $httpCode [line 91 of eloquaRequest.php] always returns 0.

                               

                              include('eloquaRequest.php');
                              include('models/identifiableContract.php'); //I PULLED THE REFERENCE OUT OF THE formData.php file because it couldn't find the file for some reason
                              include('models/data/formData.php');
                              // instantiate a new instance of the request class
                              $request = new EloquaRequest('site', 'username', 'password;', 'https://secure.eloqua.com/API/REST/1.0');
                              // populate values for each field, note that you can describe
                              // a form and its fields using the following endpoint : GET /assets/form/{id}
                                  
                              // First Name
                              $firstNameField = new FieldValue();
                              $firstNameField->id = 427;
                              $firstNameField->value = $_POST['FirstName'];
                              $firstNameField->type = 'FieldValue';
                                  
                              // Last Name
                              $lastNameField = new FieldValue();
                              $lastNameField->id = 428;
                              $lastNameField->value = $_POST['LastName'];
                              $lastNameField->type = 'FieldValue';
                                  
                              // Email
                              $emailField = new FieldValue();
                              $emailField->id = 429;
                              $emailField->value = $_POST['EmailAddress'];
                              $emailField->type = 'FieldValue';
                                  
                              // Phone
                              $phoneField = new FieldValue();
                              $phoneField->id = 430;
                              $phoneField->value = $_POST['PhoneNumber'];
                              $phoneField->type = 'FieldValue';
                                  
                              // Company/Institution
                              $companyField = new FieldValue();
                              $companyField->id = 431;
                              $companyField->value = $_POST['Institution'];
                              $companyField->type = 'FieldValue';
                                  
                              // Comments
                              $commentsField = new FieldValue();
                              $commentsField->id = 432;
                              $commentsField->value = $_POST['ContactComment'];
                              $commentsField->type = 'FieldValue';
                                  
                              // add the fieldValues to a collection
                              $fieldValues = array($firstNameField, $lastNameField, $emailField, $phoneField, $companyField, $commentsField);
                                  
                              // populate the form data submission request
                              $submitData = new formData();
                              $submitData->fieldValues = $fieldValues;
                              $submitData->type = 'FormData';
                              // invoke an HTTP Post request to submit the form data
                              $response = $request->post('data/form/45', $submitData);
                              
                              
                              

                               

                              When I run this information through the Live Docs, it seems to work fine.

                              Capture.PNG

                              Any suggestions on what is wrong with this submission? Nothing appears in Eloqua after I post this data.

                              • 14. Re: Form Submission Via REST API Using PHP Request Library
                                erickazda

                                PagingFred Sakr . Paging anyone else!

                                 

                                I could really use some help with the comment I placed at the bottom of this thread.

                                1 2 Previous Next