Advertisement
djtwigg

PHP SOAP Client to API

May 2nd, 2014
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.79 KB | None | 0 0
  1. $URL = 'http://www.reefservices.co.uk/leadws/lead.asmx';
  2.  
  3.     function createRequestXML($params = array()) {
  4.         $DealerCode = (array_key_exists('DealerCode',$params)) ? $params['DealerCode'] : 123456; // required
  5.         $LeadType = (array_key_exists('LeadType',$params)) ? $params['LeadType'] : 'Used Car Enquiry'; // required
  6.         $LeadSource = (array_key_exists('LeadSource',$params)) ? $params['LeadSource'] : 'Website'; // required
  7.         $LeadId = (array_key_exists('LeadId',$params)) ? $params['LeadId'] : '123456'; // not required
  8.         $CustomerType = (array_key_exists('CustomerType',$params)) ? $params['CustomerType'] : 1; // required. can be : 0 for Company, 1 for Individual
  9.         $Title = (array_key_exists('Title',$params)) ? $params['Title'] : 'Master'; // not required
  10.         $Forename = (array_key_exists('Forename',$params)) ? $params['Forename'] : 'Test'; // required
  11.         $Lastname = (array_key_exists('Lastname',$params)) ? $params['Lastname'] : 'Name'; // required
  12.         $Companyname = (array_key_exists('Companyname',$params)) ? $params['Companyname'] : 'Company'; // not required
  13.  
  14.         /* One of the following fields is required to have a value */
  15.         $Homephone = (array_key_exists('Homephone',$params)) ? $params['Homephone'] : '0123456789';
  16.         $Workphone = (array_key_exists('Workphone',$params)) ? $params['Workphone'] : '0123456789';
  17.         $Mobilephone = (array_key_exists('Mobilephone',$params)) ? $params['Mobilephone'] : '0123456789';
  18.         $Email = (array_key_exists('Email',$params)) ? $params['Email'] : 'test@test.co.uk';
  19.         /* One of the above fields is required to have a value */
  20.  
  21.         $Address1 = (array_key_exists('Address1',$params)) ? $params['Address1'] : 'AAAAAA'; // not required
  22.         $Address2 = (array_key_exists('Address2',$params)) ? $params['Address2'] : 'BBBBBB'; // not required
  23.         $City = (array_key_exists('City',$params)) ? $params['City'] : 'CCCCCCC'; // not required
  24.         $County = (array_key_exists('County',$params)) ? $params['County'] : 'DDDDDDDD'; // not required
  25.         $Postcode = (array_key_exists('Postcode',$params)) ? $params['Postcode'] : 'EEEEE'; // not required
  26.  
  27.         $dpapostal = (array_key_exists('dpapostal',$params)) ? $params['dpapostal'] : 'Y'; // required. can be Y/N
  28.         $dpasms = (array_key_exists('dpasms',$params)) ? $params['dpasms'] : 'Y'; // required. can be Y/N
  29.         $dpstel = (array_key_exists('dpstel',$params)) ? $params['dpstel'] : 'Y'; // required. can be Y/N
  30.         $dpaemail = (array_key_exists('dpaemail',$params)) ? $params['dpaemail'] : 'Y'; // required. can be Y/N
  31.  
  32.         $Methodofcontact = (array_key_exists('Methodofcontact',$params)) ? $params['Methodofcontact'] : 'Internet'; // not required
  33.         $Sourceofenquiry = (array_key_exists('Sourceofenquiry',$params)) ? $params['Sourceofenquiry'] : 'Autotrader'; // not required
  34.  
  35.         $Message = (array_key_exists('Message',$params)) ? $params['Message'] : 'I would like to buy a car please.'; // not required
  36.  
  37.         $Regno = (array_key_exists('Regno',$params)) ? $params['Regno'] : 'R33FBS'; // not required
  38.         $Newused = (array_key_exists('Newused',$params)) ? $params['Newused'] : 'USED'; // not required. can be "NEW"/"USED"
  39.         $Make = (array_key_exists('Make',$params)) ? $params['Make'] : 'BMW'; // not required
  40.         $Model = (array_key_exists('Model',$params)) ? $params['Model'] : '3 Series'; // not required
  41.         $Derivative = (array_key_exists('Derivative',$params)) ? $params['Derivative'] : '330D Coupe'; // not required
  42.         $Fuel = (array_key_exists('Fuel',$params)) ? $params['Fuel'] : 'Diesel'; // not required
  43.         $Colour = (array_key_exists('Colour',$params)) ? $params['Colour'] : 'Silver'; // not required
  44.  
  45.         $PartEx_Regno = (array_key_exists('PartEx_Regno',$params)) ? $params['PartEx_Regno'] : 'N476AKV'; // not required
  46.         $PartEx_Mileage = (array_key_exists('PartEx_Mileage',$params)) ? $params['PartEx_Mileage'] : '45600'; // not required
  47.  
  48.         $xml = '    <?xml version="1.0" encoding="UTF-8" ?>
  49.                     <SecurityInfo>
  50.                         <Username>'.REEF_USERNAME.'</Username>
  51.                         <Password>'.REEF_PASSWORD.'</Password>
  52.                     </SecurityInfo>
  53.                     <Lead>
  54.                         <DealerCode>'.$DealerCode.'</DealerCode>
  55.                         <LeadType>'.$LeadType.'</LeadType>
  56.                         <LeadSource>'.$LeadSource.'</LeadSource>
  57.                         <LeadId>'.$LeadId.'</LeadId>
  58.                         <Customer>
  59.                             <CustomerType>'.$CustomerType.'</CustomerType>
  60.                             <Title>'.$Title.'</Title>
  61.                             <Forename>'.$Forename.'</Forename>
  62.                             <Lastname>'.$Lastname.'</Lastname>
  63.                             <Companyname>'.$Companyname.'</Companyname>
  64.                             <Homephone>'.$Homephone.'</Homephone>
  65.                             <Workphone>'.$Workphone.'</Workphone>
  66.                             <Mobilephone>'.$Mobilephone.'</Mobilephone>
  67.                             <Email>'.$Email.'</Email>
  68.                             <Address>
  69.                                 <Address1>'.$Address1.'</Address1>
  70.                                 <Address2>'.$Address2.'</Address2>
  71.                                 <City>'.$City.'</City>
  72.                                 <County>'.$County.'</County>
  73.                                 <Postcode>'.$Postcode.'</Postcode>
  74.                             </Address>
  75.                             <Dpa>
  76.                                 <dpapostal>'.$dpapostal.'</dpapostal>
  77.                                 <dpasms>'.$dpasms.'</dpasms>
  78.                                 <dpstel>'.$dpstel.'</dpstel>
  79.                                 <dpaemail>'.$dpaemail.'</dpaemail>
  80.                             </Dpa>
  81.                             <Marketing>
  82.                                 <Methodofcontact>'.$Methodofcontact.'</Methodofcontact>
  83.                                 <Sourceofenquiry>'.$Sourceofenquiry.'</Sourceofenquiry>
  84.                             </Marketing>
  85.                             <Message>'.$Message.'</Message>
  86.                         </Customer>
  87.                         <Vehicle>
  88.                             <Regno>'.$Regno.'</Regno>
  89.                             <Newused>'.$Newused.'</Newused>
  90.                             <Make>'.$Make.'</Make>
  91.                             <Model>'.$Model.'</Model>
  92.                             <Derivative>'.$Derivative.'</Derivative>
  93.                             <Fuel>'.$Fuel.'</Fuel>
  94.                             <Colour>'.$Colour.'</Colour>
  95.                         </Vehicle>
  96.                         <PartEx>
  97.                             <Regno>'.$PartEx_Regno.'</Regno>
  98.                             <Mileage>'.$PartEx_Mileage.'</Mileage>
  99.                         </PartEx>
  100.                     </Lead>';
  101.  
  102.         return str_replace("\n","",$xml);
  103.     }
  104.  
  105.     $send = createRequestXML(array(
  106.         'DealerCode' => 123456,
  107.         'Title' => 'Mr',
  108.         'Companyname' => 'Reef',
  109.         'Email' => 'test@test.com',
  110.         'Methodofcontact' => 'Internet',
  111.         'Sourceofenquiry' => 'Stackoverflow',
  112.         'Message' => 'I would like to buy a car',
  113.         'Newused' => 'USED',
  114.         'Make' => 'BMW',
  115.         'Model' => '3 Series',
  116.         'Derivative' => '330D Coupe',
  117.         'Fuel' => 'Diesel',
  118.         'Colour' => 'Silver',
  119.         'PartEx_Mileage' => '45600'
  120.     ));
  121.  
  122. try {
  123.         $client = new SoapClient("http://www.reefservices.co.uk/leadws/lead.asmx?WSDL",array(
  124.             'location'      => $URL,
  125.             'uri'           => "http://www.reefservices.co.uk/leadws/",
  126.             'trace'         => 1,
  127.             'exceptions'    => true
  128.         ));
  129.  
  130.         $return = $client->Submit(array('Data' => $send));
  131.  
  132. $the_xml = str_replace("\n",'',rtrim($return->SubmitResult,','));
  133.  
  134.         preg_match_all("/<SUMMARY[^>]*>(.*?)<\/SUMMARY>/si", $the_xml, $summary, PREG_PATTERN_ORDER);
  135.         preg_match_all("/<LEADID[^>]*>(.*?)<\/LEADID>/si", $the_xml, $lead, PREG_PATTERN_ORDER);
  136.         preg_match_all("/<REEFID[^>]*>(.*?)<\/REEFID>/si", $the_xml, $ref, PREG_PATTERN_ORDER);
  137.        
  138.         $entries = array();
  139.         if(count($lead) == count($ref)) {
  140.             foreach($lead[1] AS $key => $value) {
  141.                 $entries[$key] = array(
  142.                     'lead' => trim($value),
  143.                     'ref' => trim($ref[1][$key])
  144.                 );
  145.             }
  146.         }
  147.  
  148.         $final = array(
  149.             'summary' => $summary[1],
  150.             'leads' => $entries
  151.         );
  152.  
  153.         echo '<pre>';
  154.         print_r($final);
  155.         echo '</pre>';
  156.     } catch(SoapFault $e) {
  157.         echo '<h3>Exception</h3>';
  158.         echo '<pre>';
  159.         print_r($e);
  160.         echo '</pre>';
  161.     }
  162. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement