1

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);
Javacadabra
  • 5,578
  • 15
  • 84
  • 152

1 Answers1

1

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.

edwardmp
  • 6,339
  • 5
  • 50
  • 77
  • Excellent, problem solved thank you very much. Will accept answer in 5 mins! – Javacadabra May 29 '13 at 21:56
  • Strangely though this problem only began tonight, up until a few hours ago it was working perfectly – Javacadabra May 29 '13 at 21:57
  • 1
    Check the accepted answer on this page on why these kind of errors occur http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work – edwardmp May 29 '13 at 21:58
  • 1
    another alternative would be just to source the url as '/js/get_tweets.php' then it won't matter. – Orangepill May 29 '13 at 22:23