how do i render twitter standard search api
reference:
https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
my code
require_once('TwitterAPIExchange.php');
$settings = array('oauth_access_token' => "XXX",'oauth_access_token_secret' => "XXX",'consumer_key' => "XXX",'consumer_secret' => "XXX");
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = "?q=#XXX from:XXX";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(),$assoc = TRUE);
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();}
var_dump($string);
it giving me response similar to the reference above
my problem is how to render it? I can't find any documentation/example about this?
like embedded time line and oEmbedAPI
reference:
https://developer.twitter.com/en/docs/twitter-for-websites/timelines/overview
https://developer.twitter.com/en/docs/twitter-for-websites/timelines/guides/oembed-api
Related
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.
Having read the The Twitter Search API as part of Twitter’s v1.1 REST API and searching here I am still stumped as to how to format the query.
K.Jack was able to get it working with a search for screen names and I thought I could use his code and adapt it but no such luck.
Here is code for what I have working using a static search term in the getfield area and including the form I am trying to incorporate.
I am searching for tweets. In K.Jack's issue he was using the search for screename and had his question answered with this snippet;
$getfield = "?screen_name=".$_GET['screen_name'];
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "MY_KEY_HERE",
'oauth_access_token_secret' => "MY_KEY_HERE",
'consumer_key' => "MY_KEY_HERE",
'consumer_secret' => "MY_KEY_HERE",
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=question&count=1';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
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();}
foreach($string as $items)
{
for($i = 0; $i < count($items); $i++) {
echo "". $items[$i]['text']. "<br />";
}
}
print ($_GET['question']);
?>
<h3>Enter the name here</h3>
<form action="" method="GET">
<input type="text" name="question" /><br />
<input type="submit" />
</form>
This is my first question here. I tend to muddle through learning by experimenting but I have spent too long on this with too little php knowledge and just had to ask.
thank you
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.
I want to print all the tweets from the reply. What am I missing?
https://github.com/J7mbo/twitter-api-php
<?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' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%23twitter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
foreach ($response->statuses as $tweet) {
echo $tweet->text;
}
Notice: Trying to get property of non-object in
/var/www/projects/twitter/twitter-api-php/index.php on line 23
Warning: Invalid argument supplied for foreach() in
/var/www/projects/twitter/twitter-api-php/index.php on line 23
The performRequest() method returns a JSON string, not an array, so you need to decode it $array = json_decode($response, true);.
You just have to use:
`echo $response;`
or instead:
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
but I recommend using the first approach.
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