Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Twitter JSON occasionally returns 400?


  • Please log in to reply
2 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:53 AM

Posted 10 August 2010 - 07:38 AM

Hi, I am working on a site at the moment that uses PHP to fetch the JSON from Twitter, this works beautifully, most of the time. Now and again it will start giving me 400 errors and warnings about file_get_contents(). At first I thought it was down to a limit of requests per hour, after making a cache script that only downloaded from Twitter after five minuits I gave it a go, but still, 400.

I am not really sure what could be causing this, Twitters end, or mine?

Here is the main section of the class, if there is a problem it will lie here, If anyone has any ideas, please let me know.

Thanks, Wolfy87.

public $cache_name = 'rathtwitter_cache';
		public $content = 'LOADING';
		public $url = 'LOADING';
		public $postDate = 'LOADING';
		
		function load($user)
		{
			if(file_exists($this->cache_name))
			{
				if(time() - filemtime($this->cache_name) > 300)
				{
					$this->cache($user);
				}
				else
				{
					$this->loadCache();
				}
			}
			else
			{
				$this->cache($user);
			}
			
			return true;
		}
		
		function cache($user)
		{
			$json = json_decode(file_get_contents('http://twitter.com/statuses/user_timeline/' . $user . '.json'));
			$this->content = $json[0]->text;
			$this->url = 'http://twitter.com/' . $user . '/status/' . $json[0]->id;
			$this->postDate = 'Posted ' . $this->fuzzy_span(strtotime($json[0]->created_at));
			$cache_content = json_encode(array('content' => $this->content, 'url' => $this->url, 'postDate' => $this->postDate));

			$fh = fopen($this->cache_name, 'w');
			fwrite($fh, $cache_content);
			fclose($fh);
		}
		
		function loadCache()
		{
			$json = json_decode(file_get_contents($this->cache_name));
			$this->content = $json->content;
			$this->url = $json->url;
			$this->postDate = $json->postDate;
		}


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:03:53 AM

Posted 11 August 2010 - 06:12 AM

...and when you looked up a 400 server error, what did you find?

#3 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:53 AM

Posted 11 August 2010 - 07:28 AM

I know that 400 is bad request, but it only happens sometimes without changing anything. Anyway, we have fixed it by using the twitter search rather than obtaining the JSON direcly, for some reason the error does not occor now.

I think it must have been down the the hourly limit of requests.

Thanks anyway.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users