How do I get phpFlickr to return the original image? - api

This code works perfectly:
$f = new phpFlickr(FLICKR_API_KEY, FLICKR_API_SECRET);
$f->setToken(FLICKR_AUTH_TOKEN);
// Next line is just WordPress providing the photoset ID.
$mySetID = get_post_meta($post->ID, 'Flickr set ID', true);
$mySet = $f->photosets_getPhotos($mySetID, NULL, NULL);
foreach ($mySet['photoset']['photo'] as $photo) {
echo '<div><img src="'. $f->buildPhotoURL($photo, 'large') .'" alt="" /></div>';
}
...until buildPhotoURL is told to fetch the "original" size, at which point the URL returned is something like "http://farm6.static.flickr.com/5607/5332878962__o." which is obviously not valid.
While everything I've found through searches seems to agree that doing this requires some "originalsecret" and "originalformat" values that are barely mentioned in Flickr's own documentation, and phpFlickr does seem to try and use them, they're clearly not being fetched by default and I've yet to see someone post code for how to actually provide them. I've tried calling $f->photos_getInfo() just before the echo line, passing in various things to no effect and I'm starting to feel like I'm missing something everyone thinks is obvious, even though nobody has ever produced a valid response(that I can find) to repeated questions about it on the phpFlickr forum.
Note:
This is a Pro account.
We are authenticating properly(these are private sets and they work fine for all other sizes).
Access to the original size in Flickr's privacy settings is set to "Anyone."
Ideas?

I know it's a little late and I'm sure you've figured out the problem by now, but I wanted to share a related answer, because I just spent some time wrestling with a similar issue, and as you said, answers on this subject can't be found anywhere.
To get the information needed to display original size images from a set, the following url should work:
http://api.flickr.com/services/rest/?&method=flickr.photosets.getPhotos&api_key=YOURAPIKEYNUMBER&photoset_id=YOURSETIDNUMBER&per_page=100&sort=date-posted-desc&extras=original_format&format=json&jsoncallback=?
Of course all of the variables are customizable to your needs, but that's just one example which shows the first 100 images in a set sorted descending by date with the original image formatting information attached including the important "original" secret code
To display the images the javascript you can use is
'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.originalsecret + '_' + 'o' + '.' + item.originalformat;
The main sticking point is "&extras=original_format" because that gives you both the originalsecret and originalformat which you need to call an original size image from flickr. I was using the SuperSized background plugin when I ran into the original size problem.

The photosets_getPhotos() method doesn't return the full array for $photo. It returns some basic info about $photo but not $photo['originalformat'], which is needed by buildPhotoURL().
You can tell photosets_getPhotos() to return the original format as an optional extra. This gives buildPhotoURL() everything it needs.
Example:
$mySet = $f->photosets_getPhotos($mySetID, 'original_format', NULL);
foreach ($mySet['photoset']['photo'] as $photo) {
echo '<div><img src="'. $f->buildPhotoURL($photo, 'original') .'" alt="" /></div>';
}
Even better, you can have photosets_getPhotos() return the URL for the original photo itself.
Example:
$mySet = $f->photosets_getPhotos($mySetID, 'url_o', NULL);
foreach ($mySet['photoset']['photo'] as $photo) {
echo '<div><img src="'. $photo['url_o'] .'" alt="" /></div>';
}

Related

Parse user input for command arguments into array

I'm making a bot in PHP and I want a better way to parse user input into arguments for later operations.
An example would be a user saying "/addresponse -"test" -"works""
I want this to parse that string into:
$command ["test", "works"];
I have found the PHP command parser but I want the user to be able to use human readable commands rather than typing something like /addresponse?p="test"&r="works"
Right now I have a regex working so the user can type "/addresponse "test" "works"" but there are obvious problems because the user cannot make a response for '"test"' only 'test'
I'd appreciate any help, right now I think I can make a regex to get all text between ' -' but I still don't think this is the best solution.
I just looked into using a regex to find text between ' -"' and while this is better than just between quotes, it doesn't solve the whole problem because it still will break if the input contains ' -"'. A string containing this isn't particularly common but I'd like a solution where almost any input will not break it.
Is this a stupid question? I don't think there is a built in php function for this and it got downvoted with no comment...
I found a partial solution:
function parse_cmd($command) {
$command = explode(' -"', $command);
array_splice($command, 0, 1);
foreach($command as &$element) {
$element = substr($element, 0, strlen($element) -1);
}
return $command;
}
This will split everything after ' -"' and return it as an array

