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

PHP中无WSDL的SOAP服务如何获取目标命名空间与调用URI

在PHP中调用无WSDL的SOAP服务:获取目标命名空间及调用方法

我需要在PHP环境下调用一个没有提供WSDL的SOAP Web服务,但不知道该如何获取SOAPClient所需的URI(即目标命名空间)。以下是服务文档提供的SOAP 1.2请求/响应示例,以及HTTP POST请求/响应示例,文档里完全没提到相关信息,求帮忙解决:

SOAP 1.2 请求示例

POST /webservices/wssamples/service.asmx HTTP/1.1
Host: www.samplecomponents.com
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>
    <QueryAvailability xmlns="http://www.samplecomponents.com/webservices/">
      <program_id>string</program_id>
      <security_id>string</security_id>
      <part_number>string</part_number>
    </QueryAvailability>
  </soap12:Body>
</soap12:Envelope>

SOAP 1.2 响应示例

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>
    <QueryAvailabilityResponse xmlns="http://www.samplecomponents.com/webservices/">
      <QueryAvailabilityResult xmlns="http://www.samplecomponents.com/schemas/sample_availability.xsd">
        <timestamp>dateTime</timestamp>
        <item_count>unsignedInt</item_count>
        <items>
          <item>
            <part_id>unsignedInt</part_id>
            <manufacturer_part_number>string</manufacturer_part_number>
            <stock_source>string</stock_source>
            <digikey_part_number>string</digikey_part_number>
            <description>string</description>
            <quantity_available>unsignedInt</quantity_available>
            <break_quantity>unsignedInt</break_quantity>
            <rohs_compliant>bytes</rohs_compliant>
            <flag_non_stock>boolean</flag_non_stock>
            <flag_obsolete>boolean</flag_obsolete>
            <flag_static_sensitive>boolean</flag_static_sensitive>
          </item>
          <item>
            <part_id>unsignedInt</part_id>
            <manufacturer_part_number>string</manufacturer_part_number>
            <stock_source>string</stock_source>
            <digikey_part_number>string</digikey_part_number>
            <description>string</description>
            <quantity_available>unsignedInt</quantity_available>
            <break_quantity>unsignedInt</break_quantity>
            <rohs_compliant>bytes</rohs_compliant>
            <flag_non_stock>boolean</flag_non_stock>
            <flag_obsolete>boolean</flag_obsolete>
            <flag_static_sensitive>boolean</flag_static_sensitive>
          </item>
        </items>
      </QueryAvailabilityResult>
    </QueryAvailabilityResponse>
  </soap12:Body>
</soap12:Envelope>

HTTP POST 请求示例

POST /webservices/wssamples/service.asmx/QueryAvailability HTTP/1.1
Host: www.samplecomponents.com
Content-Type: application/x-www-form-urlencoded
Content-Length: length
program_id=string&security_id=string&part_number=string

HTTP POST 响应示例

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<sample_availability xmlns="http://www.samplecomponents.com/schemas/sample_availability.xsd">
  <timestamp>dateTime</timestamp>
  <item_count>unsignedInt</item_count>
  <items>
    <item>
      <part_id>unsignedInt</part_id>
      <manufacturer_part_number>string</manufacturer_part_number>
      <stock_source>string</stock_source>
      <digikey_part_number>string</digikey_part_number>
      <description>string</description>
      <quantity_available>unsignedInt</quantity_available>
      <break_quantity>unsignedInt</break_quantity>
      <rohs_compliant>bytes</rohs_compliant>
      <flag_non_stock>boolean</flag_non_stock>
      <flag_obsolete>boolean</flag_obsolete>
      <flag_static_sensitive>boolean</flag_static_sensitive>
    </item>
    <item>
      <part_id>unsignedInt</part_id>
      <manufacturer_part_number>string</manufacturer_part_number>
      <stock_source>string</stock_source>
      <digikey_part_number>string</digikey_part_number>
      <description>string</description>
      <quantity_available>unsignedInt</quantity_available>
      <break_quantity>unsignedInt</break_quantity>
      <rohs_compliant>bytes</rohs_compliant>
      <flag_non_stock>boolean</flag_non_stock>
      <flag_obsolete>boolean</flag_obsolete>
      <flag_static_sensitive>boolean</flag_static_sensitive>
    </item>
  </items>
</sample_availability>

文档里完全没提到需要提供的URI或目标命名空间,求帮忙。


解决方案

1. 确定目标命名空间

从SOAP请求示例里的QueryAvailability元素的xmlns属性可以直接拿到目标命名空间:
http://www.samplecomponents.com/webservices/

这个就是SOAPClient调用时需要的URI参数。

2. PHP中调用无WSDL的SOAP服务的两种方式

方式一:使用SOAPClient的非WSDL模式

直接实例化SOAPClient时传入null作为WSDL参数,同时指定location和uri选项:

<?php
// 服务地址
$location = 'http://www.samplecomponents.com/webservices/wssamples/service.asmx';
// 目标命名空间
$uri = 'http://www.samplecomponents.com/webservices/';

// 初始化SOAPClient(非WSDL模式)
$client = new SoapClient(null, [
    'location' => $location,
    'uri' => $uri,
    'soap_version' => SOAP_1_2, // 匹配服务使用的SOAP 1.2版本
    'encoding' => 'UTF-8'
]);

// 构造请求参数
$params = [
    'program_id' => '你的program_id值',
    'security_id' => '你的security_id值',
    'part_number' => '你的part_number值'
];

// 调用服务方法
try {
    $response = $client->__soapCall('QueryAvailability', [$params]);
    // 处理响应
    print_r($response);
} catch (SoapFault $e) {
    // 捕获错误
    echo "Error: " . $e->getMessage();
}
?>

方式二:直接发送HTTP POST请求(调试更直观)

如果觉得SOAPClient的非WSDL模式不好调试,可以直接用PHP的curl发送符合要求的HTTP请求:

<?php
$url = 'http://www.samplecomponents.com/webservices/wssamples/service.asmx/QueryAvailability';

// 构造表单数据
$data = [
    'program_id' => '你的program_id值',
    'security_id' => '你的security_id值',
    'part_number' => '你的part_number值'
];

// 初始化curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded'
]);

// 执行请求并获取响应
$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch);
} else {
    // 解析XML响应
    $xml = simplexml_load_string($response);
    print_r($xml);
}
curl_close($ch);
?>

注意事项

  • 确保PHP开启了soap扩展(在php.ini中启用extension=soap)
  • 如果服务使用HTTPS,需将URL改为https://,并根据情况添加curl的SSL相关选项(如CURLOPT_SSL_VERIFYPEER)
  • 解析SOAP响应时,注意QueryAvailabilityResult属于http://www.samplecomponents.com/schemas/sample_availability.xsd命名空间,若需精准解析节点,需指定该命名空间

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 20:11:48