180 likes | 291 Views
Integrating with Services. Up to this point, we’ve focused on reading from external sources… Writing: Get better functionality than you can write Flickr, google maps, &c Get access to better (more?) audience Facebook, Tumblr, &c Lots of business examples. Business Examples.
E N D
Integrating with Services • Up to this point, we’ve focused on reading from external sources… • Writing: • Get better functionality than you can write • Flickr, google maps, &c • Get access to better (more?) audience • Facebook, Tumblr, &c • Lots of business examples
Business Examples • Nike pedometer -> fb thing • Others…..
Auth • Pass username/password of end user • What’s bad about this idea? • One time (session) key • Permanent service-to-service key • User creates a key at the 3rd party service which it shares with both the local and remote service.
Tumblr: Post I // Authorization info $tumblr_email = 'info@davidville.com'; $tumblr_password = 'secret'; // Data for new record $post_type = 'regular'; $post_title = 'The post title'; $post_body = 'This is the body of the post.'; // Prepare POST request $request_data = http_build_query( array( 'email' => $tumblr_email, 'password' => $tumblr_password, 'type' => $post_type, 'title' => $post_title, 'body' => $post_body, 'generator' => 'API example' ) );
Tumblr: Post II // Send the POST request (with cURL) $c = curl_init('http://www.tumblr.com/api/write'); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_POSTFIELDS, $request_data); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($c); $status = curl_getinfo($c, CURLINFO_HTTP_CODE); curl_close($c); // Check for success if ($status == 201) { echo "Success! The new post ID is $result.\n"; } else if ($status == 403) { echo 'Bad email or password'; } else { echo "Error: $result\n"; }
Tumblr API http://www.tumblr.com/docs/en/api
Flickr Example: Auth Our web based app has the api key '1234567890'. It has already registered a callback url for this key - 'http://viewr.com/auth.php'. • User visits http://viewr.com/ and clicks on the 'login' link. • This link points to http://flickr.com/services/auth/?api_key=1234567890&perms=read&api_sig=2f3870be274f6c49b3e31a0c6728957f. • The user is already logged in to flickr - they are asked if they wish to allow the application to have 'read' permissions to their photos. • The user clicks 'yes' and flickr updates it's auth database. • The user is redirected to http://viewr.com/auth.php?frob=abcxyz. • The app makes a background call to flickr.auth.getToken: http://flickr.com/services/rest/?method=flickr.auth.getToken&api_key=1234567890&frob=abcxyz&api_sig=3f3870be274f6c49b3e31a0c6728957f. • The call returns an auth token '334455'. • The application stores the auth token and creates a session for the user with a cookie which points to the user's auth token. • The application makes a background request to the flickr.people.getInfo to return information about the user, by calling http://flickr.com/services/rest/?method=flickr.people.getInfo&api_key=1234567890&auth_token=334455&api_sig=4f3870be274f6c49b3e31a0c6728957f.
Flickr: Upload POST to http://api.flickr.com/services/upload/ • Arguments • photo The file to upload. • title (optional) The title of the photo. • Description (optional) A description of the photo. May contain some limited HTML. • tags (optional) A space-seperated list of tags to apply to the photo. • is_public, is_friend, is_family (optional) Set to 0 for no, 1 for yes. Specifies who can view the photo. • safety_level (optional) Set to 1 for Safe, 2 for Moderate, or 3 for Restricted. • content_type (optional) Set to 1 for Photo, 2 for Screenshot, or 3 for Other. • hidden (optional) Set to 1 to keep the photo in global search results, 2 to hide from public searches.
Flickr Documentation • Auth: http://www.flickr.com/services/api/auth.spec.html • Upload: http://www.flickr.com/services/api/upload.api.html • PHP library: http://phpflickr.com/
Good APIs are complex • Let libraries do some of the work • Google/Blogger Example: require_once 'Zend/Loader.php'; Zend_Loader::loadClass('Zend_Gdata'); Zend_Loader::loadClass('Zend_Gdata_Query'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
More Google w/ Library • Auth $user = 'user@example.com'; $pass = 'secretPasswd'; $service = 'blogger'; $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service, null, Zend_Gdata_ClientLogin::DEFAULT_SOURCE, null, null, Zend_Gdata_ClientLogin::CLIENTLOGIN_URI, 'GOOGLE'); $gdClient = new Zend_Gdata($client);
More Google w/ Library • Post function createPublishedPost($title='Hello, world!', $content='I am blogging on the internet.') { $uri = 'http://www.blogger.com/feeds/' . $blogID . '/posts/default'; $entry = $gdClient->newEntry(); $entry->title = $gdClient->newTitle($title); $entry->content = $gdClient->newContent($content); $entry->content->setType('text'); $createdPost = $gdClient->insertEntry($entry, $uri); $idText = split('-', $createdPost->id->text); $newPostID = $idText[2]; return $newPostID; }
Google API + Library • http://code.google.com/apis/blogger/docs/1.0/developers_guide_php.html
Today • Write a service that relies on another for visible persistence • Write a PHP that • Take a path to an image on the web • By a get request: myscript.php?img=http://www… • Overlays the word ‘FAIL’ on it • Saves it locally • FTPs it to your (public) RSC space • Returns (prints) the URL of the file in your rpi space
Today • http://php.net/manual/en/function.imagettftext.php • Check out ex 1 • http://www.php.net/manual/en/ftp.examples-basic.php
Wed • Before the lab • Set up a Tumblr account • During Lab • Edit today’s in class work such that every time you post an image, it creates a tumblr post, including your edited image
<?php $homepage = file_get_contents('http://…...’); file_put_contents("index.html", $homepage); $file = 'index.html'; $remote_file = 'public_html/index.html'; // set up basic connection $conn_id = ftp_connect('ftp.rpi.edu'); // login with username and password $login_result = ftp_login($conn_id, ‘########', ‘###########'); // upload a file if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) { echo "successfully uploaded $file\n"; } else { echo "There was a problem while uploading $file\n"; } // close the connection ftp_close($conn_id); ?>