2 Replies - 444 Views - Last Post: 29 January 2013 - 06:15 PM Rate Topic: -----

#1 kaed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 27-January 13

PHP Function works on one site but not another

Posted 29 January 2013 - 05:44 PM

Okay so I recently developed a wp childtheme for a website and in doing so I made a small function to grab the latest tweets:
<?php

//get tweets
function getTwitterStatus($userid, $count){
	echo '<div class="latestTweets"><h3>Latest Tweets</h3>';
    $format='json'; // set format
    $tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$userid}.{$format}")); // get tweets and decode them into a variable
    $i = 1;
	echo '<ul>';
	while ($i <= $count) {
		echo '<li><a href="http://twitter.com/' . $userid . '">' . $tweet[$i]->text . '</a></li>'; // show latest tweet
		$i++;
	}
	echo '</ul></div><!-- .latestTweets -->';
 }
?>


This function works perfectly fine on my site (it's in the footer): http://codepier.com/pvsite/
But then when it loads on the site I need it to work on (http://pvhornetsnest.com/) I get the following output:
Warning: file_get_contents(http://api.twitter.com/1/statuses/user_timeline/pvei_ibpf.json) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/content/19/10381319/html/wp-content/themes/pvtheme/functions.php on line 233


Why would it work on one site but not the other? Could it be the server? My site (codepier) is hosted with HostGator and the other (PVhornetsnest) is hosted with GoDaddy.

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Function works on one site but not another

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,613
  • Joined: 08-August 08

Re: PHP Function works on one site but not another

Posted 29 January 2013 - 06:00 PM

Reading the manual should be your first step:
http://php.net/manua...et-contents.php
Note the version that function requires and see what your version is.
Was This Post Helpful? 0
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,209
  • Joined: 19-January 10

Re: PHP Function works on one site but not another

Posted 29 January 2013 - 06:15 PM

file_get_contents and json_decode were implemented in PHP 5 and PHP 5.2.0 respectively, make sure your server is running that or later.

I also believe there is a php.ini value that, when enabled, will now allow file_get_contents to work on external addresses.

Ahh you know what, I would venture to guess it's with that API reducing the amount of calls you can make. You can use CURL to detect when the HTTP 400 error code is returned so you can appropriately handle it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1