Sending xml to webservices using cURL

To give you an overview of what I’m trying to accomplish here, I am running a WordPress site with a custom theme and utilizing the Gravity Forms plugin to create all forms on the site.

We are integrating all (2) forms on the site with an external lead management service. Of course their documentation on how to interact with their webservice is far from well documented.

Read More

The provided PDF states:

It is assumed that the third-party vendor is familiar with creating XML files, understands XSD documents in order to create well-formed XML documents, and has the appropriate tools necessary to submit and receive XML documents via a webservice.

Webservice URL:

https://interface.webservices.popcard.ltsolutions.com/service.asmx

Webservice Method

InsertTraffic

InsertTraffic is a method a third-party vendor will use to insert a single piece of traffic into Yardi PopCard’ PopCard application. This single piece of traffic could represent a prospect filling out a contact form on an ILS website or a phone call received from a prospect which was answered by a call center. – Method description from PDF

I have been in contact with the company and know that the XML that I’m trying to push to their webservice is correct, but the leads are not getting into the system.

<?php 
add_action("gform_after_submission", "submit_contact_lead", 10, 2);

function submit_contact_lead($entry, $form){

  $fname = $entry['1.3'];
  $lname = $entry['1.6'];
  $userEmail = $entry['2'];
  if ($entry['3']) {
    $comments = $entry['3'];
  } else {
    $comments = '';
  }
  $date = date('Y-m-dTH:i');
  $baseURL = 'http://interface.webservices.popcard.ltsolutions.com/service.asmx/InsertTraffic';
  $xmlRequest = '<?xml version="1.0" encoding="utf-8"?>
    <traffic 
        xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        contactdatetime="'.$date.'" 
        transactiondatetime="'.$date.'">
    <trafficsource>
        <vendorid>551d12d8a1de</vendorid>
        <emailaddress></emailaddress>
        <sourcename></sourcename>
        <propertyname>CityView</propertyname>
    </trafficsource>
    <prospect>
        <firstname>'.$fname.'</firstname>
        <middlename></middlename>
        <lastname>'.$lname.'</lastname>
        <streetaddress1></streetaddress1>
        <streetaddress2></streetaddress2>
        <city></city>
        <state></state>
        <zipcode></zipcode>
        <daytimephone></daytimephone>
        <eveningphone></eveningphone>
        <cellphone></cellphone>
        <otherphone></otherphone>
        <emailaddress>'.$userEmail.'</emailaddress>
        <comments>'.$comments.'</comments>
      </prospect>
    <prospectpreferences>
        <pricerangemin></pricerangemin>
        <pricerangemax></pricerangemax>
        <numberofoccupants></numberofoccupants>
        <pets></pets>
        <dateneeded></dateneeded>
        <appointmentdate></appointmentdate>
        <appointmenttime></appointmenttime>
        <numberofbedsdesired></numberofbedsdesired>
        <numberofbathsdesired></numberofbathsdesired>
    </prospectpreferences>
    </traffic>';

    $xmlRequest = preg_replace( "/r|n/", "", $xmlRequest );

    // Set up cURL request directly in this funtion
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://interface.webservices.popcard.ltsolutions.com/service.asmx');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "InsertTraffic=" . $xmlRequest);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); // set to 300 after testing purposes
    curl_setopt($ch, CURLOPT_TIMEOUT, 0); // set to 300 after testing purposes
    $result = curl_exec($ch);
    curl_close($ch);

    // $array_data = json_decode(json_encode(simplexml_load_string($data)), true);

    error_log('PopCards Submission | Contact/Reserve Submission for '.$lname.', '. $fname.'.');
    error_log($xmlRequest);
    error_log($result);
}

As you can see at the end of the code I’m logging the lead and the result. The following is what I’m getting in my error_log file.

[10-Apr-2015 15:08:34 UTC] PopCards Submission | Contact/Reserve Submission for John, Doe.
[10-Apr-2015 15:08:34 UTC] 
<?xml version="1.0" encoding="utf-8"?>  
<traffic        
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"      
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
  contactdatetime="2015-04-10T15:08"        
  transactiondatetime="2015-04-10T15:08">   
    <trafficsource>     
      <vendorid>551d12d8a1de</vendorid>     
      <emailaddress></emailaddress>     
      <sourcename></sourcename>      
      <propertyname>CityView</propertyname> 
    </trafficsource>    
    <prospect>      
      <firstname>John</firstname>       
      <middlename></middlename>     
      <lastname>Doe</lastname>      
      <streetaddress1></streetaddress1>     
      <streetaddress2></streetaddress2>     
      <city></city>     
      <state></state>       
      <zipcode></zipcode>       
      <daytimephone></daytimephone>     
      <eveningphone></eveningphone>     
      <cellphone></cellphone>       
      <otherphone></otherphone>     
      <emailaddress>johndoe@gmail.com</emailaddress>        
      <comments></comments>   
    </prospect> 
    <prospectpreferences>       
      <pricerangemin></pricerangemin>       
      <pricerangemax></pricerangemax>       
      <numberofoccupants></numberofoccupants>       
      <pets></pets>     
      <dateneeded></dateneeded>     
      <appointmentdate></appointmentdate>       
      <appointmenttime></appointmenttime>       
      <numberofbedsdesired></numberofbedsdesired>       
      <numberofbathsdesired></numberofbathsdesired> 
    </prospectpreferences>  
  </traffic>


