You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中调用SOAP API并设置头部与函数参数

Hey there! Since you're used to REST APIs and new to SOAP, let's walk through exactly how to call that GetAvailibility function—including setting up the authentication header and request parameters properly. Let's start by breaking down the SOAP request structure you shared.

Understanding the SOAP Request Structure

First, let's unpack the XML you provided to know what we're working with:

  • SOAP Envelope: The root wrapper for all SOAP requests/responses.
  • SOAP Header: Contains authentication details (Authenticate node) with four required fields: InterfaceCode, InterfaceAuthKey, AgentCode, and Password. These are specific to the third-party's auth system.
  • SOAP Body: Holds the actual function call (GetAvailibility), which takes a single parameter strRequestXML (looks like this expects an XML string as input).

All elements under Authenticate and GetAvailibility use the namespace http://tempuri.org/—this is crucial, so don't omit it or mistype it.

How to Call the API (Practical Example with Python)

Since you're familiar with REST, using the requests library (which you probably already know) is a great way to dip your toes into SOAP. Here's a complete, working example:

import requests

# Replace this with the actual SOAP endpoint URL from the third-party docs
soap_endpoint = "https://third-party-service-url.com/YourService.asmx"

# Build the SOAP XML payload (replace placeholders with real values)
soap_payload = """<?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:Header>
    <Authenticate xmlns="http://tempuri.org/">
      <InterfaceCode>10</InterfaceCode>
      <InterfaceAuthKey>jskjks</InterfaceAuthKey>
      <AgentCode>ggsjs2222</AgentCode>
      <Password>abcd</Password>
    </Authenticate>
  </soap:Header>
  <soap:Body>
    <GetAvailibility xmlns="http://tempuri.org/">
      <!-- Use CDATA to wrap your actual request XML to avoid parsing errors -->
      <strRequestXML><![CDATA[<YourRequest><Date>2024-06-01</Date><Location>NYC</Location></YourRequest>]]></strRequestXML>
    </GetAvailibility>
  </soap:Body>
</soap:Envelope>"""

# Set required HTTP headers for SOAP
request_headers = {
    "Content-Type": "text/xml; charset=utf-8",
    # SOAPAction tells the server which method to invoke—must match the namespace + function name
    "SOAPAction": "http://tempuri.org/GetAvailibility"
}

# Send the POST request
response = requests.post(soap_endpoint, data=soap_payload, headers=request_headers)

# Handle the response
if response.status_code == 200:
    print("Success! Here's the SOAP response:")
    print(response.text)
else:
    print(f"Request failed with status code {response.status_code}:")
    print(response.text)

Key Notes to Avoid Common Pitfalls

  • SOAPAction Header: This is non-negotiable for most SOAP services. It tells the server exactly which function you want to call, formatted as [Namespace]/[FunctionName] (in this case, http://tempuri.org/GetAvailibility).
  • CDATA for strRequestXML: If your strRequestXML value contains XML tags (which it likely does), wrapping it in <![CDATA[...]]> prevents the SOAP parser from treating those tags as part of the main SOAP envelope. No need to manually escape <, >, or & this way.
  • Namespace Consistency: Make sure the xmlns="http://tempuri.org/" attribute is present on both the Authenticate and GetAvailibility nodes. Missing this will cause the server to reject your request.
  • Endpoint URL: Always use the exact SOAP endpoint provided by the third party—unlike REST, SOAP typically uses a single endpoint for all functions.

Quick Comparison to REST (To Contextualize)

If you're wondering how this differs from what you're used to:

  • SOAP uses XML-only payloads (no JSON support in most cases)
  • All requests go to a single endpoint (REST uses different endpoints for different resources)
  • Auth is often embedded in the SOAP Header instead of HTTP headers like Authorization
  • You need to explicitly specify the function via the SOAPAction header

内容的提问来源于stack exchange,提问作者Abhishek Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:56:30