0

I am trying to get a twitter feed working on my website. However whenever I load my page I get the following error:

XMLHttpRequest cannot load http://www.jciadt.com/demos/twitter/get_tweets.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://jciadt.com' is therefore not allowed access.

The page in question can be found here -> http://jciadt.com/demos/twitter/testindex.html

I don't know why this is happening. Any help would be greatly appreciated.

<?php

session_start();

require_once('http://jciadt.com/demos/twitter/twitteroauth/twitteroauth/twitteroauth.php');



$twitteruser = "jackcoldrick";

$notweets = 30;

$consumerkey="xxxxxxx";

$consumersecret="xxxxxxx";

$accesstoken="xxxxxxx-xxxxxxx";

$accesstokensecret="xxxxxxx";



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



?>
2
  • I don't think so. Based on the URLs, I don't think this really NEEDS to be a cross-domain request. On the other hand, if accessing the HTML and the PHP through the same subdomain doesn't work for some other reason, and the 500 error I got below is unresolvable without the www distinction, then using a server side script to make the request for you is the most commonly used option. Commented Feb 11, 2014 at 23:50
  • 1
    I would hope that Twitter's API was at least a little bit communicative, but chances are that either the instantiation or the get is throwing an exception. Do you have display_errors turned on? Do you have any exceptions being logged? Double check all your variables and make sure your tokens and such are right and you're passing them in the right order, all that...I'm not familiar with Twitter, tbh...I've just started poking around the Google APIs. Commented Feb 12, 2014 at 0:03

1 Answer 1

0

You're trying to make a cross domain request to a resource that has disallowed it. If you run your script from this link instead, what happens? The www in the address puts you on a different subdomain and will interfere with XHRs. When I try it via the www prefaced link, I get a 500 error from your script instead of the XHR failure error.

Sign up to request clarification or add additional context in comments.

3 Comments

I also get this error now.
At this point you have to debug your PHP. I don't have access to your error logs or your source code to deduce the source of the 500. Add them to your answer (at least the PHP src) and I'll take a look...
I added the script that seems to be generating the 500 error but cant see why it might be generating the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.