Jump to content

What's wrong with my widget?


wdele

Recommended Posts

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 :)

Link to comment
Share on other sites

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. :idea:

 

z1KYIbm.png

 

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.

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated