Question

I am having trouble sending data in a SOAP request to an API in PHP.

Security Information

$security_info = array(
    "SecurityInfo" => array(
        'Username' => REEF_USERNAME,
        'Password' => REEF_PASSWORD
    )
);

Data

$data = array (
    'Lead' => array (
        'DealerCode' => 12345,
  ),
);

Soap Request

$URL = 'http://www.reefservices.co.uk/leadws/lead.asmx';

$client = new SoapClient("http://www.reefservices.co.uk/leadws/lead.asmx?WSDL" , array(
    'location' => $URL,
    'uri'      => "http://www.reefservices.co.uk/leadws/",
    'trace'    => 1,)
);

$return = $client->Submit(  $security_info  );

Now, no matter what I put in the actual Submit function, I get the following error by print_r($result):

object(stdClass)[4]
  public 'SubmitResult' => string 'No data is provided.' (length=20)

The API file is here: http://www.reefservices.co.uk/leadws/lead.asmx?WSDL

I have tried sending XML data (as that is what is required), strings, arrays, objects, everything, and it just throws error at me, no data is provided.

I have the API spec here and it states a lot of XML fields that are required, could this be why it fails? Because I have not sent ALL the data?

Edit: API Spec: Here

The final solution is found Here, working as intended!

Was it helpful?

Solution 2

Try this with your valid credentials :

<?php
define('REEF_USERNAME','YOUR_VALID_USERNAME');
define('REEF_PASSWORD','YOUR_VALID_PASSWORD');

$URL = 'http://www.reefservices.co.uk/leadws/lead.asmx';

