0

I have the following code:

$url      = 'http://api.creativecommons.org/rest/1.5/license/standard/get'
                .'?commercial=y&derivatives=y&jurisdiction=ca';
$response = simplexml_load_file($url);

echo $response->result->license-name;

Which uses this URL: http://api.creativecommons.org/rest/1.5/license/standard/get?commercial=y&derivatives=y&jurisdiction=ca

But the only output I get is:

PHP Parse error: parse error, expecting T_STRING' orT_VARIABLE' or '{'' or'$'' in /path/to/script.php on line 3

What am I doing wrong?

hakre
  • 193,403
  • 52
  • 435
  • 836
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 2
    You have a `-` in `license-name`. You need to provide something that can be subtracted on the right hand side. – Wooble Jun 24 '13 at 12:23
  • How would I get license-name then? – Doug Smith Jun 24 '13 at 12:27
  • The more general related question is [How do I access this object property?](http://stackoverflow.com/q/758449/367456) as this is not limited to SimpleXML. Another dedicated simplexml Q&A with a nice and short answer is: [Using XML node names with hyphens in PHP](http://stackoverflow.com/q/3634599/367456). – hakre Jun 24 '13 at 22:06
  • Please understand that a simple "What am I doing wrong?" question is not constructive and discouraged on this website. We expect you put in facts and that you research your issue. This question is very verbose for 2013, pleas use the search and understand your issue more before asking a question. – hakre Jun 24 '13 at 22:07
  • [Reference - What does this error mean in PHP?](http://stackoverflow.com/q/12769982/367456) – hakre Jun 24 '13 at 22:14

1 Answers1

2

Special characters in name will not fetch you the value.

You need to use it like this:

echo $response->{'license-name'};
hakre
  • 193,403
  • 52
  • 435
  • 836
user1402647
  • 480
  • 4
  • 9
  • That's not a special character, OP has written a standard minus operation with the minus operator. Also you should link as well the PHP manual to a page where it explains this, otherwise it's just providing some "working code" and next user will just typehit the next question in. Also it's a duplicate. Sorry for so much critique here. – hakre Jun 24 '13 at 22:12
  • @hakre: sorry to say i fail to understand your response. in php `minus` operations work only on numbers not on strings. it is a variable name with a `-` not a minus operation. the example i have posted above is a standard way of accessing variables in php - it is not a working sample. – user1402647 Jun 25 '13 at 07:22
  • What OP has written and what has thought he has written indeed does not match. On closer sight it is not understandable, the only conclusion that is left is that OP made a little mistake. That can happen, however it does not qualify as a programming question. You will see the question closed and removed the sooner or later. Thanks for helping out, just a comment does as well. Probably worth to know for the next time. – hakre Jun 25 '13 at 08:52