phpxmlhttprequestaccesscontrolexception

Twitter feed has stopped working, Access-Control-Allow-Origin


This is my site here:

Income Brokers

Up until recentley my Tweets where displaying fine, however now I am getting this error in the console:

XMLHttpRequest cannot load http://www.incomebrokers.com/js/get_tweets.php. Origin http://incomebrokers.com is not allowed by Access-Control-Allow-Origin.

I don't know why it has just stopped working...Can anyone help me out?

This is my get_tweets.php script:

session_start();
require_once('twitteroauth/twitteroauth/twitteroauth.php');
$twitteruser = "xxxxxxx";
$notweets = 30;
$consumerkey="xxxxxxxxxx";
$consumersecret="xxxxxxxxxxxxxxx";
$accesstoken="xxxxxxxxxxxxxxxxxxxxx";
$accesstokensecret="xxxxxxxxxxxxxxxxxxxxxxxxxx";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret){

    $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);

    return $connection;

}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

echo json_encode($tweets);

Solution

  • You are effectively requesting from another domain.

    The request is coming from http://incomebrokers.com while you are loading http://www.incomebrokers.com (with www in it). Because the url with and without www are seen as different URL's you need to make sure to append www in both cases.

    There is another workaround. In your PHP file, place this right under your <?php tag.

    <?php header('Access-Control-Allow-Origin: *'); ?>
    

    This will allow requesting from 'other' domains.