0

This is what I tried:

$doc = new DOMDocument();

    $jsonurl = "http://v1.syndication.nhschoices.nhs.uk/.json?apikey=xxxxxx";
    $doc->load($jsonurl);
    var_dump(json_decode($doc));
    var_dump(json_decode($doc, true));

The output is NULL NULL for the 2 var_dumps.

The JSON returned from the url looks like this (after view source):

[{"Text":"Live Well","Uri":"http:\/\/v1.syndication.nhschoices.nhs.uk\/livewell?apikey=xxxxx"},{"Text":"Conditions","Uri":"http:\/\/v1.syndication.nhschoices.nhs.uk\/conditions?apikey=xxxxx"},{"Text":"Organisations","Uri":"http:\/\/v1.syndication.nhschoices.nhs.uk\/organisations?apikey=xxxxx"},{"Text":"Carers Direct","Uri":"http:\/\/v1.syndication.nhschoices.nhs.uk\/carersdirect?apikey=xxxxx"}]

Is this valid?

3
  • 1
    What does var_dump($doc) output. Is it valid json? Commented Mar 11, 2011 at 11:51
  • 2
    Why are you loading the JSON into a DOMDocument? JSON != XML. Commented Mar 11, 2011 at 11:52
  • Just put the JSON through jsonlint.com and it came out as valid :/ Commented Mar 11, 2011 at 13:25

4 Answers 4

9

If that URL returns a JSON string, you should not use the DOMDocument class to load it : that class is to manipulate XML data.


Instead, you probably should do something like this, using [**`file_get_contents()`**][2] to do the HTTP request, and get its raw result :
$url = 'http://v1.syndication.nhschoices.nhs.uk/.json?apikey=xxxxxx';
$json_string = file_get_contents($url);
$data = json_decode($json_string);
var_dump($data);

As a sidenote : using `file_get_contents()` to make HTTP requests will only work if [`allow_url_fopen`][3] is enabled.

If allow_url_fopen is not enabled, you'll have to be ready to fallback to a solution based on curl.

Sign up to request clarification or add additional context in comments.

3 Comments

If it still doesn't work after this, it's due to php's json encoding & decoding being very picky. You might have to refactor the json before passing it into json_decode
Yea, it'll require valid JSON (and rightly so). Check it with jsonlint.com.
Checked php info: allow_url_fopen Local: On, Master: On
0

You're trying to somehow decode a DOMDocument. json_decode works on strings. What is the relationship between a DOM document and a JSON string? Not very much at all. Pick one!

Comments

0

@Pascal MARTIN

With a slight modification it works:

$jsonurl = 'http://v1.syndication.nhschoices.nhs.uk/.json?apikey=XXXXXX';
$jsonstr = json_encode(file_get_contents($jsonurl));
$data = json_decode($jsonstr);
var_dump(json_decode($data));

I added json_encode around the file_get_contents call. Thanks all for the help :)

2 Comments

Somehow this seems wrong. json_encode either takes an array or object as input, but not a string.
The var_dump of data returns an object. Still figuring out how to get to the elements!
-1
http://v1.syndication.nhschoices.nhs.uk/.json

I think your url is not valid? /.json maybe should be /something.json or maybe /json

5 Comments

-1 Why would this not be valid? It's perfectly valid. e.g. .htaccess. Windows has brainwashed you into thinking that every file must be "file.ext", which is simply not true.
Why do you think you should personally attack me? If I am not correct than give me some info. By the way I am not using windows.
I did not personally attack you. I made an observation. There is no reason to think that this URL is invalid other than through the widespread Windows brainwashing. If it's not Windows that's led you to believe that a file must have characters before a period, then I can't imagine what did!
It works - I have removed the apikey for security reasons. They also offer the data as XML e.g. v1.syndication.nhschoices.nhs.uk/.xml?apikey=XXXXXX xhtml+xml and text/html.
@JohnnyBizzle ok, my bad. @Tomatak your example is contradictory, htaccess is not accessible from browser. You sad something about brainwashing which I think (with my brainwashed brain) is not a compliment. You do not know a thing about me, so you are only guessing. Further more I only wanted to help, and I learned something I did not know. I agree with your technical part, but your attitude is bad. Experience led me to believe that dot files are not accessible from web, now I know correct state of matter. Kiss my darling, do not be so frustrated with life :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.