views:

333

answers:

1

Hi guys, I have to create a PHP SOAP client that sends leads, but I have never worked with SOAP before, and my XML isn't that great, here is the code I have managed to write so far. . .

<?php

    try {

        $client = new SoapClient(null, array(
        'location'  =>  "https://wwa.website.co.za/CallmasterTes/LeadService.asmx",
        'uri'       =>  "urn:Website:Callmaster:InTuch/CreateLead",
        'login'     =>  "username",
        'password'  =>  "password"
        ));

            $sysName = ' ';
            $clientCode = ' ';
            $expTimestamp = ' ';
            $schedTimestamp = ' ';

        $client->CreateLead("ExternalLead",
                        new SoapParam($sysName, "BusinessSystemName"),
                        new SoapParam($clientCode, "BusinessSystemClientCode"),
                        new SoapParam($_POST['Title'], "Title"),
                        new SoapParam($_POST['FirstName'], "FirstName"),
                        new SoapParam($_POST['Surname'], "Surname"),
                        new SoapParam($_POST['IdNumber'], "IdNumber"),
                        new SoapParam($_POST['Gender'], "Gender"),
                        new SoapParam($_POST['DateOfBirth'], "DateOfBirth"),
                        new SoapParam($_POST['Language'], "Language"),
                        new SoapParam($_POST['EmailAddress'], "EmailAddress"),
                        new SoapParam($_POST['HomeTelNumber'], "HomeTelNumber"),
                        new SoapParam($_POST['BusinessTelNumber'], "BusinessTelNumber"),
                        new SoapParam($_POST['MobileTelNumber'], "MobileTelNumber"),
                        new SoapParam($_POST['OtherTelNumber'], "OtherTelNumber"),
                        new SoapParam($_POST['PreferredTelNumberCode'], "PreferredTelNumberCode"),
                        new SoapParam($_POST['CampaignName'], "CampaignName"),
                        new SoapParam($_POST['ProductName'], "ProductName"),
                        new SoapParam($_POST['Comments'], "Comments"),
                                            new SoapParam($expTimestamp, "ExpiryTimestamp"),
                                            new SoapParam($schedTimestamp, "ScheduledTimestamp"),
                        );

    }

    catch (SoapFault $fault) {
        trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
    }

?>

The following is a sample SOAP 1.2 request and response that I was given by the developers on the server side of things.

POST /CallmasterTest/LeadService.asmx HTTP/1.1
Host: wwa.website.co.za
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"&gt;
  <soap12:Body>
    <CreateLead xmlns="urn:Website:Callmaster:InTuch">
      <Lead>
        <BusinessSystemName>string</BusinessSystemName>
        <BusinessSystemClientCode>int</BusinessSystemClientCode>
        <Title>Mr or Ms or Mrs or Miss...etc</Title>
        <FirstName>string</FirstName>
        <Surname>string</Surname>
        <IdNumber>string</IdNumber>
        <Gender>Male or Female</Gender>
        <DateOfBirth>date</DateOfBirth>
        <Language>string</Language>
        <EmailAddress>string</EmailAddress>
        <HomeTelNumber>string</HomeTelNumber>
        <BusinessTelNumber>string</BusinessTelNumber>
        <MobileTelNumber>string</MobileTelNumber>
        <OtherTelNumber>string</OtherTelNumber>
        <PreferredTelNumberCode>Business or Home or Mobile or Other</PreferredTelNumberCode>
        <CampaignName>string</CampaignName>
        <ProductName>string</ProductName>
        <Comments>string</Comments>
        <ExpiryTimestamp>dateTime</ExpiryTimestamp>
        <ScheduledTimestamp>dateTime</ScheduledTimestamp>
      </Lead>
    </CreateLead>
  </soap12:Body>
</soap12:Envelope>

The response:

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"&gt;
  <soap12:Body>
    <CreateLeadResponse xmlns="urn:Website:Callmaster:InTuch">
      <CreateLeadResult>
        <Success>boolean</Success>
        <StatusMessage>string</StatusMessage>
        <LeadGuid>guid</LeadGuid>
        <Errors>
          <LeadError ErrorLevel="Information or Warning or Error">
            <Message>string</Message>
            <Field>string</Field>
          </LeadError>
          <LeadError ErrorLevel="Information or Warning or Error">
            <Message>string</Message>
            <Field>string</Field>
          </LeadError>
        </Errors>
      </CreateLeadResult>
    </CreateLeadResponse>
  </soap12:Body>
</soap12:Envelope>

I have really tried everything, and googled into high heaven, but I just can't get it to work, I am sure that it's probably something small that I am missing, I would really appreciate the help, thanx!

+1  A: 

I'm not sure if this is a copy/paste error or not, but all the _POSTs should be $_POSTs in your first code example.

emmychan
Whoops, only saw that now, it must be a C/P error. Thanx for pointing it out.
Also, it looks like `'location' => "https://wwa.website.co.za/CallmasterTes/LeadService.asmx"` should be `'location' => "https://wwa.website.co.za/CallmasterTest/LeadService.asmx"`
emmychan
I'll have a look at that, but does the code look like it could work?
because I don't know if I'm off by a inch or a mile.
On the surface, it looks okay. But problems could be from a number of things - working with SOAP can be a real PITA - and there's a lot that depends on the soap server you're interfacing with. You may need to use `$client->__soapCall('CreateLead', $params)` instead of `$client->CreateLead` since you're in non-wsdl mode. It may be a typing issue - some xml data types can be tricky to work with. You've never said what exactly isn't working - are you getting a soapfault?
emmychan
Sorry for the massively delayed reply! Cool, I'll play around with that for a bit and see what I can come up with, and yes, I am getting a fault, I'll post it here in a bit! Thanks!