passing msqli to a function - can't suss out why it's not working

I've searched high and low for an answer on this, but I'm either missing something, or I just can't find anything in this context.
Background - trying to avoid spaghetti frenzy with a little casual project I'm starting; part of this will involve mainly just calls to a mysql database, displaying table content and so on. Simply put, like a CRM I guess.
I may be way off base here, but I want to be able to create my sql calls as functions, which will help if/when I tweak and tune, as well as creating a cleaner code for what I'm looking to do.
So, without further ado, I have this as a demomstration:
echo "<table>";
selectall('actions','content',$mysqli);
echo "</table><br><br>";
What this does is show all rows from my table of 'actions. "content" is just an example field name I'm passing through that I want to display, as it is the main human-relevant field name in that table. I'm also passing $mysqli through here for my function db call.
My function looks like this:
function selectall($s_table,$s_content,$mysqli){
$query = "SELECT * FROM " . $s_table;
$resource = $mysqli->query($query);
if ( !$resource ) throw new Exception($db->error);
while ( $row = $resource->fetch_assoc() ) {
$id = $row['id'];
echo "<tr><td>{$row[$s_content]}</td></tr>";
}
$resource->free();
$mysqli->close();
}
However.... it doesn't work, and it seems to throw a wobbly saying:
Warning: mysqli::query(): Couldn't fetch mysqli
This points to the action within the line $resource = $mysqli->query($query);
I know the function and everything is ok, as if I restate and declare $mysqli within the first line of the function, like so...
$mysqli = new mysqli(username password and so on in here);
... it works spot on.
$mysqli exists and works within the same code that is passing the variable within the function too.
This is early stages, so by shuffling the code around trying to poke the $mysqli pass into life I have perhaps made the code a little messier that intended, so try not to worry too much about that.
Anyone any ideas why it doesn't like this?
D'oh...
I had a
$mysqli->close();
in the lines above. Solved myself.
For reference, this is my function:
function selectall($s_table,$s_field,$mysqli){
if ($mysqli->connect_error) {die('Connect Error (' . $mysqli->connect_errno . ') '. $mysqli->connect_error);}
$s_table = preg_replace('/[^0-9a-zA-Z_]/', '', $s_table); // Cleans up the table name variable
$s_field = preg_replace('/[^0-9a-zA-Z_]/', '', $s_field); // Cleans up the field name variable
$query = "SELECT * FROM " . $s_table; // Adds passed table name to the select all
$resource = $mysqli->query($query);
if ( !$resource ) throw new Exception($db->error);
while ( $row = $resource->fetch_assoc() ) {
echo "<tr><td>{$row[$s_field]}</td></tr>"; // Content for each row of the select all
}
$resource->free();
$mysqli->close();
}
As you can see, I've also tried to protect the variables that enter the function.
This can be called via:
selectall('actions','content',$mysqli);
In this context, I want to view all the entries in the 'actions' table by the field name 'content'. This function, with some code above and below for a table, will create a new row for each entry.
I'll probably evolve a few, already created on that includes a delete button at the end of the line which is 'selectalldel'.
Open to comments on whether this actually is worthwhile, but thought I'd post up my corrected stupidity in case anyone finds this useful.

Possibly missing something very simple in this PHP foreach loop

This question is an extension for this question.
Basically the script compares two arrays and outputs the difference (what to show to the user).
$urlsToShow = array_diff($siteUrls, $seenUrls);
if (!empty($urlsToShow)) {
// Echo the url to show for the iframe within browse.php and add an entry to the database that the user has seen this site
foreach ($urlsToShow as $urlToShow) {
echo $urlToShow;
$entry = "INSERT INTO views VALUES ('', '$currentUsername', '$urlToShow')";
mysqli_query($con, $entry);
break;
}
}
The problem is that I get two entries into the database with one iteration? The first site from the $urlsToShow array is displayed (echoed into the iframe) and gets added to the database along with the next site from the same array. But the user will never see the next site.
Am I using break incorrectly?
It is possible that using break; will not be in your best interest. I suggest using continue;.

Using the output of one velocity template in another

