Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a PHP script that displays Tweets embedded in a site I built out on my local machine. When I uploaded the site to my IIS 8.0 server, the PHP script no longer works and I receive this error:

Warning: Invalid argument supplied for foreach() in C:\inetpub\wwwroot\i360_new\footer.php on line 76

The script is:

<?php
    ini_set('display_errors', 1);
    require_once('TwitterAPIExchange.php');

    /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
    $settings = array(
        'oauth_access_token' => "xxxxx",
        'oauth_access_token_secret' => "xxxxx",
        'consumer_key' => "xxxxx",
        'consumer_secret' => "xxxxx"
    );


    /** Perform a GET request and echo the response **/
    /** Note: Set the GET field BEFORE calling buildOauth(); **/
    $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    $getfield = '?screen_name=interactive360&count=1';
    $requestMethod = 'GET';
    $twitter = new TwitterAPIExchange($settings);
    $string = json_decode($twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(),$assoc = TRUE);
    foreach($string as $items)
        {
            echo $items['text']."<br />";
        }
?>

I thought it might be a PHP version issue but my local machine is running 5.4.10 and my server is running 5.4.14.

share|improve this question
1  
Which line is line 76? "Warning" is not an error -- "warning" can often be completely ignored. Put a "print_r($string)" lin in just before the foreach() to see what is actually in that sucker –  JDS Sep 30 '13 at 14:45
    
$string is actually returning empty which is causing the error. I don't know why it's returning empty though. –  user715564 Sep 30 '13 at 16:08
add comment

3 Answers 3

Did you compare both servers phpinfo()? While running in almost the same environment each server can work differently depending on which modules are installed.

share|improve this answer
    
The only comparison I did was the version number. Is there something else I should specifically be looking for? They also both have JSON 1.2.1 enabled. –  user715564 Sep 30 '13 at 14:33
add comment

You might want to make sure that the $string isn't empty. I believe a ForEach will throw this error if you provide a string that is empty. It looks like the string depends on TwitterAPIExchange.php, I see the 'Require Once', but ensure it's configured correctly.

share|improve this answer
    
Actually, it looks like you are correct. The $string variable is returning empty. I looked through TwitterAPIExchange.php and the only thing that I noticed might be the problem is that is uses CURL. My IIS server has CURL enabled though. –  user715564 Sep 30 '13 at 14:50
1  
Could this possibly have something to do with user permissions on the IIS side? –  user715564 Sep 30 '13 at 15:11
    
Have you checked to see what $twitter has in it? I'm assuming that $settings hasn't changed since you copied it over to production, but might wanna check that as well. –  Noobixide Sep 30 '13 at 15:43
    
Ok so when I echo $twitter I get this error: Catchable fatal error: Object of class TwitterAPIExchange could not be converted to string in C:\inetpub\wwwroot\i360_new\footer.php on line 80 –  user715564 Sep 30 '13 at 15:51
1  
Don't do "echo" do "print_r( $twitter );" -- you can't "echo" an Object –  JDS Sep 30 '13 at 16:15
add comment

It turns out this was an SSL/CURL issue. In the TwitterAPIExchange.php file, I had to add these two lines to the CURL options array:

CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.