wdele Posted October 19, 2014 Share Posted October 19, 2014 Hi, I'm new to creating widgets and I'm starting with a very simple one: <?php function widget_yo_momma() { $json = file_get_contents('http://api.yomomma.info/'); $data = json_decode($json); return array('title' => 'Yo Momma', 'content' => $data); } add_hook("AdminHomeWidgets",1,"widget_yo_momma"); ?> As you can see, it gets JSON from http://api.yomomma.info, which will contain a random Yo Momma joke. For example it can output: {"joke":"Yo momma so fat when she fell no one laughed but the ground started cracking up"} But the problem is, $data doesn't work. The widget content is just empty. Is this a problem with my PHP code or an error in the widget? I think my PHP (but I wouldn't know what) because it works when the content is $json instead of $data, but of course I don't want to print plain JSON! Thanks 0 Quote Link to comment Share on other sites More sharing options...
brian! Posted October 21, 2014 Share Posted October 21, 2014 I think the problem is that the api content is invalid - if you paste the link into jsonlint.com, it tells you that it's invalid - probably because of the google analytics etc being in the code... there are at least two ways around this - this simple method of replacing strings, or the longer version of stripping the content of the URL and then passing it to json_decode... the simple way... replacing the start & end strings and you are just left with the joke content. <?php function widget_yo_momma() { $json = file_get_contents('http://api.yomomma.info/'); $string1 = str_replace('{"joke":"', "", $json); $string2 = str_replace('"}', "", $string1); $content = '<table align="center"><tr><td>'.$string2.'</td></tr></table>'; return array('title' => 'Yo Momma', 'content' => $content); } add_hook("AdminHomeWidgets",1,"widget_yo_momma"); ?> you could probably use regex to do the replacing, but I was being lazy! the longer method requires the content to be stripped and only the content between the <body> tags is kept... then you can pass it to json_decode as normal. if you want to convert the output to an associative array, you use... <?php function widget_yo_momma() { $d = new DOMDocument; $mock = new DOMDocument; $d->loadHTML(file_get_contents('http://api.yomomma.info/')); $body = $d->getElementsByTagName('body')->item(0); foreach ($body->childNodes as $child){ $mock->appendChild($mock->importNode($child, true)); } $json = json_decode($mock->saveHTML()); $content = '<table align="center"><tr><td>'.$json["joke"].'</td></tr></table>'; return array('title' => 'Yo Momma', 'content' => $content); } add_hook("AdminHomeWidgets",1,"widget_yo_momma"); ?> if you keep it as an object, you can use... <?php function widget_yo_momma() { $d = new DOMDocument; $mock = new DOMDocument; $d->loadHTML(file_get_contents('http://api.yomomma.info/')); $body = $d->getElementsByTagName('body')->item(0); foreach ($body->childNodes as $child){ $mock->appendChild($mock->importNode($child, true)); } $json = json_decode($mock->saveHTML()); $content = '<table align="center"><tr><td>'.$json->joke.'</td></tr></table>'; return array('title' => 'Yo Momma', 'content' => $content); } add_hook("AdminHomeWidgets",1,"widget_yo_momma"); ?> though for this example, I don't think it really matters which you use. unfortunately, you seem to have chosen a bad api to write a widget for - if it were correctly formatted, you shouldn't need to do any work with the content and should just pass it to json_decode. 0 Quote Link to comment Share on other sites More sharing options...
fishy49 Posted October 31, 2014 Share Posted October 31, 2014 Funny that I found this here. I actually maintain the API, thanks for pointing this out. Should be working now without all the pesky html in there. Sorry for the trouble! 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.