0

I have a JSON file that is causing me some trouble. It looks as follows.

process({
  "www.google.com": {
    "target": "google.com",
    "0": [ 94, 70 ],
    "1": [ 94, 70 ],
    "2": [ 94, 70 ],
    "4": [ 93, 66 ],
    "categories": {
      "501": 99,
      "301": 48,
      "304": 5
      }
    }
  }
)

How would I go about echoing out the contents of "target" or "4" or "categories". I am rather new to JSON, but I have done my research and I am having no luck. Here is what I currently have.

<?php
$xml = file_get_contents('http://api.mywot.com/0.4/public_link_json2?hosts=http://www.google.com/&callback=process&key=*****');
//print_r($xml);
$json = json_decode($xml, true);
echo '<pre>' . print_r($xml, true) . '</pre>';

var_dump($json["www.google.com"]['target']);

You can see that I am getting the contents of my JSON from the mywot API. I am then printing it for testing purposes so I can see it. I just cant' access any of the individual elements in there. What am I doing wrong? The var_dump was just a test, but it returns NULL.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • You've just posted your API key to the Internet and should now consider it compromised. You should invalidate it and create a new one immediately. – Alex Howansky Apr 25 '17 at 18:12
  • `NULL` after `json_decode` means that you have error on parsing. [See](http://php.net/manual/ru/function.json-last-error.php) to debug error – Pavlo Zhukov Apr 25 '17 at 18:14
  • I'll bet you have a UTF-8 encoding problem – Machavity Apr 25 '17 at 18:15
  • just remove `&callback=process` from the url – Pedro Lobito Apr 25 '17 at 18:16
  • What you have is a JSONP response, as described by [the API docs](https://www.mywot.com/wiki/API#public_link_json2). Try omitting the `callback` parameter. If that doesn't work you'll need to strip the leading `process(` and trailing `)`. – Jordan Running Apr 25 '17 at 18:17

0 Answers0