Retrieving Posts from Tumblr - php

I've tried to do my research regarding this topic but the more digging I do, the more confused I'm getting - seems like the documentation on Tumblr's API is all over the place (and fairly dated?); so perhaps someone out there will have some advice for me.
All I'm trying to do is to snag my most recent Tumblr posts and display them on the main page of my website; nothing too fancy. I don't need to POST to tumblr, I just need the info; the idea is that the team I'm working with would like to use Tumblr as a sort of poor-man's CMS while maintaining their presence on the Tumblr site proper.
I can easily retrieve posts using PHP and SimpleXML (http://username.tumblr.com/api/read) - but this doesn't offer too much support outside simple text posts (Other content, like video, photosets, etc - post differently); and it doesn't really seem like an officially supported method of accessing tumblr data (or..maybe it is?). Tumblr itself points folks towards tumblr.php, but I for the life of me can't figure out how to set it up since there's no real documentation for someone just starting to dabble. Some posts on StackOverflow say Tumblr.php doesn't work anymore and point folks to tumblrOauth.php, but I'm running into the same problem. No real 'new to API' documentation. I know how to set up my 'app' in Tumblr - I just don't know how all the Github files (for both of these systems) communicate to each other/the page...
Anyone have any advice? I'm just looking for a point in the right direction, maybe an initial nudge on how to hook the bits up?
Much thanks e-friends!

Hey I've used a few API's but not the Tumblr API, after reading through this page: http://www.tumblr.com/docs/en/api/v2 I'm pretty sure I understand how to set this up so I'll have a go at explaining and you can let me know if you have some success
(Removed code, that PHP API is awful)
EDIT: Original edit removed
Right since the Tumblr PHP Client is rubbish I've had a look myself and just designed a little snippet of code to solve the problem here and retrieve your latest posts in an array that will give you like so:
Array
(
[0] => Array
(
[id] => 49867777007
[type] => photo
[description] =>
[date] => 2013-05-07 18:28:41 GMT
[short_url] => http://tmblr.co/Z5XfMxkSMUFl
[caption] =>
[photo_url] => http://31.media.tumblr.com/a13494fb3cda1e40ab39211973a094f9/tumblr_mm0038sZGH1qdlh1io1_400.gif
[width] => 395
[height] => 350
)
Use this code and then go through your $myposts later on the page
<?php
$apikey = "your_api_key";
$limit = 10;
$tumblr = "your.tumblr.com";
$apidata = json_decode(file_get_contents("http://api.tumblr.com/v2/blog/$tumblr/posts?api_key=$apikey&limit=$limit"));
$mypostsdata = $apidata->response->posts;
$myposts = array();
$i = 0;
foreach($mypostsdata as $postdata) {
$post['id'] = $postdata->id;
$post['type'] = $postdata->type;
$post['description'] = $postdata->description;
$post['date'] = $postdata->date;
$post['short_url'] = $postdata->short_url;
$post['caption'] = $postdata->caption;
$post["photo_url"] = $postdata->photos[0]->original_size->url;
$post["width"] = $postdata->photos[0]->original_size->width;
$post["height"] = $postdata->photos[0]->original_size->height;
$myposts[$i] = $post;
$i++;
}
// Then handle $myposts later on your page
echo '<pre>'.print_r($myposts).'</pre>';
// There's quite a lot more data you can use so if you want
// to print out $mypostsdata then you might find any more data you need
?>
Sorry the previous suggestions didn't work, the PHP Client really is awful but let me know if that's enough or if you need some revision to it

Related

SoundCloud - Update Cover / Artwork Image

We use the SoundCloud API to upload tracks with PHP. This is all working perfectly, but we would like to be able to update the track cover artwork with the SoundCloud API. We already upload the cover art when we initially upload the track, but when we try and modify it after the initial upload, it doesn't seem to work (we don't get any errors, it just doesn't get updated and stays the same as it was before).
Is this an intentional limitation of the SoundCloud API or am I missing something?
Edit 1:
I've emailed the SoundCloud API team to ask if they'd just clarify if updating track artwork is possible via the API, but I've had no response (around 48 hours so far).
Edit 2 - 2014-07-08:
4 days on and still no reply yet from the SoundCloud API team? Am I being ignorant thinking this is a simple "yes it should work" or "no that isn't supported with the current API" reply?
Edit 3 - 2014-08-28:
Over 7 weeks and still haven't heard anything from SoundCloud. Looks like they aren't going to be replying!
$track_info_array = array(
'track[title]' => $track['title'],
'track[sharing]' => $track['sharing'],
'track[track_type]' => $track['track_type'],
'track[purchase_url]' => $track['purchase_url'],
'track[description]' => $track['description'],
'track[genre]' => $track['genre'],
'track[label_name]' => $track['label_name'],
'track[tag_list]' => $track['tag_list'],
'track[release_day]' => $release_date[2],
'track[release_month]' => $release_date[1],
'track[release_year]' => $release_date[0],
'track[isrc]' => $track['isrc'],
'track[release]' => $track['release'],
'track[bpm]' => $track['bpm'],
'track[key_signature]' => $track['key_signature']
);
if($release_image){
$track_info_array['track[artwork_data]'] = '#'.$release_image;
}
$track_info = $soundcloud->put('tracks/' . $track['soundcloud_track_id'], $track_info_array);
If it's possible, I believe you want to use PUT instead of a POST request. you also will want to include the track's ID in the endpoint.
Acording to the docs, it is only possible to use artwork_data for uploading, no updates to the audio file nor the artwork.
The fields artwork_data and asset_data have a comment of "only for uploading"
Reference: https://developers.soundcloud.com/docs/api/reference#tracks
Delete all your temporary internet and cache files. It may actually be changing for everyone else but you.
There is a trick that usually works. The Soundcloud API may have some sloppy coding. Try editing the entire URL link for the target file. That should work.

PHRets using POST

I'm using PHRets and trying to return some search results. Below is the code, which is very basic and should work:
include('../include/common.php');
include('../classes/phrets.php');
$rets = new phRETS();
$connect = $rets->connect(RETS_LOGIN_URL, RETS_USERNAME, RETS_PASSWORD);
if($connect){
$search = $rets->SearchQuery('PROPERTY', 'RES', '((COUNTY=Dallas))', array('LIMIT'=>20));
print_r($rets->Error());
echo $rets->TotalRecordsFound($search);
$rets->Disconnect();
}else{
$error = $rets->Error();
print_r($error);
}
When I run the page, I receive the following error:
Array ( [type] => rets [code] => 20203 [text] => The request limit is too large for a GET. Please use the POST method to submit your search. ) 0
I don't see a parameter to force PHRets to send the request as a POST. Will this require a hack of the class, or am I missing something?
Thanks
EDIT: I just heard back from the developer. It's not supported at this time. Perhaps a less-busier person should clone it and work on that.. :) Anyway, if anyone has already has modified the code to make this work, let me know, please.
Get the latest phRETS version which supports POST method from here and replace with your old phrets.php.
then you need to add an extra line to the script
$rets->SetParam('use_post_method', true);
Note: By default it will be GET method.
I've published an updated version supporting POST searches available at https://github.com/nathanklick/PHRETS or via composer by adding "nathanklick/phrets": "1.0.2" to your composer.json file.

How to save XML API data to database in WordPress once an hour for later use?

I manage the website for PAWS New England, an animal rescue organization who relies heavily on PetFinder. While PetFinder offers an iframe based widget for display your available animals, it breaks the responsive design of the site on smaller screens.
Because of this, I've built custom "Our Dogs" page by using PetFinder's API (XML based).
Unfortunately, the API can run pretty slow at times. I would like to pull data from the API once an hour and store it on the site's mySQL database (it's powered by WordPress), and run the custom page off that instead of the live API data. WordPress's "Transient API" seems like a perfect fit, but I can't for the life of me figure out how to make it work.
After some searching, it seems like the problem may be in PHP's ability (or lack there of) to store XML data. In other words, I may need to convert the data to a string or array first.
I'm now officially stuck. Anyone have any insights on how to save XML data to the WordPress database once and hour, and access that data for use in a function?
Here's my existing code. Thanks in advance!
<?php
function petf_shelter_list( $atts ) {
extract( shortcode_atts( array(
'shelter_id' => '1234',
'api_key' => 'abcdef',
'count' => 150,
'status' => 'A'
), $atts ) );
$xml = simplexml_load_file( "http://api.petfinder.com/shelter.getPets?key=" . $api_key . "&count=" . intval($count) . "&id=" . $shelter_id . "&status=" . $status . "&output=full" );
// Stuff I do with $xml here...
}
add_shortcode('shelter_list','petf_shelter_list');
?>
You can use PHP's XML Parser to deal with the XML portion of your problem.
As for running something every hour, you need to setup a cron job to execute your desired script.

Facebook Graph API Change: Picture type (size) no longer working?

According to Facebook's Graph API documentation (here), you can access various sizes of a user's profile picture through URLs such as:
https://graph.facebook.com/(userid)/picture?type=small
https://graph.facebook.com/(userid)/picture?type=large
You'll notice that the first resolves to a url ending in _t.jpg (the small thumbnail) and the second ending in _n.jpg (the large image). So far so good. Equivalently, we should be able to query for these images like this:
https://graph.facebook.com/(userid)?fields=picture&type=small
https://graph.facebook.com/(userid)?fields=picture&type=large
This latter format worked as expected for many months, until just a few days ago when it suddenly started ignoring the "type" parameter entirely - everything now resolves to an image ending in _q.jpg, which is the default if no "type" is specified. As a result, I can no longer figure out a way to query for the large image in PHP (my real problem). It used to work like this:
$pic = $facebook->api('/me', array('fields' => 'picture', 'type' => 'large'));
...but as described above, "type" has spontaneously started being ignored. I've spent several hours scouring their documentation but haven't been able to find any reference to what has changed, or the "new" way this should be done - any pointers would be hugely appreciated...
EDIT:
None of the following work, either (returns nothing):
$pic = $facebook->api('/me/picture', array('type' => 'large'));
$pic = $facebook->api('/(my_uid)/picture', array('type' => 'large'));
$pic = $facebook->api('/me/picture/?type=large');
$pic = $facebook->api('/(my_uid)/picture/?type=large');
Basically, since Facebook broke things a few days ago there doesn't seem to be any way to get a non-default picture size from PHP. You can try out some of the calls yourself from the Graph API Explorer (here).
Other related/relevant links:
http://stackoverflow.com/questions/2978761/facebook-graph-api-will-not-give-me-picture-data
http://stackoverflow.com/questions/7718704/requesting-picture-for-event
You must request the API with the field_expansion syntax
These api requests works :
$results = $facebook->api('/me', array('fields' => 'picture.height(300).width(300)'));
$results = $facebook->api('/me', array('fields' => 'picture.type(large)'));
As described in this bug on Facebook, you can request this via the new API "field expansion" syntax.
This works for me:
https://graph.facebook.com/____OBJECT_ID____?fields=picture.type(large)
I found a workaround - profile pictures of various sizes can still be accessed via an FQL query:
$pic = $facebook->api(array('method'=>'fql.query', 'query'=>"SELECT pic_big FROM user WHERE uid=$fb_uid"));
("pic_big" is equivalent to "type=large" - see here).
This still doesn't explain why the GRAPH call suddenly broke though, or why image sizes don't seem to be accessible via Graph at all anymore (which I'd still like to know)...but at least there's some way to get the other size photos.
Gotta love Facebook and their top-notch reliability...

Geotargeting Page Posts with the Facebook API

I'm trying to geotarget wall posts to my fan page using the Facebook PHP SDK.
The following snippet successfully posts a message to my wall, but the geotargeting isn't taking.
I'm new to programming and I've done my best to follow the documentation here but it's pretty sparse -- I'm not confident that my syntax is correct.
Any help would be very much appreciated.
//compiling the geotargeting parameters
$geoTarget = "{'cities':'Richmond,VA','regions':'Virginia','countries':'US'}";
//my arguments
$args = array(
'access_token' => $page_access_token,
'message' => "this is my message",
'targeting' => $geoTarget
);
//posts the message
$facebook->api("/$page_id/feed","post",$args);
The field is privacy in the Post table
The description field may contain a comma-separated lists of valid country, city and language if a Page's post targeting by location/language is specified.
http://developers.facebook.com/docs/reference/api/post/
To make this easier this is the bit you need http://developers.facebook.com/docs/reference/api/page/#targeting
this will give the info on how to target regions too.
http://developers.facebook.com/docs/reference/ads-api/get-autocomplete-data/
What you need to complete the targeting in the question would be:
The countries bit was right.
Virginia's region code is 51 (which you can find by searching _">https://graph.facebook.com/search?q=vi&type=adregion&match_country_code=true&country_list=US&access_token=_)
Richmond's city id is 2538983 (which you can find by searching _">https://graph.facebook.com/search?q=richmon&type=adcity&limit=150&access_token=_)
so the geotarget would be
$geoTarget = "{'cities':[2538983],'regions':[51],'countries':'US'}";

Categories