twitterlibphp - can't follow at all - php

This is driving me absolutely crazy. I can update my status using twitterlibphp and I can get followers, following etc but no matter what I do $o->follow(twitter_id) just will not work. It responds with
<?xml version="1.0" encoding="UTF-8"?>
<hash>
<request>/notifications/follow.xml</request>
<error>There was a problem following the specified user.</error>
</hash>
I've done some googleling, read through the library source and can't see what on earth I'm doing wrong. If you are in the london area I will probably buy you a pint if you know whats going on... The code is pretty simple:
$twitter = new Twitter($username, $password);
$xml = $twitter->getFriendIDs(array('user'=>$user));
$output = simplexml_load_string($xml);
foreach($output->id as $id) {
echo $twitter->follow($id);
}
(The $user var just comes from a textbox on a form - all that works fine and the foreach loops over the ID's)
But no dice. Anyone having the same issue or am I doing something silly? I've also tried running $twitter->verifyCredentials() beforehand but still the same unhelpful error. If i put in a random (non-existant) account then it tells me that that user was not found so I know its kinda working.
Cheers!

Well, I found the following tidbit after some more googling:
follow
Enables notifications for updates from
the specified user to the
authenticating user. This method
requires the authenticated user to
already be friends with the specified
user otherwise the error "there was a
problem following the specified user"
will be returned.
Hopefully calling $o->createFriendship($id) and then follow will work :) I'm amazed thats not in the examples or more prominent in the docs...

Related

Laravel - Problem Of How To Tune Strange Behavior With Sending Text Message

I have a very special problem and I don't know how to deal with it.
I have web App in Laravel, when i open index page, I receive text message to my mobile phone.
Problem is, sometimes I receive 2 messages or 3, sometimes 1.
Is there a tool how to debug this strange behavior which is not always the same?
A few words about my code:
user opens the page, and because its first visit Session doesn't have attribute message_sent and SendTextMessage::SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat); is executed. After that Session has message_sent and can't be sent again, for example if I refresh the page.
SendTextMessage::SendMessage() is Class in Laravel Helpers.
controller code:
public function index($url_attribute, $id_message, Request $request)
{
if(!Session::has('message_sent'))
{
$user = User::where('id_message', $id_message)->first()->toArray();
$phoneNumber = $user['mobile_phone'];
$smsCode = $user['sms_code'];
$newDateFormat = date("d.m.yy", strtotime($smsExpirationTime));
$request->session()->flash('message', 'Text message sended.' );
SendTextMessage::SendMessage($phoneNumber,$id_message, $smsCode, $newDateFormat);
Session::put('message_sent', true);
}
return view('login');
}
SendTextMessage Class:
class SendTextMessage
{
public static function SendMessage($phoneNumber, $id_message, $smsCode, $newDateFormat)
{
$sms = new Connect();
$sms->Create("user","pass",Connect::AUTH_PLAIN);
$sms->Send_SMS($phoneNumber,"Message");
$sms->Logout();
}
}
Many thanks for any tip or help.
UPDATE:
problem is only in Chrome.
Edge and internet explorer are fine.
As this script runs on server-side the browser shouldn't be an issue. Based on your code provided, there is no clear answer to give here.
Please try the following in order to debug your problem:
Log messages at each stage of the script in order to see which part was called how often. That will help you to locate the problem. You can use \Log::error("Message") to do that.
Once you know where the problem might be, try to log "decision" making / mission critical variables to logile as well. E.g. \Log::error($session) so that you can understand why that problem might occur. One reason could be that you have a bad configured session caching or your cookies might be messed up. At some point there is probably a piece of data not the way you expect it to be.
You should maybe try to change the way you use Laravel Session.
You indicated that it was working fine on some browsers, that means your server-side code is correct so far, but there is someting messing with Chrome… From there,
if you take a quick look at the Laravel Session doc, you'll see that Session can be stored in cookies, and I bet that this is your actual setup (check in your .env file the SESSION_DRIVER constant, or in your config/session.php file).
If so, to confirm that this cookies-based session setting is the culprit, you might want to change the Session config to make it browser-independent: any other option than cookies will work, the database or file options might be the easier to setup… And if it works I would strongly encourage you to keep using this no-cookie setting to make your code browser-safe.

Geocoding a specific address fails from PHP, works from browser using the same URL

