simplexml


can't navigate SimpleXMLElement data


I am trying to consume a REST web service.
Below is the code
//set token in custom header
$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>";
}
The value of $sxml is as below
SimpleXMLElement Object ( [advertisers] => SimpleXMLElement Object ( [#attributes] => Array ( [total-matched] => 496 [records-returned] => 25 [page-number] => 1 ) [advertiser] => Array ( [0] => SimpleXMLElement Object ( [advertiser-id] => 129899 [account-status] => Active [seven-day-epc] => 7.81 [three-month-epc] => 9.66 [language] => en [advertiser-name] => BOOKSAMILLION.COM [program-url] => xhttp://www.booksamillion.com [relationship-status] => joined [mobile-tracking-certified] => false [network-rank] => 4 [primary-category] => SimpleXMLElement Object ( [parent] => Books/Media [child] => Books ) [performance-incentives] => false [actions] => SimpleXMLElement Object ( [action] => SimpleXMLElement Object ( [name] => Sale [type] => sale [id] => 266 [commission] => SimpleXMLElement Object ( [default] => 5.00% ) ) ) [link-types] => SimpleXMLElement Object ( [link-type] => Array ( [0] => Text Link [1] => Banner [2] => DeepLink [3] => Advanced Link ) ) ) [1] => SimpleXMLElement Object ( [advertiser-id] => 79362 [account-status] => Active [seven-day-epc] => 19.94 [three-month-epc] => 16.17 [language] => en [advertiser-name] => Testclear.com [program-url] => xhttp://www.testclear.com [relationship-status] => joined [mobile-tracking-certified] => false [network-rank] => 1 [primary-category] => SimpleXMLElement Object ( [parent] => Health and Wellness [child] => Health Food ) [performance-incentives] => true [actions] => SimpleXMLElement Object ( [action] => SimpleXMLElement Object ( [name] => Sale [type] => sale [id] => 234 [commission] => SimpleXMLElement Object ( [default] => 12.00% ) ) ) [link-types] => SimpleXMLElement Object ( [link-type] => Array ( [0] => Text Link [1] => Banner [2] => DeepLink ) ) ) [2] => SimpleXMLElement Object ( [advertiser-id] => 198699 [account-status] => Active [seven-day-epc] => 17.86 [three-month-epc] => 10.10 [language] => en [advertiser-name] => Wickers Sportswear, Inc. [program-url] => xhttp://www.wickers.com [relationship-status] => joined [mobile-tracking-certified] => false [network-rank] => 1 [primary-category] => SimpleXMLElement Object ( [parent] => Sports & Fitness [child] => Apparel ) [performance-incentives] => false [actions] => SimpleXMLElement Object ( [action] => SimpleXMLElement Object ( [name] => Sale [type] => sale [id] => 435 [commission] => SimpleXMLElement Object ( [default] => 7.00% ) ) ) [link-types] => SimpleXMLElement Object ( [link-type] => Array ( [0] => Text Link [1] => Banner [2] => DeepLink [3] => Advanced Link ) ) )
When I loop thru this data, I am able to get value for the "language" element, however if I try to get value for "advertiser-id" field, I get the error "( ! ) Notice: Use of undefined constant id - assumed 'id'". My guess is that dash (-) between advertiser and id is the cause of the issue as I have successully displayed value for the language element. Thanks for your help.
"advertiser-id" is not a valid identifier, due to the dash. PHP is interpreting the dash as a minus sign, so $advertiser->advertiser-id is being parsed as $advertiser->advertiser minus the constant named id.
Try using $advertiser->{'advertiser-id'} instead. That's one way to access properties whose names aren't valid identifiers.

Related Links

PHP 7 simpleXML fatal error
SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found
Add xml attribute documentname
Pick shortest route with simpleXML
SimpleXML extract text between two tags
XML Node outside parent item
SimpleXMLElement cannot get the output
SimpleXML changing parsed decimals
can't navigate SimpleXMLElement data
PHP SimpleXML Load File with Null elements
Iterating through and editing a SimpleXML object by reference in PHP
Simplexml Insert into sub element
warning: simplexml_load_file(): I/O warning : failed to load external entity drupal [closed]
What's wrong with this code? SimpleXMLElement [closed]
Get children attributes using simplexml
Select attribute in simplexml

Categories

HOME
jdo
hive
minimum-spanning-tree
infragistics
rubygems
binary-tree
umd
callback
cross-browser
autotools
vifm
ibm-odm
clearcase-ucm
highlight.js
reactive-cocoa
shared-hosting
google-pagespeed
jasonette
sylius
accessor
wkwebview
rundeck
android-widget
social-media
ejabberd-module
saas
compatibility
large-file-upload
libssl
create-table
pdb
language-concepts
picasso
catalog
xcode-extension
theano.scan
ansible-playbook
streamreader
graphenedb
particles.js
gulp-sourcemaps
g-code
no-www
midl
vao
import-from-excel
netcdf4
fault
outlook-2013
jedis
dds
celery-task
knockout-components
sqlclient
snmptrapd
sqlbulkcopy
zendesk-app
flow-control
flutterwave
minimization
log4c
url-masking
adxstudio-portals
merge-conflict-resolution
impresspages
plottable.js
fuzzy-search
skype4py
dataview
system.management
freelancer.com-api
spim
lib.web.mvc
content-length
android-listview
system32
c3
gadt
aapt
jboss-weld
hyprlinkr
concurrent-collections
prettify
flexmojos
page-layout
dexterity
mqx
limejs
lcs
runas
automount
heartbeat
infobox
file-comparison
propagation
wise
web-architecture
brewmp
sef
backcolor
avatar
w3wp
ugc
wsdl.exe
document-conversion

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App