Page 1 of 1

Topic: Need to POST xml string to 3rd party api, using gravi

Posted: Mon Mar 03, 2014 2:11 pm
by aaugust7126
[text]I need have a gform submission POST to third party api as xmldata.

Url that data needs to POST to:

https://www.3rdparty.com/api/lead/inser ... ourxmldata

XML Format Needed (being pulled from gravity form below):[/text]

Code: Select all

	 <crcloud>
           <lead>
            <type>Client</type>
            <firstname>Scott</firstname>
            <lastname>James</lastname>
            <client_assigned_to>Bill Smith</client_assigned_to>
           </lead>
          </crcloud>
[text]I have a curl function written but I am receiving a parsing error when posting. I have validated the xml and it is correct. I can also paste the xml data that prints out directly behind my url and get the result I want.[/text]

Code: Select all

 <?php
  
  add_action("gform_after_submission", "set_post_content", 10, 2);
  function set_post_content($entry, $form){
  
   
      function post_to_url($url, $post) {
          
          $fields = '';
         foreach($post as $key => $value) {
             $fields .= $key . '=' . $value . '&';
         }
         rtrim($fields, '&');
  
         $url = 'https://www.3rdparty.com/api/lead/insertRecord?apiauthkey=yourapikey&secretkey=yoursecretkey&xmlData=yourxmldata'.$xml;
  
 
         $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_SSL_VERIFYHOST, 0);
         curl_setopt($post, CURLOPT_SSL_VERIFYPEER, 0);
         curl_setopt($post, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
         curl_setopt($post, CURLOPT_VERBOSE, true);
  
         $verbose = fopen('php://temp', 'rw+');
         curl_setopt($post, CURLOPT_STDERR, $verbose);
  
         $result = curl_exec($post);
         echo $post; //Resource id #*** currently
         echo $result; //False 4404 XML parsing error currently
  
         rewind($verbose);
         $verboseLog = stream_get_contents($verbose);
         echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog), "</pre>\n";
  
         curl_close($post);
     }
 
     if($form["id"] == 2){//sign up now
 
         $data = array(
             "type" =>     $entry["31"],      
 

	

    "firstname" =>     $entry["3"], 
             "lastname" =>     $entry["4"], 
             "client_assigned_to" =>     $entry["34"]); 
 
  
         $xml = new SimpleXMLElement('<crcloud/>');
         $lead = $xml->addchild('lead');
         $data = array_flip($data);
         array_walk_recursive($data, array ($lead, 'addChild'));
  
         print $xml->asXML(); 
 
 
         post_to_url($url, $xml); 
  
     }
  
 }
  
 ?>