I'm using this code snippet to geocode an address using Google Geolocation API:
<?php
$address = "Frazione Levà - 16030 Sori (GE)";
$url = 'http://maps.google.com/maps/api/geocode/xml?address='.urlencode($address).'&sensor=false';
echo $url."\n";
$xml = file_get_contents($url);
var_dump($xml);
?>
The echo command shows the URL called is:
http://maps.google.com/maps/api/geocode/xml?address=Frazione+Lev%C3%A0+-+16030+Sori+%28GE%29&sensor=false
and the var_dump command shows the result is:
string(107) "<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>ZERO_RESULTS</status>
</GeocodeResponse>
"
So it seems the address cannot be geolocated.
If I call exactly the same URL above ( http://maps.google.com/maps/api/geocode/xml?address=Frazione+Lev%C3%A0+-+16030+Sori+%28GE%29&sensor=false) from my browser, I get a totally different result (the address is correctly geolocated):
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
...
How this is possible and how I can fix it? I need to geolocate addresses from PHP.
Please note I'm using the same code snippet in a loop to geolocate hundreds of addresses every day, and it works fine on most of them; only on some addresses it shows issues like that.
Found the solution here https://stackoverflow.com/a/10302440/2717254 thread found thanks to the StackOverflow related questions suggested in the sidebar :-)
Issue was the Region Biasing. I added ?region=it to the URL and results are now coherent.
Before adding that param, probably Google guessed it me when I loaded the page in the web browser because of headers sent by my browsers (in italian Language on Win7 italian). When I make the request from PHP, less headers are sent and probably Google thinks this is an en-US PC or something similar... :)
Also removing the ZIP code from the address also gives me coherent result across my PHP script and my web browser, but I think the ?region=it is the most clean and logically correct solution.
In my case I solved it with language instead of region: language=it (https://developers.google.com/maps/faq#languagesupport)

Facebook GraphAPI via another webpage php?

First: please forgive me - Im a bit of a novice as some of this...
I have a working test site which is running the php facebook SDK to perform some simple graphAPI requests successfully. Namely read a group's feed, which the user is a member of, and process this and display it back on a webpage.
This all works fine, the problem I have encountered is when trying to perform the same request via a php curl POST to another webpage (on the same domain). It seems that the SDK does not carry the expected session to another page when a post request is formed (see "AUTH ERROR2" in code)...this works fine when the following file is included via a "require_once" but not when a curl is made.
I would much rather do a "curl" as Im finding when a "require_once" is done from a page in a different directory level, Im getting php errors of the page not being found - which is expected.
I may just be tackling this problem all wrong...there may be a simpler way to make sure when files are includes, their correct directly level remains intact, or there may be a way to send over the currently authorised facebook sdk session via a curl post. All of which I have tried to no avail, and I would really appreciate any help or advise on this.
Thank you for your time.
//readGroupPosts.inc.php
function readGroupPosts($postVars)
{
//$access_token = $postVars[0];
// ^-- I'm presuming I need this? I have been experimenting appending it to
// the graphAPI request to no success...
$groupID = $postVars[1];
$limit = $postVars[2];
require_once("authFb.inc.php"); //link to the facebookSDK & other stuff
if ($user) {
try {
$groupFeed = $facebook->api("/$groupID/feed?limit=$limit"); //limit=0 returns all;
$groupFeed = $groupFeed['data']; //removes first tier of array for simpler access
$postArray;
for($i=0; $i<count($groupFeed); $i++)
{
$postArray[$i] = array($groupFeed[$i]['from']['name'], $groupFeed[$i]['message'], $groupFeed[$i]['updated_time'], count($groupFeed[$i]['likes']['data']));
}
return $postArray;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
return "AUTH ERROR1"; //for testing..
}
}
else
{
return "AUTH ERROR2"; //no user is authenticated i.e. $user == null..
}
}
I would much rather do a "curl" as Im finding when a "require_once" is done from a page in a different directory level, Im getting php errors of the page not being found - which is expected.
I may just be tackling this problem all wrong...
Definitively.
Using cURL as a “workaround” just because you’re not able to find your way around your server’s file system is an outrageous idea. Don’t do it. Stop even thinking about it. Now.
there may be a simpler way to make sure when files are includes, their correct directly level remains intact
Yes – for example, to use absolute paths instead of relative ones. Prefixing the path with the value of $_SERVER['DOCUMENT_ROOT'] for example – that way, once you’ve given the path correctly in respect to this “base path”, it does not matter where you’re requiring the file from, because an absolute path is the same no matter from where you look at it.
(And since this is not a Facebook-related problem at all, but just concerns basics of PHP and server-side programming, I’ll edit the tags.)

Getting a facebook name out of a fb id - file_get_contents() not working

Background:
I am making a facebook app where users post messages like in a forum. For that I save the users facebook id to then present their names. I know it is possible to show the name out of a facebook id by using the Graph API, but I can't make it work.
The code I use is the following:
//$fbId is the facebook id to find out the name for
$facebookUrl = "https://graph.facebook.com/".$fbId;
echo(file_get_contents($facebookUrl));
$str = file_get_contents($facebookUrl);
$result = json_decode($str);
echo($result);
return $result->name;
I researched and tested this for hours, but I feel like I'm getting nowhere. I got an idea of how it works from:
http://www.phpexpertsforum.com/how-to-get-the-facebook-name-with-user-id-using-php-code-t1852.html
Didn't work so I researched further from here:
Get user's name from Facebook Graph API
I dowloaded and currently use the Facebook php SDK, but I don't find the way to use it to get anyone's name other than the current user.
The problem here seems to be the file_get_contents() function that returns a false, meaning that it can't read the file.
I checked out the php documentation. Also, by using the php functions fopen() and file().
//echo(pathinfo($facebookUrl, PATHINFO_EXTENSION)); -> Doesn't give any response!
//echo(file_exists($facebookUrl)); -> No response!
//fopen() doesn't work
//file() doesn't work
//file_get_contents() doesn't work
//tried adding an access token to the $facebookUrl but it doesn't make any difference
Any idea of what could be wrong here? Little guidance would be very helpful. What am I doing wrong? What did I miss?
Seems that your allow_url_fopen directive is set to Off.
Look for it in your php.ini (or create a php file containing <?php phpinfo(); ?> and point to it in your browser) and check if is set to 'On'.

Get Tweets from EpiTwitterJson

I've been banging my head against the wall for a few days now trying to do something that should be so simple. I am trying to use Jaison Mathai's EpiTwitter library to retrieve tweets from my account. Here is my code to get my tweets:
<?php $twitterObj = new EpiTwitter($__TWITTER['ConsumerKey'], $__TWITTER['ConsumerSecret'],$__TWITTER['MyAccessToken'],$__TWITTER['MyAccessTokenSecret']);
$twitterInfo= $twitterObj->get_accountVerify_credentials();
$twitterInfo->response;
// ^ This part works fine, I can get my user info and profile pic and whatnot
$username = $twitterInfo->screen_name;
$tjson = $twitterObj->get('statuses/user_timeline.json', array("screen_name"=>$username));
As far as I can tell, ^ this also works as it should. If I var_dump($tjson), I can see the tweets that I want to access, among a HUGE mess of other data.
My question boils down to this: from this point, how can i simply print out each tweet returned? Here's what I've tried so far:
foreach($tjson as $tweet) print($tweet)
//^ Throws IllegalArgumentException, "Not an array or object"
foreach($tjson->responseText as $tweet) print($tweet)
//^ Warning: illegal argument. Doesn't print anything
print_r($tjson->response) //Prints NULL
I thought $tjson->response would be what I want, because in the __get method in EpiTwitterJson, there is a line:
$this->response = json_decode($this->responseText, 1);. Right after this line if I var_dump($this->response), I get my tweets along with a little less data than var_dump($tjson) before. If on that same line I print($this->response['status']['text']), I get a nice string of my tweet, which is exactly what I want.
What I don't quite understand is why $this->response is set inside of the EpiTwitterJson class, but $tjson->response is NULL in my code. I've looked and looked and looked but I can't seem to find any code snippets to get tweets with this library specifically. I think it's just one of those things that must be so obvious and simple that my brain refuses to accept it.
Obviously I could just crawl through $tjson->responseText, but I thought the whole point of the EpiTwitterJson class was so I would not have to do that? The ideal answer to my question will be code to boil down my $tjson object so i can say
foreach(something as $tweet) //do stuff with the tweet
Second best answer would be code to do this with another library. I only chose the EpiTwitter lib because it 'supports async' which sounds kinda cool if I ever decide to use it, and because it seemed like the simplest (ie least lines of code on my part). Quite frankly I really care more about making this work than understanding it; I can figure out what it all means later on.
foreach($tjson->response as $post){
echo '<pre>';
var_dump($post);
echo '</pre>';
}
this works for me.
for eg. u can get id of post like that $post['id'], or user name $post['user']['name']

Categories