如何通过PHP中的SOAP将XML数据发送到API?

时间:2014-05-02 10:04:21

标签: php xml web-services soap wsdl

我无法将SOAP请求中的数据发送到PHP中的API。

安全信息

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

数据

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

肥皂请求

$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  );

现在,无论我在实际的Submit函数中添加什么,我都会通过print_r($ result)得到以下错误:

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

API文件位于: http://www.reefservices.co.uk/leadws/lead.asmx?WSDL

我已经尝试过发送XML数据(因为这是必需的),字符串,数组,对象,所有内容,它只是向我抛出错误,没有提供数据。

我在这里有API规范,它说明了许多所需的XML字段,这可能是它失败的原因吗?因为我还没有发送所有数据?

编辑:API规范:Here

找到最终解决方案Here,按预期工作!

2 个答案:

答案 0 :(得分:3)

使用SOAP API时,我发现的最佳工具之一是SoapUI。将允许您快速查看发送和接收的内容。

答案 1 :(得分:0)

使用您的有效凭据尝试此操作:

<?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>';
}

&GT;

还在文档中留下了一些关于参数的评论。