PHP script works on localhost but not IIS server - php

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.

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

Related

Api twitter response no statuses

I'm trying to display tweets for a specific hashtag. The code below works perfectly what ever the hashtag is, but when i try with the specific one, it doesn't work. For the last month, it worked perfectly with the right hashtag but since yesterday it's broken. (i didn't put my tokens in the code)
require_once('twitter-api-php-master/TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "my_tokens",
'oauth_access_token_secret' => "my_tokens",
'consumer_key' => "my_tokens",
'consumer_secret' => "my_tokens"
);
$hashtag = 'smartspend_eu';
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = "?q=#$hashtag";
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
The response is :
{"statuses":[],"search_metadata":{"completed_in":0.053,"max_id":1105019731759656961,"max_id_str":"1105019731759656961","query":"%23SMARTSPEND_EU","refresh_url":"?since_id=1105019731759656961&q=%23SMARTSPEND_EU&include_entities=1","count":15,"since_id":0,"since_id_str":"0"}}
What is the problem, and what can i do to fix it ?
I finaly found what's wrong. My code was right from the begining. The problem is that it's impossible to get the tweets from more than 7 days ago. And no tweets has been written for my hashtag for the last 7 days.

Require once/include not finding correct file laravel

Require once/include in my laravel project is not functioning properly. I am using the correct path however i continue to get the same error. I can see the file is in the designated space. What could cause this? Here is my code:
#extends('apps.admin')
#section('main')
<?php
include(app_path().'vendor/j7mbo/twitter-api-php/TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "token",
'oauth_access_token_secret' => "token",
'consumer_key' => "token",
'consumer_secret' => "token"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#nerd';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
?>
#stop
I also tried this:
require_once('vendor/j7mbo/TwitterAPIExchange.php');
and this:
require_once('TwitterAPIExchange.php');
errors:
include(/var/www/web/elephantegin/htdocs/app/vendor/j7mbo/twitter-api-php/TwitterAPIExchange.php): failed to open stream: No such file or directory (View: /var/www/web/elephantegin/htdocs/resources/views/socialApps/twitter.blade.php)
errror:
main(): Failed opening required '/vendor/j7mbo/TwitterAPIExchange.php' (include_path='.:/usr/share/php:/var/www/web/elephantegin/htdocs')
You are referencing the file incorrectly, use the correct path:
include(app_path().'../vendor/j7mbo/twitter-api-php/TwitterAPIExchange.php');

How to display an image in PHP given the url of the image? (PHP NOOB)

So what I'm doing is using the Twitter RestAPI to get the picture of a user and just be able to display the image on a local page. I've figured out the way to grab the URL of the image and this is what I have so far:
$url = $result->profile_image_url;
This holds the url of the profile picture of the user. Now how do I use this url to display it on the page? I know HTML allows us to do something like :
<img src = "some url">
And it will display the picture on the web page. I've looked around the internet for a while now, and still haven't figured it out. I've run into the same suggestion of the following :
<img src = "<?php echo ($url); ?>"/>
But I keep getting a parse error when I attempt loading the page. I would really appreciate if someone could guide me/help me figure out what's going on. Clearly, I'm doing something wrong which is why the PHP interpreter is not able to parse the code. Thanks!
Here is my whole code (if that helps):
<html>
<body>
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "N/A",
'oauth_access_token_secret' => "N/A",
'consumer_key' => "N/A",
'consumer_secret' => "N/A"
);
$url = 'https://api.twitter.com/1.1/users/show.json';
$getfield = '?screen_name=kobebryant';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$result = json_decode($json);
$url = $result->profile_image_url;
<img src = "<?php echo ($url);?>"/> //This is the line giving me an error
?>
</body>
</html>
You closed the php tag too late. That is why it is recommended to split php from html. Try this code below:
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => 'N/A',
'oauth_access_token_secret' => 'N/A',
'consumer_key' => 'N/A',
'consumer_secret' => 'N/A'
);
$url = 'https://api.twitter.com/1.1/users/show.json';
$getfield = '?screen_name=kobebryant';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$result = json_decode($json);
$url = $result->profile_image_url;
?>
<html>
<head></head>
<body>
<img src="<?php echo ($url);?>"/> //This is the line giving me an error
</body>
</html>
Hint: if you've just a string without variables use ' instead of " because php tries to parse content between " and this is waste of CPU power.

TwitterAPIExchange suddenly stops working

So I'm making a Twitter fetch website where all the tweets from a specific hashtag will be fetched in real-time with ajax. It all works fine, I thought, until it all suddenly stops working. If I then wait for 3 minutes or so, it then starts working again. PHP is giving me errors such as
Notice: Trying to get property of non-object...
The code I'm running:
require_once '../lib/TwitterAPIExchange.php';
$settings = array(
'oauth_access_token' => "...",
'oauth_access_token_secret' => "...",
'consumer_key' => "...",
'consumer_secret' => "..."
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=#hashtag&result_type=recent';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
foreach(json_decode($response) as $result){
foreach($result as $post){
echo '<div class="post"><div class="header"><img src="'.$post->user->profile_image_url.'"><p class="user">'.$post->user->name.'</p></div><div class="text">'.$post->text.'</div></div>';
}
break;
}
I figured it out with help from #Uwe Allner. It turned out the Twitter API has something called API Rate Limits. So, as you may guess, they are limiting the amount of requests per x time. Under "Search", I noticed they have set a limit at 180 queries per 15 minutes. So I can make a query each 5 seconds.
180/15 = 12
60/12 = 5

PHP (Xampp vs Mamp)

I am new as a programer in PHP, I have Macbook Pro so I have installed mamp for it and it all worked well while now I am using xampp and same program not working in xampp.
Program is as follows:
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "47w756hhd7jBU0yXqOfeJQKlgXYD",
'oauth_access_token_secret' => "YjhdfhUE1hYaNu5E4IiU0gZnqt1kp5nSUy1lP",
'consumer_key' => "878bdfdffhEOXx5AdJhpelO9ZNStb",
'consumer_secret' => "jhfhrhXjJ867vvIRSwsI6CJuhUIEYoj0iGHGNpIPkXJ3lcTP9W"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=#varanasi&result_type=recent';
//this code converts json code into simple string object
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
echo $string;
if($string["errors"][0]["message"] != "") {
echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";
exit();
}
echo "<pre>";
print_r($string);
echo "</pre>";
if(empty($string))
{
echo "hello there is nothing";
}
else
{
echo "hello everything:";
}
?>
Now the $string is showing empty data in xampp while in mamp it is all working well.
I've solved this by editing TwitterAPIExchange.php file.
Just adding
CURLOPT_SSL_VERIFYPEER => false
at the $options array at the performRequest method

Categories