We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Plugin that almost works

Development and Programming

scottb's avatar
scottb
348 posts
15 years ago
scottb's avatar scottb

My php skills are highly limited, but I’ve built a simple plugin that grabs weather data from one site and displays it in an EE template on another site. The plugin works fine as raw php, and it works fine on a blank EE template.

But when I put the tags between a template header and footer, the output displays above the header rather than underneath it.

I’ve looked at other plugins for a clue as to what’s causing this, but without success. Can anyone offer a suggestion?

[Mod Edit: Moved to the Development and Programming forum]

       
hd 's avatar
hd 
156 posts
15 years ago
hd 's avatar hd 

you are probably echo-ing the content, instead of returning the content.

for example, in a plugin you must:

$this->return_data = 'your content';

and not:

echo 'your content';
       
scottb's avatar
scottb
348 posts
15 years ago
scottb's avatar scottb

Thanks for the reply, HD. I’m actually using neither one, but you’re close to the problem. The plugin uses curl to get the data. When I originally had $this->return_data = ‘your content’ I ended up with a resource error. When I removed it, the error went away and the content displayed fine. But even with it in there, I still end up with the data displaying above the header.

I assume that by following the API tutorial and having this:

var $return_data = "";

And having the curl statements within double quotes was creating the resource error. But every attempt to find a better way didn’t seem to help. Any thoughts on a way to have curl work within return_data?

Like I said, my php is limited…

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Scott

Could you share the whole plugin (or at least the function in question) with us?

       
scottb's avatar
scottb
348 posts
15 years ago
scottb's avatar scottb

Sure thing. The EE template tags are:

{exp:Weather3} zip code {/exp:Weather3}

Here is the main part of the plugin:

class Weather3
{

var $return_data;

  function Weather3()
  {
    global $TMPL;

    $tagdata = $TMPL->tagdata;

        $tagdata = trim($tagdata);
        $tagdata = str_replace('\n','', $tagdata);
        $tagdata = str_replace('\r','', $tagdata);
        $tagdata = str_replace('\t','', $tagdata);
        $tagdata = str_replace('\x0B','', $tagdata);

    $ch = curl_init();

    curl_setopt ($ch, CURLOPT_URL, "http://www.mysite.com/weather_script_path&zipcode;=".$tagdata."");

    curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
 
    curl_setopt($ch, CURLOPT_HEADER, 0);

    $output = curl_exec($ch);
 
    curl_close($ch);
 
    return $output;

  }

It pulls a three-day weather forecast from a weather database I installed on one site and displays it on a partner’s Web site.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

For your constructor I think you need to utilize the $return_data variable (though I don’t think it’s required for other functions within your plugin class)

Try replacing this

return $output;

with this

$this->return_data = $output;
       
scottb's avatar
scottb
348 posts
15 years ago
scottb's avatar scottb

That didn’t do the trick, but when I put the php directly on an EE template and set the preferences to php and input, the header displayed correctly at the top.

I’m starting to think it has something to do with the passing of tag data from the template to the plugin.

       
Adrian Macneil (Crescendo)'s avatar
Adrian Macneil (Crescendo)
28 posts
15 years ago
Adrian Macneil (Crescendo)'s avatar Adrian Macneil (Crescendo)

Try this..

Note I haven’t actually tested this, I’m just going from memory…

{exp:Weather3 zip_code="12345"}
class Weather3
{

  var $return_data = '';

  function Weather3()
  {
    // get a reference to the ee super object
    $this->ee =& get_instance();
    
    // get parameters from the template (seems more logical than using tag data)
    $zip_code = (int)$this->ee->TMPL->fetch_param('zip_code');
    
    // do curl to weather service
    $ch = curl_init();
    // note the EE forum seems to put an extra semicolon in the middle of this line
    curl_setopt ($ch, CURLOPT_URL, 'http://www.mysite.com/weather_script_path&zipcode;='.$zip_code);
    curl_setopt($ch, CURLOPT_USERAGENT, 'MozillaXYZ/1.0');
    curl_setopt($ch, CURLOPT_HEADER, 0);
    
    $output = curl_exec($ch)
    curl_close($ch)
    
    // save the output to this variable
    $this->return_data = $output;
  }
}

😊

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
15 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

It’s important to note that your original code is for the 1.x branch of EE and Crescendo’s suggestion is for the 2.x branch.

What version of EE are you using?

       
Adrian Macneil (Crescendo)'s avatar
Adrian Macneil (Crescendo)
28 posts
15 years ago
Adrian Macneil (Crescendo)'s avatar Adrian Macneil (Crescendo)

Ah good point - I’m pretty new to EE development, was wondering where that global variable came from!

       
scottb's avatar
scottb
348 posts
15 years ago
scottb's avatar scottb

Quite right,I’m using 1.6.9. Unfortunately, I’m dealing with other issues in 2.1, so I’m not ready to switch yet.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.