Get Tweets from EpiTwitterJson - php

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']

Related

PHP Variables not passing to url correctly from API

I'm trying to use the Premium URL Shortener script from codecanyon, I have asked for support but they seem to be a little busy, so the response time is not to quick.
The issue I have is when the API sends the request to the url shortener script with the following shortened query for example purposes:
$short = "http://myurl/api?api=MYAPI&format=text&url=http://myfullwebsite.com/email/quote.php?fullname=$fullname&address=$address&emailaddress=$emailaddress";
Although the variables are being placed in the script correctly using echo function at the end of the script after the api request is sent shows they are correctly inserted like so:
http://myurl/api?api=MYAPI&format=text&url=http://myfullwebsite.com/email/quote.php?fullname=Dan Smith&address=12 Main Street, London&emailaddress=dan#smith.com
However if I click the shortened url provided to me from the script I only get the following url string appear in the browser:
http://myfullwebsite.com/email/quote.php?fullname=Dan
It seems as soon as there is a space or even if there is no second name such as Dan Smith and only Dan is the available name, it will not even apply the second ampersand or & sign.
I have tried to use urlecode() but still no joy and I've been pulling my hair out for the last 3 days!
As a novice beginner it has been somewhat difficult to try and achieve the end result and it seems unreachable so I would appreciate any kind help or advice if possible, Maybe I'm missing something so simple?
I've thought of having the url query build from an array of variables but as a novice I've tried one way and failed so not sure if I have done it wrong.
Here is my full api code where I have tried both with SESSION and GET but that is not the problem as the end result echos to the browser with the variables there.. it's only when you follow the shortened url link that you see they're missing.
<?php
session_start();
$fullname = htmlspecialchars($_GET["fullname"]);
$address = htmlspecialchars($_GET["address"]);
$postcode = htmlspecialchars($_GET["postcode"]);
$emailaddress = htmlspecialchars($_GET["emailaddress"]);
$short = "http://myurl/api?api=MYAPI&format=text&url=http://ukhomesurveys.co.uk/email/quote.php?fullname=$fullname&address=$address&emailaddress=$emailaddress";
echo $short;
// Using Plain Text Response
$api_url = $short;
$res= #file_get_contents($api_url);
if($res){
echo $res;
}
?>
Hope I covered everything and hope I have not confused anyone. Thanks.
I think the good choice here is to encode your query with base64 and then pass it to the shortener. In your http://myfullwebsite.com/email/quote.php you just decode the query and use it. The standart PHP functions are base64_encode and base64_decode.
Did you try to encode URI using rawurlencode?
$url = rawurlencode('http://myfullwebsite.com/email/quote.php?fullname=Dan Smith&address=12 Main Street, London&emailaddress=dan#smith.com');

How to access result set in phpDataMapper?

I started learning this very nice PHP ORM api last week: http://phpdatamapper.com/
and have been trying to get up to speed with it
What I'm not seeing in the site documentation, is how to iterate with "$postMapper->all()"?
http://phpdatamapper.com/documentation/usage/finders/
When I try to iterate through the value returned from ->all(), it seems only to have gotten the last element in the table.
Here's the code I have:
// $postMapper uses phpDataMapper framework. It works to create the schema & insert values
$postEntities = $postMapper->all();
$postEntities->execute(); // tried adding this to help things
foreach ( $postEntities as $postEntity);
{
echo $postEntity->title;
echo "<br/>";
}
I see other folks are forking it from GitHub and using it in their projects, so I believe I'm making some mistake in my call logic.
It would be great if someone could share a small example of how to access the query data correctly when using mapper->all()? This is an important part of a PHP stack and I would very much like to be able to use this particular solution in my projects going forward. Thanks
Ok, this turned out to be a "not so smart" mistake on my part. The font in my IDE was too small I guess, and I didn't see the ';' at the end of the line that declares the for loop.
So the ORM is all good, just my code wasn't. I'm updating this in case someone else can learn from my mistake - other option is to just delete the post all together (... not sure which is better)

mysql fetch array returns false in function but not in the document

i am creating a CMS and have php creating a page. i have a while loop like this
while($row = mysql_fetch_array($results)) {
echo "some html code" . $row['name'];
its shortend but hopefully you get the point. i have the full thing in my page working just as it should and i wanted to move it to a function include as i want to reuse it. the problem is i do that and it stops working.
i did some testing and found that the function is getting the query result and after doing a var dump both were identical the problem comes when i try to assign it to an array. it comes back as false so in the above code, for example,
$row = false;
im toatly lost in this and if my explanation is confusing i appologise but i am a bit of a newbie i have tried searching but....i dont really know where to begin
any thoughts.
the query you are doing is basically wrong, try posting exactly the code which you have in $query and then let us see the problem.
also, it is better to use mysqli functions.
but for this, edit the question and type the query, or simply put a die(mysql_error()) at the end of your query which is in $query. It will show your exact error.
i fugured it out
when i was testing the function i commented out the original code on the main page but for some reason i had not comented out enough (it was a mix of php and html clearly the php had not been commented out properly) this must have been causing a clash of some kind as when i put the function above the code on my page the function worked and the long code below it did not
sorry for wasting your time guys

Parse Google Images API Json PHP

Hey, well I'm trying to use google images api with PHP, and I'm really not sure what to do.
This is basically what I have right now:
$jsonurl = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=test";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
Where would I go from there to retrieve the first image url?
With a minor change to the last line of your code sample, the following will output the url of the first image in the result set.
<?php
$jsrc = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=test";
$json = file_get_contents($jsrc);
$jset = json_decode($json, true);
echo $jset["responseData"]["results"][0]["url"];
?>
For security reasons, most server configurations won't let you use file_get_contents on a remote file (different domain name). It would potentially allow a hacker to load code from anywhere on the Internet to your site, then execute it.
Even if your server configuration does allow for it, then I wouldn't recommend using it for this purpose. The standard tool for retrieving remote HTTP data is cURL, and there are plenty of good tutorials out there doing exactly what you should do in this case.
So, let's say you've successfully used cURL to retrieve the JSON array.
$json_output = json_decode($json); // Now the JSON is an associative array
foreach ($json_output['responseData']['results'] as $result)
{
echo $result['url'] . '<br />';
}
Of course, you don't have to echo the URL there; you can do whatever you need to with the value.
I have to say, this is 10 shades of awesome.. But I come with bad news (don't shoot the messenger..)
Important: The Google Image Search API has been officially deprecated as of May 26, 2011. It will continue to work as per our deprecation policy, but the number of requests you may make per day may be limited.
That is, as they same, lame.
I feel as if Google might have hired one-too-many layed-off-from-IBM-types... as they seem to be killing off all their "cool" API's.
They launch services haphazardly, promising this and that and the other thing... but then some middle-manager gets screamed at after realizing (ta-da!) that XYZ project doesn't generate income (like image results without ads, lol) and then... they axe it..
Lesson: Don't get married (aka build your software or service) around any google API you can't replace on-the-fly at a moment's notice... Now, I'm no LTS-junkie - I'm just bitter because I'd much rather get my Google results via XML or JSON than the icky HTML-soup they throw at you...
One Question #Marcel... How can I get an array, or at least multiple JSON result responses using that same "formula". I thought maybe the 1 meant "result 1", but alas, no... Is their a "trick" to generate a content stream ala a Picasa xml feed?

twitterlibphp - can't follow at all

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...

Categories