1

我正在尝试执行以下代码:

<?php
$xml = <<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns="urn:enterprise.soap.sforce.com"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <loginResponse>
            <result>
                <metadataServerUrl>
                    https://na3.salesforce.com/services/Soap/m/28.0/00D50000000IdrE
                </metadataServerUrl>
                <passwordExpired>false</passwordExpired>
                <sandbox>false</sandbox>
                <serverUrl>
                    https://abc.com/services/Soap/c/28.0/00D50000000IdrE
                </serverUrl>
                <sessionId>
                    00D50000000IdrE!AREAQK4VYXRaHoL_uRvOi.QXXw3ahAt2Cge254wygiW7cr_f6DVa2pDC6g57w5IE
                    fidAu3ZRsJFBN5Bwb6DVhF18zKFiVVyT
                </sessionId>
                <userId>00550000001Dd4uAAC</userId>
                <userInfo>
                    <accessibilityMode>false</accessibilityMode>
                    <currencySymbol>$</currencySymbol>
                    <orgAttachmentFileSizeLimit>5242880
                    </orgAttachmentFileSizeLimit>
                    <orgDefaultCurrencyIsoCode>USD</orgDefaultCurrencyIsoCode>
                    <orgDisallowHtmlAttachments>false
                    </orgDisallowHtmlAttachments>
                    <orgHasPersonAccounts>false</orgHasPersonAccounts>
                    <organizationId>00D50000000IdrEEAS</organizationId>
                    <organizationMultiCurrency>false</organizationMultiCurrency>
                    <organizationName>3CLogic</organizationName>
                    <profileId>00e500000017al5AAA</profileId>
                    <roleId xsi:nil="true"/>
                    <sessionSecondsValid>7200</sessionSecondsValid>
                    <userDefaultCurrencyIsoCode xsi:nil="true"/>
                    <userEmail>ramana@3clogic.com</userEmail>
                    <userFullName>Ramana</userFullName>
                    <userId>00550Dd4uAAC</userId>
                    <userLanguage>en_US</userLanguage>
                    <userLocale>en_US</userLocale>
                    <userName>raman@gmail.com.com</userName>
                    <userTimeZone>America/New_York</userTimeZone>
                    <userType>Standard</userType>
                    <userUiSkin>Theme3</userUiSkin>
                </userInfo>
            </result>
        </loginResponse>
    </soapenv:Body>
</soapenv:Envelope>
EOD;
$sxe = new SimpleXMLElement($xml);
$sxe->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/ ');
$sxe->registerXPathNamespace('def', 'urn:enterprise.soap.sforce.com');
$result = $sxe->xpath('/env:Envelope/env:Body/def:loginResponse/def:result/def:serverUrl');
?>

但是,收到以下错误:

Call to undefined method SimpleXMLElement::registerXPathNamespace() 

我正在使用 php 版本 5.0.5。但是,使用 php 版本 5.1.0 和 5.5.3 我收到此错误但也没有输出。我正在尝试提取 xml 标记“serverUrl”的值。请帮忙。如果可能,请为 php 5.0.5 提出解决方案

4

1 回答 1

0

XPath 功能仅在 PHP 5.2.0 中添加到 SimpleXML 中(它本身比“生命终结”晚了 2 年半多),所以如果你真的坚持使用 5.0.5 一样古老的 PHP 版本,你必须想出一个不同的解决方案。

幸运的是,除了使用命名空间之外,您的 XPath 表达式非常基本,并且该->children()方法从 PHP 5.0.1 开始就能够处理这些。

因此,从那时起,此代码应该在任何版本的 PHP 下都可以正常运行:

$sxe = new SimpleXMLElement($xml);

// Define some short identifiers for namespaces without relying on their prefix in the XML
define('NS_ENV', 'http://schemas.xmlsoap.org/soap/envelope/');
define('NS_SFORCE', 'urn:enterprise.soap.sforce.com');

// $sxe already represents the soapenv:Envelope node, so we don't need to mention that
$result = $sxe
    ->children(NS_ENV)->Body
    ->children(NS_SFORCE)->loginResponse->result->serverUrl;

// $result is now the SimpleXML object for the `serverUrl` node(s)
// (unlike with ->xpath(), which returns a plain PHP array of "search results")
echo trim($result);

这是一个实时示例,显示它在各种不同版本的 PHP 中进行了评估。

于 2013-09-01T20:20:44.627 回答