简单的 XML PHP 错误 - Echo 没什么

发布于 2024-09-17 09:10:58 字数 653 浏览 3 评论 0原文

好吧,

基本上,我从这样的 URL 加载 simplexml_load_file ,

$stats =  simplexml_load_file("http://example.com/api/api.asmx/Campaign.GetSummary?ApiKey=$apikey&CampaignID=$CID");

它返回这个

SimpleXMLElement Object
(
    [Recipients] => 1
    [TotalOpened] => 0
    [Clicks] => 0
    [Unsubscribed] => 0
    [Bounced] => 0
    [UniqueOpened] => 0
)

在我加载之后我想回显信息,所以我尝试像这样回显它

echo '<ul id="views">'; 
echo '<li>';
print $stats['Recipients'];
echo '</li>';
echo '</ul>';

但是当它运行时,我没有得到任何数据,只是一个空的

  • Ok guys,

    Essentially, im loading a simplexml_load_file from a URL like this

    $stats =  simplexml_load_file("http://example.com/api/api.asmx/Campaign.GetSummary?ApiKey=$apikey&CampaignID=$CID");
    

    Which returns this

    SimpleXMLElement Object
    (
        [Recipients] => 1
        [TotalOpened] => 0
        [Clicks] => 0
        [Unsubscribed] => 0
        [Bounced] => 0
        [UniqueOpened] => 0
    )
    

    After I load that up I want to echo the info, so I try to echo it out like so

    echo '<ul id="views">'; 
    echo '<li>';
    print $stats['Recipients'];
    echo '</li>';
    echo '</ul>';
    

    But when it runs, I dont get any of the data, just an empty <li></li>

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(2

    醉生梦死 2024-09-24 09:10:58

    使用 SimpleXMLElements 时,不使用 [] 表示法 - 而是使用 ->。因此,您的代码应该是:

    echo '<ul id="views">'; 
    echo '<li>';
    print $stats->Recipients;
    echo '</li>';
    echo '</ul>';
    

    我相信这种表示法可能(在我的应用程序中确实如此,但我对 SimpleXMLElements 不太熟悉)返回 SimpleXMLElement 对象,而不是字符串 - 您可以将其转换为字符串/整数/任何在比较等中使用它的东西。

    When working with SimpleXMLElements, you do not use the [] notation - instead you use ->. So, your code should be:

    echo '<ul id="views">'; 
    echo '<li>';
    print $stats->Recipients;
    echo '</li>';
    echo '</ul>';
    

    I believe such notation may (it does in my application, but I am not overly familiar with SimpleXMLElements) return a SimpleXMLElement object, not a string - you can cast it to a string/int/whatever to use it in comparisons etc.

    墨落成白 2024-09-24 09:10:58

    SimpleXMLElement Object 不是一个数组,它是一个对象,线索就在名称中:-)

    您需要使用对象表示法来访问它

     $stats->Recipients
    

    SimpleXMLElement Object is not an array, it is an Object, the clue is in the name :-)

    You need to access it using Object notation

     $stats->Recipients
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文