在php中集成SOAP API

时间:2015-03-31 19:16:44

标签: php xml soap

我正在尝试将用于汽车注册在线查找功能的SOAP / HTTP POST / XML API集成到我的网站中,我还没有想到如何在php文件中插入此脚本,一直都会出错。

脚本



SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /api/reg.asmx HTTP/1.1
Host: www.regcheck.org.uk
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Check"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Check xmlns="http://tempuri.org/">
      <RegistrationNumber>string</RegistrationNumber>
      <username>string</username>
    </Check>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CheckResponse xmlns="http://tempuri.org/">
      <CheckResult>
        <vehicleJson>string</vehicleJson>
        <vehicleXml>string</vehicleXml>
        <numberOfSeats>string</numberOfSeats>
        <carValue>string</carValue>
        <immobiliser>string</immobiliser>
        <vehicleData>
          <ABICode type="NCName" />
          <Description type="NCName" />
          <RegistrationYear type="NCName" />
          <ManufactureYearFrom type="NCName" />
          <ManufactureYearTo type="NCName" />
          <CarMake type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </CarMake>
          <CarModel type="NCName" />
          <BodyStyle type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </BodyStyle>
          <EngineSize type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </EngineSize>
          <NumberOfDoors type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </NumberOfDoors>
          <Transmission type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </Transmission>
          <FuelType type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </FuelType>
          <MakeDescription type="NCName" />
          <ModelDescription type="NCName" />
          <Immobiliser type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </Immobiliser>
          <NumberOfSeats type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </NumberOfSeats>
          <IndicativeValue type="NCName" />
          <DriverSide type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </DriverSide>
        </vehicleData>
      </CheckResult>
    </CheckResponse>
  </soap:Body>
</soap:Envelope>
SOAP 1.2

The following is a sample SOAP 1.2 request and response. The placeholders shown need to be replaced with actual values.

POST /api/reg.asmx HTTP/1.1
Host: www.regcheck.org.uk
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">
  <soap12:Body>
    <Check xmlns="http://tempuri.org/">
      <RegistrationNumber>string</RegistrationNumber>
      <username>string</username>
    </Check>
  </soap12:Body>
</soap12:Envelope>
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">
  <soap12:Body>
    <CheckResponse xmlns="http://tempuri.org/">
      <CheckResult>
        <vehicleJson>string</vehicleJson>
        <vehicleXml>string</vehicleXml>
        <numberOfSeats>string</numberOfSeats>
        <carValue>string</carValue>
        <immobiliser>string</immobiliser>
        <vehicleData>
          <ABICode type="NCName" />
          <Description type="NCName" />
          <RegistrationYear type="NCName" />
          <ManufactureYearFrom type="NCName" />
          <ManufactureYearTo type="NCName" />
          <CarMake type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </CarMake>
          <CarModel type="NCName" />
          <BodyStyle type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </BodyStyle>
          <EngineSize type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </EngineSize>
          <NumberOfDoors type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </NumberOfDoors>
          <Transmission type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </Transmission>
          <FuelType type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </FuelType>
          <MakeDescription type="NCName" />
          <ModelDescription type="NCName" />
          <Immobiliser type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </Immobiliser>
          <NumberOfSeats type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </NumberOfSeats>
          <IndicativeValue type="NCName" />
          <DriverSide type="NCName">
            <CurrentValue xsi:nil="true" />
            <CurrentTextValue xsi:nil="true" />
          </DriverSide>
        </vehicleData>
      </CheckResult>
    </CheckResponse>
  </soap12:Body>
</soap12:Envelope>
HTTP GET

The following is a sample HTTP GET request and response. The placeholders shown need to be replaced with actual values.

