本文介绍了如何在 PHP 中通过 SOAP 将 XML 数据发送到 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

安全信息

$security_info = 数组(安全信息" =>大批('用户名' =>REEF_USERNAME,'密码' =>REEF_PASSWORD));

数据

$data = 数组 ('铅' =>大批 ('经销商代码' =>12345,),);

Soap 请求

$URL = 'http://www.reefservices.co.uk/leadws/lead.asmx';$client = new SoapClient("http://www.reefservices.co.uk/leadws/lead.asmx?WSDL" , array('位置' =>$网址,'uri' =>"http://www.reefservices.co.uk/leadws/",'跟踪' =>1、));$return = $client->Submit( $security_info );

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

object(stdClass)[4]公共'提交结果' =>字符串'没有提供数据.'(长度=20)

API 文件在这里:http://www.reefservices.co.uk/leadws/lead.asmx?WSDL

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

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

API 规范:此处

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

解决方案

尝试使用您的有效凭据:

'先生','公司名称' =>'礁','电子邮件' =>'[email protected]','联系方式' =>'互联网','Sourceofenquiry' =>'堆栈溢出','消息' =>'我想买车','Newused' =>'用过的','制作' =>'宝马','模型' =>'3系列','衍生' =>'330D双门轿车','燃料' =>'柴油机','颜色' =>'银','PartEx_Mileage' =>'45600'));尝试 {$client = new SoapClient("http://www.reefservices.co.uk/leadws/lead.asmx?WSDL",array('位置' =>$网址,'uri' =>"http://www.reefservices.co.uk/leadws/",'跟踪' =>1、'例外' =>真的));$return = $client->Submit(array('Data' => $send));echo '

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

Exception

';echo '

';打印_r($e);echo '</pre>';}

?>

还对文档中的参数留下了一些评论.

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!

解决方案

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'] : '[email protected]';
    /* 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' => '[email protected]',
    '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.

这篇关于如何在 PHP 中通过 SOAP 将 XML 数据发送到 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:18