Archive for the 'api' Category
How to use the digg API
By Justin Silverton

This article will show you how to use the digg API using PHP. I have also written a library in PHP that maps all of the endpoints documented in the API to easily accessible functions (it is also compatible with PHP 4).
What can you do with the digg API?
- Get all popular stories submitted after January 1, 2007 sorted by promotion date
- Get the most recent 100 upcoming stories in the topic “Apple”
- Get the first 20 stories that were promoted on or after December 25, 2005
- Determine whether a story, identified by its URL, has been submitted to Digg and, if so, get details like the number of Diggs and comments it has received
- Get the details of a story, identified by its URL on Digg
- Get all stories submitted today from nytimes.com
- Get all stories submitted by Kevin Rose (or another Digger)
How it works
Requests are sent to the digg servers through URLS that are described in the API doc (here).
Here is an example:
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com
This will return all stories. By default only 10 will be returned at a time (which can be increased to a maximum of 100). Also, you may notice an appkey=XXX argument. This is a unique identifier required by the digg servers. It is recommended to use the urlencoded refereral URL. Other return types are also available, which can make parsing easier depending on the language used.
response types:
- XML: Easily parsed in many languages on many platforms. It is particularly easy to use in Flash applications.
- JSON: May be directly eval’d in Javascript, and also can be parsed in many languages.
- Javascript: Useful as the source of a script tag, it passes JSON response to the Javascript callback function you specify.
- Serialized PHP: Easily unserialized in PHP to create objects, to which the programmer can attach custom methods. Or the programmer can directly access the response data through the public properties of the objects.
example:
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com (will return XML by default)
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=json
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=php
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=javascript
Other basic arguments
- count
Number of events to retrieve.
Event count integer
Default: 10, Maximum: 100.
offset
Offset in complete events list.
Event offset integer.
Default: 0.
PHP code
The following is a function that will allow you to connect to the digg service and parse the results. It also will automatically generate the appid based on the current location of the script.
function connect($hostname) {
//this is an ID that is unique to your app. It is auto-generated, based on the calling URL
$appid = urlencode("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
$host = $hostname."&appkey=".$appid;
echo $host;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL,$host);
curl_setopt ($ch,CURLOPT_USERAGENT,"Test library");
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
$response = curl_exec ( $ch );
curl_close($ch);
return $response;
}
Download
The digg toolkit can be downloaded Here
1 commentHow to use the facebook API
By Justin Silverton
The facebook API allows you to access profiles, friends, photos, and events. To access the API from an outside server, you need to go through the following steps:
- login to your Facebook Account
- get an account key: http://developers.facebook.com/account.php?action=add_key
- Get the client library: http://developers.facebook.com/documentation.php?doc=clients (I will be discussing the PHP API, but it is also available for c#, c++, java, perl, python, ruby, and visual basic.NET).
- The php 5 library can be found here: http://developers.facebook.com/clientlibs/php5_client.tar.gz (curl is required)
Getting started
Place the following in a file called “client.php”. Also, facebook_conf.php and facebookapi_php5_restlib.php need to both be in the same directory.
include_once ‘facebook_conf.php’;
include_once ‘facebookapi_php5_restlib.php’;
$rest_server = ‘api.facebook.com/restserver.php’;
$api_key = (Your api key from facebook);
$secret_key = (your secret key from facebook);
$session_key = md5(time());
Note: This is just a sample session key. This should be a unique key that can identify the current session user. It can also be placed in a cookie.
$client = new FacebookRestClient($rest_server, $api_key,$secret_key, $session_key, false);
$session_info = $client->auth_getSession($auth_token);
$client->session_key = $session_info[’session_key’];
$uid = $session_info[’uid’];
$session_info = $client->auth_getSession($auth_token);
//Here are some sample pieces of information that can be retrieved
$your_messages = $client->messages_getCount(); //get your total number of messages
$your_wall_count = $client->wall_getCount(); //get the number of posts on your wall
$p_albums = $client->photos_getAlbums($uid); //get list of photo albums
The facebook api can do much more than explained in these examples. For more information, go here: http://developers.facebook.com/documentation.php.
20 commentsThe Zend Framework
By Justin Silverton
Introduction
The Zend Framework is a recently released (still in alpha) set of open source tools for php designed for developing Applications and Web Services.
Included Functionality
Zend_Controller and Zend_View
These components provide the base for a simple MVC website and are already used on this site and several others. A front controller dispatches requests to page controllers. It is as minimalist as possible and we’re working to make it even simpler. The Zend_View component provides encapsulation for view logic. It can use templates written in PHP or can be combined with a third-party template engine.
Zend_Db
Database access is a very light layer on top of PDO. Solutions existing systems not using PDO (such as mysqli or oci8) are presently under development. Included are adapters, a profiler, a tool to assist with building everyday SELECT statements, and simple objects for working with table row data.
Zend_Feed
The links on the sidebars of our home page are generated using Zend_Feed. This component provides a very simple way to consume RSS and Atom data from feeds. It also includes utilities for discovering feed links, importing feeds from different sources, and feeds can even be modified and saved back as valid XML.
Zend_HttpClient
This component provides a client for the HTTP protocol and does not require any PHP extensions. It drives our web services components. In time, we will develop support for extension-based backends such as cURL.
Zend_InputFilter
The input filtering component encourages the development of secure websites by providing the basic tools necessary for input filtering and validation.
Zend_Json
Easily convert PHP structures into JSON for use in AJAX-enabled applications.
Zend_Log
Log data to the console, flat files, or a database. Its no-frills, simple, procedural API reduces the hassle of logging to one line and is perfect for cron jobs and error logs.
Zend_Mail and Zend_Mime
Almost every internet application needs to send email. Zend_Mail, assisted by Zend_Mime, creates email messages and sends them. It supports attachements and does all the MIME dirty work.
Zend_Pdf
Portable Document Format (PDF) from Adobe is the de facto standard for cross-platform rich documents. Now, PHP applications can create PDF documents on the fly, without the need to call utilities from the shell, depend on PHP extensions, or pay licensing fees. Zend_PDF can even modify existing PDF documents. Create a sharp customer invoice in Adobe Photoshop, fill in the order from Zend_Pdf, and send it with Zend_Mail.
Zend_Search_Lucene
The Apache Lucene engine is a powerful, feature-rich Java search engine that is flexible about document storage and supports many complex query types. Zend_Search_Lucene is a port of this engine written entirely in PHP 5, allowing PHP-powered websites to leverage powerful search capabilities without the need for web services or Java. Zend_Search_Lucene’s file format is fully binary compatible with its Java counterpart.
Zend_Service: Amazon, Flickr, and Yahoo!
Web services are becoming increasingly important to the PHP developer as mashups and composite applications become the standard for next generation web applications. The Zend Framework provides wrappers for service APIs from three major providers to make the as simple to use as possible. We’re working on more and engaging API vendors directly to make PHP the premier platform for consuming web services.
Zend_XmlRpc
PHP 5’s SOAP extension dramatically lowered the bar for communicating with SOAP services from PHP. Zend_XmlRpc brings the same capabilities to XML-RPC, mimmicking the SOAP extension and making these services easier to use than ever from PHP 5.
Conclusion
The Zend Framework looks promising, but I think that in its current state, it is more of a set of classes than an actual framework. Currently, PEAR is a much better choice in terms of community support and component availability. I am glad that Zend is continuing to embrace the open source community and I will be curious to see the future builds of this framework.
Download
It can be downloaded Here
No commentsFile downloads in PHP
By Justin Silverton
This is a simple code snippet. It will allow you to force the web-browser that is currently viewing your script to come up with a file-download box (and the a file on your system can be downloaded).
Here is the code:
//so only the data from the headers is sent
ob_start();
//dispable caching
header (”Cache-Control: must-revalidate, pre-check=0, post-check=0″);
header (”Content-Type: application/binary”);
header (”Content-Length: ” . filesize($export_long_name));
header (”Content-Disposition: attachment; filename=yourfile.ext”);
readfile($export_long_name);
A list of of mime types can be found Here
No comments