GET /api/reg.asmx/Check?RegistrationNumber=string&username=string HTTP/1.1
Host: www.regcheck.org.uk
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<Vehicle xmlns="http://tempuri.org/">
  <vehicleJson>string</vehicleJson>
  <vehicleXml>string</vehicleXml>
  <numberOfSeats>string</numberOfSeats>
  <carValue>string</carValue>
  <immobiliser>string</immobiliser>
  <vehicleData>
    <ABICode type="NCName" />
    <Description type="NCName" />
    <RegistrationYear type="NCName" />
    <ManufactureYearFrom type="NCName" />
    <ManufactureYearTo type="NCName" />
    <CarMake type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </CarMake>
    <CarModel type="NCName" />
    <BodyStyle type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </BodyStyle>
    <EngineSize type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </EngineSize>
    <NumberOfDoors type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </NumberOfDoors>
    <Transmission type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </Transmission>
    <FuelType type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </FuelType>
    <MakeDescription type="NCName" />
    <ModelDescription type="NCName" />
    <Immobiliser type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </Immobiliser>
    <NumberOfSeats type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </NumberOfSeats>
    <IndicativeValue type="NCName" />
    <DriverSide type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </DriverSide>
  </vehicleData>
</Vehicle>
HTTP POST

The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.

POST /api/reg.asmx/Check HTTP/1.1
Host: www.regcheck.org.uk
Content-Type: application/x-www-form-urlencoded
Content-Length: length

RegistrationNumber=string&username=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<Vehicle xmlns="http://tempuri.org/">
  <vehicleJson>string</vehicleJson>
  <vehicleXml>string</vehicleXml>
  <numberOfSeats>string</numberOfSeats>
  <carValue>string</carValue>
  <immobiliser>string</immobiliser>
  <vehicleData>
    <ABICode type="NCName" />
    <Description type="NCName" />
    <RegistrationYear type="NCName" />
    <ManufactureYearFrom type="NCName" />
    <ManufactureYearTo type="NCName" />
    <CarMake type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </CarMake>
    <CarModel type="NCName" />
    <BodyStyle type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </BodyStyle>
    <EngineSize type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </EngineSize>
    <NumberOfDoors type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </NumberOfDoors>
    <Transmission type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </Transmission>
    <FuelType type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </FuelType>
    <MakeDescription type="NCName" />
    <ModelDescription type="NCName" />
    <Immobiliser type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </Immobiliser>
    <NumberOfSeats type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </NumberOfSeats>
    <IndicativeValue type="NCName" />
    <DriverSide type="NCName">
      <CurrentValue type="NCName" />
      <CurrentTextValue type="NCName" />
    </DriverSide>
  </vehicleData>
</Vehicle>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

我认为你应该做一些研究。有两种方法可以解决这个问题。

1)您可以使用CURL并模仿这些HTTP请求,并在构建标题后简单地发送字符串

2)您可以使用已在PHP 5.0.1及更高版本(http://php.net/manual/en/class.soapclient.php)中实现的SOAPClient。您正在集成的网站必须具有WSDL文件,该文件允许您在面向对象的事物中将其与SOAPClient一起使用。

如果您研究上述主题,您应该知道如何做到这一点。

答案 1 :(得分:0)

我使用网站“wsdltophp.com”为此网络服务生成代理类“www.regcheck.org.uk

https://www.wsdltophp.com/Wsdls/8a71627601a684624c5694104cce023f/RegCheck-1-0-1-1-1-1-0-0-0-0

您可以在此处下载所有文件; https://app.box.com/s/tepsfd6t0cahfdi9ivgnj305qocxrrt6

或者,您可以使用HTTP GET请求,可以通过HTTP GET请求调用该Web服务,如下所示;

<?php
$username = 'Your username here';
$regNumber = 'SK08KPT';
$xmlData = file_get_contents("https://www.regcheck.org.uk/api/reg.asmx/Check?RegistrationNumber=" . $regNumber  ."&username=" . $username);
$xml=simplexml_load_string($xmlData);    
$strJson = $xml->vehicleJson;    
$json = json_decode($strJson);    
print_r($json->Description);    
?>

不像SOAP调用那么优雅,但更简单。