[10-Apr-2015 15:08:34 UTC] 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>500 - Internal server error.</h2>
  <h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
 </fieldset></div>
</div>
</body>
</html>

I need some assistance/guidance on what I’m doing wrong here.

Related posts

Leave a Reply

1 comment

  1. Ok, we will take this one step at a time.

    Your request is being rejected by the Serve the service is running on.

    We need to get your request header from curl.

    Leave the timeouts at 300, zero = never time out an you get no response and it hangs until (if) PHP times out.

    None of these option I want yu to add will affect you Request, they are only for test and debug.

    Change:

    curl_setopt($ch, CURLOPT_HEADER, true);
    

    Add these:

    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_FAILONERROR,true);
    

    Add this code:

      $result = curl_exec($ch);
      if (curl_errno($ch)){
          $data .= 'Retreive Base Page Error: ' . curl_error($ch);
      }
      else {
        $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
        $responseHeader = substr($result ,0,$skip);
        $result = substr($result ,$skip);
        $info = var_export(curl_getinfo($ch),true);
       }
      $fp = fopen('xml.log','w');
      fwrite($fp,"$responseHeadern$infon$result ");
      fclose($fp);
    

    Post this xml.log rather than your log. I think this will include what you have and more. Important stuff more.


    Update

    Change:

          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
    

    To:

    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/xml'),'Content-Length: ' . strlen($xmlRequest ));
    

    Why is this there? Maybe.

    contactdatetime="$date" transactiondatetime="$date"
    

    Heredoc format is better than concatenation "' . $date . '". too easy to miss a double quote or some other issue.

    Put the namespace (xmlns) all on one line, don’t need editor EOL issues.

     $xmlRequest = <<<EOX
    <?xml version="1.0" encoding="utf-8"?>
    <traffic xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" contactdatetime="$date" transactiondatetime="$date">
      <trafficsource>
        <vendorid>551d12d8a1de</vendorid>
        <emailaddress></emailaddress>
        <sourcename></sourcename>
        <propertyname>CityView</propertyname>
      </trafficsource>
      <prospect>
        <firstname>$fname</firstname>
        <middlename></middlename>
        <lastname>$lname</lastname>
        <streetaddress1></streetaddress1>
        <streetaddress2></streetaddress2>
        <city></city>
        <state></state>
        <zipcode></zipcode>
        <daytimephone></daytimephone>
        <eveningphone></eveningphone>
        <cellphone></cellphone>
        <otherphone></otherphone>
        <emailaddress>$userEmail</emailaddress>
        <comments>$comments</comments>
      </prospect>
      <prospectpreferences>
        <pricerangemin></pricerangemin>
        <pricerangemax></pricerangemax>
        <numberofoccupants></numberofoccupants>
        <pets></pets>
        <dateneeded></dateneeded>
        <appointmentdate></appointmentdate>
        <appointmenttime></appointmenttime>
        <numberofbedsdesired></numberofbedsdesired>
        <numberofbathsdesired></numberofbathsdesired>
      </prospectpreferences>
    </traffic>
    EOX;
    

    end update 1


    Update 2

    This is what I think your XML should look like:

    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
     <soap:Body>
      <traffic xmlns="http://tempuri.org/PopCardInterfaceWebservice/Service1">
        <trafficsource>     
          <vendorid>551d12d8a1de</vendorid>     
          <emailaddress></emailaddress>     
          <sourcename></sourcename>      
          <propertyname>CityView</propertyname> 
        </trafficsource>    
        <prospect>      
          <firstname>John</firstname>       
          <middlename></middlename>     
          <lastname>Doe</lastname>      
          <streetaddress1></streetaddress1>     
          <streetaddress2></streetaddress2>     
          <city></city>     
          <state></state>       
          <zipcode></zipcode>       
          <daytimephone></daytimephone>     
          <eveningphone></eveningphone>     
          <cellphone></cellphone>       
          <otherphone></otherphone>     
          <emailaddress>johndoe@gmail.com</emailaddress>        
          <comments></comments>   
        </prospect> 
        <prospectpreferences>       
          <pricerangemin></pricerangemin>       
          <pricerangemax></pricerangemax>       
          <numberofoccupants></numberofoccupants>       
          <pets></pets>     
          <dateneeded></dateneeded>     
          <appointmentdate></appointmentdate>       
          <appointmenttime></appointmenttime>       
          <numberofbedsdesired></numberofbedsdesired>       
          <numberofbathsdesired></numberofbathsdesired> 
        </prospectpreferences>  
      </traffic>
     </soap:Body>
    </soap:Envelope>