Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7003381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:03:40+00:00 2026-05-27T21:03:40+00:00

couldn’t find a similar topic but this may boil down to not knowing how

  • 0

couldn’t find a similar topic but this may boil down to not knowing how to adresse my issue, so here goes.

I’ve got this block of code that’ll post to twitter using the epitwitter library:

<?php
include 'includes/EpiCurl.php';
include 'includes/EpiOAuth.php';
include 'includes/EpiTwitter.php';
include 'includes/tokens.php';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
$twitterObj->setToken($oauth_token, $oauth_secret);

$update_status = $twitterObj->post_statusesUpdate(array('status' => 'This is a test tweet!'));
$temp = $update_status->response;
?>

This is working perfectly and everything is good, until i do this:

<?php
include 'includes/EpiCurl.php';
include 'includes/EpiOAuth.php';
include 'includes/EpiTwitter.php';
include 'includes/tokens.php';

function postTweet() {
    $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
    $twitterObj->setToken($oauth_token, $oauth_secret);

    $update_status = $twitterObj->post_statusesUpdate(array('status' => 'This is a sample tweet!'));
    $temp = $update_status->response;
}

postTweet();
?>

Putting the code inside a function somehow breaks it and causes it to return a 500 error. Would anyone be able to explain this behavior and help me fix it?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:03:40+00:00Added an answer on May 27, 2026 at 9:03 pm

    Most likely this is because all the secret and token variables are defined in global scope inside the included files. You moved them into a function out of scope, and one of the functions called inside it cannot handle the empty variables without erroring out.

    Access them globally, or pass them as parameters to your function.

    Recommended: Pass parameters to the function

    // Pass variables as parameters to your function
    function postTweet($consumer_key, $consumer_secret, $oath_token, $oauth_secret) {
        $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
        $twitterObj->setToken($oauth_token, $oauth_secret);
    
        $update_status = $twitterObj->post_statusesUpdate(array('status' => 'This is a sample tweet!'));
        $temp = $update_status->response;
    }
    

    Alternative: use global

    The alternative solution, but the less preferred solution is to use the global keyword (or $GLOBALS[] array). It is usually considered best practice to pass them as parameters as I’ve done above though.

    function postTweet() {
       // Access them with the 'global' keyword
       global $consumer_key, $consumer_secret, $oath_token, $oauth_secret;
    
       $twitterObj = new EpiTwitter($consumer_key, $consumer_secret);
        $twitterObj->setToken($oauth_token, $oauth_secret);
    
        $update_status = $twitterObj->post_statusesUpdate(array('status' => 'This is a sample tweet!'));
        $temp = $update_status->response;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Couldn't really find it, but probably it's me not knowing how to search properly
I couldn't really find this in Rails documentation but it seems like 'mattr_accessor' is
I couldn't find a more descriptive title, but here there is an example: import
I couldn't find this online so I thought I'd ask here. What protocols can
I couldn't find a similar question, that's why here it is: Whats the best
Couldn't find an answer to this question. It must be obvious, but still. I
I couldn't find this anywhere, so i thought I'd ask here. Can you do
I couldn't find a proper discussion thread on this topic, so I'm going to
Couldn't find any answer to this problem, or not even any questions asked. So
Couldn't find anything about this topic. I have a Windows TCP C++ server application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.