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
Related
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
Here I have a json which i get after making a request to the twitter API. I have already seen different working examples of looping a json. Even I have do it in another projects. But this time it isn't working.
I've tested a lot of things. I guess it's normal it doesn't work for now. The variable arrayFriends isn't even being used. Just i was testing some moves.
Don't know exactly where I am failing. I would like to have some feedback
Thank you guys!
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?screen_name=J7mb0';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$arrayFriends = var_dump(json_decode($json, true, 512, JSON_BIGINT_AS_STRING));
$json = json_decode($json, true);
echo $json;
foreach($json->ids as $obj){
echo $obj->ids;
}
If you're using json_decode with true, the returned value will be an array, not an object.
Therefore, you won't be able to use the property-access method here:
foreach($json->ids as $obj){
You have to use the array-access method instead:
foreach($json['ids'] as $obj){
or, change the parameter at decoding, so you get an object:
$json = json_decode($json, false);
// or just simply:
// $json = json_decode($json);
// since the second parameter defaults to FALSE
ALSO, $arrayFriends will remain empty, because var_dump returns nothing at all.
// Change this:
// $arrayFriends = var_dump(json_decode($json, true, 512, JSON_BIGINT_AS_STRING));
// ...to this:
$arrayFriends = json_decode($json, true);
It worked! After trying different things it started working.
Actually I'm trying to access another json but this isn't working. It is the same app but with more content.
$user = 'seyroku';
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?screen_name='.$user.'';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$arrayFriends = json_decode($json, true,512,JSON_BIGINT_AS_STRING);
foreach($arrayFriends['ids'] as $obj){
echo $obj,",";
$url2 = 'https://api.twitter.com/1.1/users/lookup.json';
$getfield2 = '?user_id='.$obj.'';
$json2 = $twitter->setGetfield($getfield2)
->buildOauth($url2, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
echo $twitter->setGetfield($getfield2)
->buildOauth($url2, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$json3 = json_decode($json2, true);
foreach ($json3['name'] as $nombre) {
echo $nombre,",";
}
/* $sql = "INSERT INTO friendtest (user, friends)
VALUES ('$user' , '$obj' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}*/
}
The first for each it returns me the id properly. But the second one which I expect to give me the name of that id doesn't do it. I tried different things but still isn't working.
Could you give me some feedback?
Sorry guys for being so bad at this.
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 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