function createRequestXML($params = array()) {
    $DealerCode = (array_key_exists('DealerCode',$params)) ? $params['DealerCode'] : 2603; // required
    $LeadType = (array_key_exists('LeadType',$params)) ? $params['LeadType'] : 'Used Car Enquiry'; // required
    $LeadSource = (array_key_exists('LeadSource',$params)) ? $params['LeadSource'] : 'Website'; // required
    $LeadId = (array_key_exists('LeadId',$params)) ? $params['LeadId'] : 'Null'; // not required
    $CustomerType = (array_key_exists('CustomerType',$params)) ? $params['CustomerType'] : 1; // required. can be : 0 for Company, 1 for Individual
    $Title = (array_key_exists('Title',$params)) ? $params['Title'] : 'Null'; // not required
    $Forename = (array_key_exists('Forename',$params)) ? $params['Forename'] : 'Ben'; // required
    $Lastname = (array_key_exists('Lastname',$params)) ? $params['Lastname'] : 'Thomas'; // required
    $Companyname = (array_key_exists('Companyname',$params)) ? $params['Companyname'] : 'Null'; // not required

    /* One of the following fields is required to have a value */
    $Homephone = (array_key_exists('Homephone',$params)) ? $params['Homephone'] : 'Null';
    $Workphone = (array_key_exists('Workphone',$params)) ? $params['Workphone'] : 'Null';
    $Mobilephone = (array_key_exists('Mobilephone',$params)) ? $params['Mobilephone'] : 'Null';
    $Email = (array_key_exists('Email',$params)) ? $params['Email'] : 'aa@test.com';
    /* One of the above fields is required to have a value */

    $Address1 = (array_key_exists('Address1',$params)) ? $params['Address1'] : 'Null'; // not required
    $Address2 = (array_key_exists('Address2',$params)) ? $params['Address2'] : 'Null'; // not required
    $City = (array_key_exists('City',$params)) ? $params['City'] : 'Null'; // not required
    $County = (array_key_exists('County',$params)) ? $params['County'] : 'Null'; // not required
    $Postcode = (array_key_exists('Postcode',$params)) ? $params['Postcode'] : 'Null'; // not required

    $dpapostal = (array_key_exists('dpapostal',$params)) ? $params['dpapostal'] : 'Y'; // required. can be Y/N
    $dpasms = (array_key_exists('dpasms',$params)) ? $params['dpasms'] : 'Y'; // required. can be Y/N
    $dpstel = (array_key_exists('dpstel',$params)) ? $params['dpstel'] : 'Y'; // required. can be Y/N
    $dpaemail = (array_key_exists('dpaemail',$params)) ? $params['dpaemail'] : 'Y'; // required. can be Y/N

    $Methodofcontact = (array_key_exists('Methodofcontact',$params)) ? $params['Methodofcontact'] : 'Null'; // not required
    $Sourceofenquiry = (array_key_exists('Sourceofenquiry',$params)) ? $params['Sourceofenquiry'] : 'Null'; // not required

    $Message = (array_key_exists('Message',$params)) ? $params['Message'] : 'Null'; // not required

    $Regno = (array_key_exists('Regno',$params)) ? $params['Regno'] : 'Null'; // not required
    $Newused = (array_key_exists('Newused',$params)) ? $params['Newused'] : 'Null'; // not required. can be "NEW"/"USED"
    $Make = (array_key_exists('Make',$params)) ? $params['Make'] : 'Null'; // not required
    $Model = (array_key_exists('Model',$params)) ? $params['Model'] : 'Null'; // not required
    $Derivative = (array_key_exists('Derivative',$params)) ? $params['Derivative'] : 'Null'; // not required
    $Fuel = (array_key_exists('Fuel',$params)) ? $params['Fuel'] : 'Null'; // not required
    $Colour = (array_key_exists('Colour',$params)) ? $params['Colour'] : 'Null'; // not required

    $PartEx_Regno = (array_key_exists('PartEx_Regno',$params)) ? $params['PartEx_Regno'] : 'Null'; // not required
    $PartEx_Mileage = (array_key_exists('PartEx_Mileage',$params)) ? $params['PartEx_Mileage'] : 'Null'; // not required

    $xml = '    <?xml version="1.0" encoding="UTF-8" ?>
                <SecurityInfo>
                    <Username>'.REEF_USERNAME.'</Username>
                    <Password>'.REEF_PASSWORD.'</Password>
                </SecurityInfo>
                <Lead>
                    <DealerCode>'.$DealerCode.'</DealerCode>
                    <LeadType>'.$LeadType.'</LeadType>
                    <LeadSource>'.$LeadSource.'</LeadSource>
                    <LeadId>'.$LeadId.'</LeadId>
                    <Customer>
                        <CustomerType>'.$CustomerType.'</CustomerType>
                        <Title>'.$Title.'</Title>
                        <Forename>'.$Forename.'</Forename>
                        <Lastname>'.$Lastname.'</Lastname>
                        <Companyname>'.$Companyname.'</Companyname>
                        <Homephone>'.$Homephone.'</Homephone>
                        <Workphone>'.$Workphone.'</Workphone>
                        <Mobilephone>'.$Mobilephone.'</Mobilephone>
                        <Email>'.$Email.'</Email>
                        <Address>
                            <Address1>'.$Address1.'</Address1>
                            <Address2>'.$Address2.'</Address2>
                            <City>'.$City.'</City>
                            <County>'.$County.'</County>
                            <Postcode>'.$Postcode.'</Postcode>
                        </Address>
                        <Dpa>
                            <dpapostal>'.$dpapostal.'</dpapostal>
                            <dpasms>'.$dpasms.'</dpasms>
                            <dpstel>'.$dpstel.'</dpstel>
                            <dpaemail>'.$dpaemail.'</dpaemail>
                        </Dpa>
                        <Marketing>
                            <Methodofcontact>'.$Methodofcontact.'</Methodofcontact>
                            <Sourceofenquiry>'.$Sourceofenquiry.'</Sourceofenquiry>
                        </Marketing>
                        <Message>'.$Message.'</Message>
                    </Customer>
                    <Vehicle>
                        <Regno>'.$Regno.'</Regno>
                        <Newused>'.$Newused.'</Newused>
                        <Make>'.$Make.'</Make>
                        <Model>'.$Model.'</Model>
                        <Derivative>'.$Derivative.'</Derivative>
                        <Fuel>'.$Fuel.'</Fuel>
                        <Colour>'.$Colour.'</Colour>
                    </Vehicle>
                    <PartEx>
                        <Regno>'.$PartEx_Regno.'</Regno>
                        <Mileage>'.$PartEx_Mileage.'</Mileage>
                    </PartEx>
                </Lead>';

    return str_replace("\n","",$xml);
}

$send = createRequestXML(array(
    'Title' => 'Mr',
    'Companyname' => 'Reef',
    'Email' => 'test@test.com',
    'Methodofcontact' => 'Internet',
    'Sourceofenquiry' => 'Stackoverflow',
    'Message' => 'I would like to buy a car',
    'Newused' => 'USED',
    'Make' => 'BMW',
    'Model' => '3 Series',
    'Derivative' => '330D Coupe',
    'Fuel' => 'Diesel',
    'Colour' => 'Silver',
    'PartEx_Mileage' => '45600'
));

try {
    $client = new SoapClient("http://www.reefservices.co.uk/leadws/lead.asmx?WSDL",array(
        'location'      => $URL,
        'uri'           => "http://www.reefservices.co.uk/leadws/",
        'trace'         => 1,
        'exceptions'    => true
    ));

    $return = $client->Submit(array('Data' => $send));

    echo '<pre>';
    print_r($return);
    echo '</pre>';
} catch(SoapFault $e) {
    echo '<h3>Exception</h3>';
    echo '<pre>';
    print_r($e);
    echo '</pre>';
}

?>

Also left some comments regarding the parameters as they were in the docs.

OTHER TIPS

One of the best tools I've found when using a SOAP API is SoapUI. Will allow you to quickly see what was sent and received.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top