I have a velocity template (in a Confluence user macro) that looks like:
## This macro takes a jiraissues macro in the body with "Show Total Only" configured to TRUE.
## It then parses the return and puts a green check if the number returned is ZERO or a red X otherwise.
## #noparams
#set ($start = $body.indexOf("{") + 1)
#set ($end = $body.indexOf("}") )
Printf debugging...<br />
body.substring($start, $end) = $body.substring($start, $end) <br />
<ac:rich-text-body>
<ac:image ac:thumbnail="false">
## BUG BUG This substring is ALWAYS zero. Dunno why.
#if ($body.substring($start, $end) == "0")
<ri:url ri:value="/images/icons/emoticons/check.png" />
#else
<ri:url ri:value="/images/icons/emoticons/error.png" />
#end
</ac:image>
</ac:rich-text-body>
This template has a nested other velocity template that is configured by the user to query a DB and return the number of bugs that match some criteria. The idea is that if the number returned is ZERO, then everything is hunkydory. Otherwise, well... you get the picture.
Now, there's something CLEARLY screwed up in my thinking.
The $body string seems to returns something that looks like {0} issues.
The {0} seems like a variable or something, but hell if I can find any documentation.
Questions
Which template gets evaluated first?
Can I even base the logic of one template on the output of another?
Why is my life like this? Never mind, I know the answer to that one.
A string like {0} suggests that what you see isn't the actual end result, but a message template that is supposed to be filled in with real data. And to me it looks like a key used by MessageFormat, but it could be something else.
Do you happen to have the code for the inner macro as well?

Check if an existing value is in a database

I was wondering how I would go about checking to see if a table contains a value in a certain column.
I need to check if the column 'e-mail' contains an e-mail someone is trying to register with, and if something exists, do nothing, however, if nothing exists, insert the data into the database.
All I need to do is check if the e-mail column contains the value the user is registering with.
I'm using the RedBeanPHP ORM, I can do this without using it but I need to use that for program guidelines.
I've tried finding them but if they don't exist it returns an error within the redbean PHP file. Here's the error:Fatal error: Call to a member function find() on a non-object in /home/aeterna/www/user/rb.php on line 2433
Here's the code that I'm using when trying this:
function searchDatabase($email) {
return R::findOne('users', 'email LIKE "' . $email . '"');
}
My approach on the function would be
function searchDatabase($email) {
$data = array('email' => $email);
$user = R::findOne('users', 'email LIKE :email, $data);
if (!empty($user)) {
// do stuff here
} // end if
} // end function
It's a bit more clean and in your function
Seems like you are not connected to a database.
Have you done R::setup() before R::find()?
RedBeanPHP raises this error if it can't find the R::$redbean instance, the facade static functions just route calls to the $redbean object (to hide all object oriented fuzzyness for people who dont like that sort of thing).
However you need to bootstrap the facade using R::setup(). Normally you can start using RB with just two lines:
require('rb.php'); //cant make this any simpler :(
R::setup(); //this could be done in rb.php but people would not like that ;)
//and then go...
R::find( ... );
I recommend to check whether the $redbean object is available or whether for some reason the code flow has skipped the R::setup() boostrap method.
Edited to account for your updated question:
According to the error message, the error is happening inside the function find() in rb.php on line 2433. I'm guessing that rb.php is the RedBean package.
Make sure you've included rb.php in your script and set up your database, according to the instructions in the RedBean Manual.
As a starting point, look at what it's trying to do on line 2433 in rb.php. It appears to be calling a method on an invalid object. Figure out where that object is being created and why it's invalid. Maybe the find function was supplied with bad parameters.
Feel free to update your question by pasting the entirety of the find() function in rb.php and please indicate which line is 2433. If the function is too lengthy, you can paste it on a site like pastebin.com and link to it from here.
Your error sounds like you haven't done R::setup() yet.
My approach to performing the check you want would be something like this:
$count = count(R::find('users', 'email LIKE :email', array(':email' => $email)));
if($count === 0)
{
$user = R::dispense('users');
$user->name = $name;
$user->email = $email;
$user->dob = $dob;
R::store($user);
}
I don't know if it is this basic or not, but with SQL (using PHP for variables), a query could look like
$lookup = 'customerID';
$result = mysql_fetch_array(mysql_query("SELECT columnName IN tableName WHERE id='".$lookup."' LIMIT 1"));
$exists = is_null($result['columnName'])?false:true;
If you're just trying to find a single value in a database, you should always limit your result to 1, that way, if it is found in the first record, your query will stop.
Hope this helps