无法导航SimpleXMLElement数据

时间:2014-01-03 20:09:19

标签: simplexml

我正在尝试使用REST Web服务。

以下是代码 //在自定义标题中设置标记

$options = array(
    'http'=>array(
    'method'=>"GET",
    'header'=>"authorization:$cj_token\r\n"));

$context=stream_context_create($options);
$mymerchants="https://advertiser-lookup.api.cj.com/v3/advertiser-lookup?advertiser-ids=joined";
$sxml = simplexml_load_string(file_get_contents($mymerchants,false,$context));

foreach ($sxml->advertisers->advertiser as $advertiser) {
    print_r($advertiser);
    //$a = $advertiser->advertiser-id;
    $language = $advertiser->language;      
    //echo "<BR>id=".$a;
    echo "<BR>langauge=".$language;
    echo "<BR><BR>";
}

$ sxml的值如下 SimpleXMLElement对象([advertisers] =&gt; SimpleXMLElement对象([@attributes] =&gt;数组([total-matched] =&gt; 496 [records-returned] =&gt; 25 [page-number] =&gt; 1)[ advertiser] =&gt;数组([0] =&gt; SimpleXMLElement对象([advertiser-id] =&gt; 129899 [account-status] =&gt;有效[七天-epc] =&gt; 7.81 [三个月 - epc] =&gt; 9.66 [语言] =&gt; zh [advertiser-name] =&gt; BOOKSAMILLION.COM [program-url] =&gt; xhttp://www.booksamillion.com [relationship-status] =&gt;加入[mobile-tracking-certified] =&gt; false [network-rank] =&gt; 4 [primary-category] ​​=&gt; SimpleXMLElement对象([parent] =&gt; Books / Media [child] =&gt; Books)[性能] -incentives] =&gt; false [actions] =&gt; SimpleXMLElement对象([action] =&gt; SimpleXMLElement对象([name] =&gt; Sale [type] =&gt; sale [id] =&gt; 266 [佣金] = &gt; SimpleXMLElement对象([默认] =&gt; 5.00%)))[link-types] =&gt; SimpleXMLElement对象([link-type] =&gt;数组([0] =&gt;文字链接[1] =&gt;横幅[2] =&gt; DeepLink [3] =&gt;高级链接)))[1] =&gt; SimpleXMLElement对象([advertiser-id] =&gt; 79362 [account-status] =&gt;有效[seven-day-epc] =&gt; 19.94 [three-month-epc] =&gt; 16.17 [语言] =&gt; zh [advertiser-name] =&gt; Testclear.com [program-url] =&gt; xhttp://www.testclear.com [relationship-status] =&gt;加入[mobile-tracking-certified] =&gt; false [网络-rank] =&gt; 1 [primary-category] ​​=&gt; SimpleXMLElement对象([parent] =&gt; Health and Wellness [child] =&gt;健康食品)[performance-incentive] =&gt; true [actions] =&gt; ; SimpleXMLElement对象([action] =&gt; SimpleXMLElement对象([name] =&gt; Sale [type] =&gt; sale [id] =&gt; 234 [委托] =&gt; SimpleXMLElement对象([default] =&gt; 12.00 %)))[link-types] =&gt; SimpleXMLElement Object([link-type] =&gt; Array([0] =&gt; Text Link [1] =&gt; Banner [2] =&gt; DeepLink))) [2] =&gt; SimpleXMLElement对象([advertiser-id] =&gt; 198699 [account-status] =&gt;有效[seven-day-epc] =&gt; 17.86 [three-month-epc] =&gt; 10.10 [语言] =&gt; zh [advertiser-name] =&gt; Wickers Sportswear,Inc。[program-url] =&gt; xhttp://www.wickers.com [relationship-status] =&gt;已加入[mobile-tracking-certified] =&gt; false [network-rank] =&gt; 1 [primary-category] ​​=&gt; SimpleXMLElement对象([parent] =&gt; Sports&amp; Fitness [child] =&gt;服饰)[performance-incentive] =&gt; false [actions] =&gt; SimpleXMLElement对象([action] =&gt; SimpleXMLElement对象([name] =&gt; Sale [type] =&gt; sale [id] =&gt; 435 [委托] =&gt; SimpleXMLElement对象([default] =&gt; ; 7.00%)))[link-types] =&gt; SimpleXMLElement对象([link-type] =&gt;数组([0] =&gt;文字链接[1] =&gt;横幅[2] =&gt; DeepLink [ 3] =&gt;高级链接)))

当我通过这些数据循环时,我能够获得“语言”元素的值,但是如果我尝试获取“advertiser-id”字段的值,我会收到错误“(!)注意:使用undefined constant id - 假设'id'“。我的猜测是广告客户和id之间的破折号( - )是问题的原因,因为我已经成功地显示了语言元素的值。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

由于破折号,

“advertiser-id”不是有效的标识符。 PHP将短划线解释为减号,因此$advertiser->advertiser-id被解析为$advertiser->advertiser减去名为id的常量。

请尝试使用$advertiser->{'advertiser-id'}。这是访问名称不是有效标识符的属性的一种方法。