Freebase PHP – albumlist – building a simple app

July 5, 2007

A step-by-step guide to building your the first Freebase PHP application.

This is an update of the Freebase documentation ‘4.6. albumlist.php: A Metaweb-enabled web application in PHP’ which currently has some bugs.

  1. Follow the steps in Getting started with Freebase and PHP
  2. Once you’ve confirmed that you can access Freebase using PHP, download this tarball contaning metaweb.php , albumlist.php , JSON.php to your webserver.
  3. Replace the dummy cookie in albumlist.php with your own (see Getting Started for details)
  4. View albumlist.php through your webserver. It should look like this.

Congratulations on building your first Freebase app in PHP :-)

Exercises for the reader:

  1. Try making the band name search less sensitive. For example ‘the beatles’ is OK but ‘beatles’ currently gives no results.
    Hint: read “Pattern Matching in Queries
  2. Add a band picture
    Hint: “/common/topic/image”:[{}],
  3. Add more info about each album
    Hint: “album”:[{ "*":[{}] }],

Tip: The best way to develop queries is to use the Query Builder and then the Query Editor. Only once you’ve got the query working should you translate it into PHP.


Freebase API – cookie test

July 5, 2007

Here is one line shell script to test your Freebase cookie. (Replace the dummy cookie with your own cookie):

curl --globoff --cookie \\
'metaweb-user="Z|a_aaaaaaaaaa|a_#111...snip...111|1.aaZZZaZaZaaZZaZZ1aZ/ZZ"' \\
'http://www.freebase.com/api/service/mqlread?q={"query":[{"id":"/type/int"}]}'

You should see this result:

{
  "status": "200 OK",
  "query": {},
  "code": "/api/status/ok",
  "messages": [],
  "result": {
    "query": [
      {
        "id": "/type/int"
      }
    ]
  }

Freebase PHP – building a simple query

July 5, 2007

Bart asked how I would go about “trying to query the database to find out which subdomains/types the domain computer games has?”

Here are the steps I took: (By the way, I don’t think subdomains exist)

  1. Check the steps in ‘Getting started with Freebase and PHP’
  2. On Freebase do a keyword search for ‘Computer Games
  3. The first hit is the right domain, you can see that its id is ‘/cvg’ from the URL
  4. Open the MQL Query Editor
  5. Scroll through the examples for a suitable example query.
    Choose ‘3.2.21 Types in the music domain’
  6. Change “id”:”/music” to “id”:”/cvg

So the query in Javascript is:

{
  "query":{
    "id":"/cvg",
    "type":"/type/domain",
    "types":[]
  }
}

Which translates to the following PHP:

$query = array( "type" => "/type/domain",
                 "id" => "/cvg",
                 "types" => array());

There you go!


Getting started with Freebase and PHP

July 4, 2007

It seems some of the Freebase API examples are broken. Thanks to Bart for the heads up.

(Update Nov 07 - Freebase no longer requires an account for read access. So the cookie is probably unnecessary. )

Here are the most basic steps to getting started with Freebase PHP:

  1. Check your webserver runs PHP
  2. Download TestFreebasePHP.php to your webserver.
  3. View the PHP script through your server, you should see ‘Query failed’
  4. Check you are logged into your Freebase account.
  5. Get your metaweb-user cookie. (With Firefox: Preferences, Privacy, Show Cookies, search ‘Freebase’). It should look like this:
    metaweb-user="Z|a_aaaaaaaaaa|a_#1111a1a1111...snip...ZaaZZaZZ1aZ/ZZ"
  6. Test your cookie using this shell one-liner
  7. Edit TestFreebasePHP.php and replace the dummy cookie with your own.
  8. View the PHP script through your web server. If you see ‘Query succeeded’ then all is well.

I challenge you to come up with a smaller test script!

You can see the script running successfully on my server:

Test Freebase access using PHP

Loading: http://www.freebase.com/api/service/mqlread?q={"query":[{"id":"/type/int"}]}

Query succeeded
Response:

{
  "status": "200 OK",
  "query": {},
  "code": "/api/status/ok",
  "messages": [],
  "result": {
    "query": [
      {
        "id": "/type/int"
      }
    ]
  }
}

MapIt – a Freebase Google Maps builder (updated)

May 31, 2007

My latest version of MapIt has a better GUI. Less black-magic going on now.

Try it (probably Firefox only for now)

mapit builder

Building a query:
origin of rivers + image

 

MapIt

Resulting mashup
showing Freebase rivers on Google Earth

Example maps:

Let me know if you manage to build any interesting maps. Feedback and bug reports very welcome.


Mapit – a Freebase Google Maps builder

May 14, 2007

 Sorry, this program no longer works. I’d love to fix it but I really don’t have time right now.

Mapit is my first attempt at a usable map-builder for Freebase. The goal is to enable ordinary users to build their own Freebase / Google Maps mashups. (Freebase account required – ask me if you need an invite. Tested on Firefox) Mapit screenshot I spent most of the weekend underground so I didn’t have much time to work on the GUI. It would be great to make something like Yahoo Pipes. But even this work-in-progress version of Mapit is pretty powerful. My previous map of Freebase Users can now be generated in 6 mouse clicks. Examples:

I know that there is loads of scope for improvement and bug fixes. Comments very welcome.

 Sorry, this program no longer works. I’d love to fix it but I really don’t have time right now. 


Freebase + Ruby

May 11, 2007

Jan is working on a Ruby connector for Freebase.

His initial results look really promising:

metallica = Freebase.find(:first, :name => 'Metallica', :type => '/music/artist')
metallica.album.first(5)
=> ["Kill 'Em All", "Ride the Lightning", "Garage Days Re-Revisited", "...and Justice for All", "Harvester of Sorrow"]
metallica.genre
=> ["Metal"]

Way to go Jan! This is definitely a glimpse of the future, high-level programming with the world’s knowledge at your finger tips.


Freebase invites up for grabs

May 9, 2007

Update: Freebase no longer requires invites

Freebase logo

At the time of my BarcampBrussels talk on Freebase I didn’t have any invites to give away. Now I’ve got some. Leave a comment here if you’d like one.


Freebase users on Google Maps, a mash-up

May 8, 2007

Here is my mash-up showing Freebase users on Google Maps:

Freebase users on Google Maps

  • You can search by username and fullname. Example “john”.
  • MQL regexp is supported. Example “^j*” (all users that start with j)
  • Or just show everybody (with a geocode). Example: Everybody

Issues raised:

  • Some location data was foobar. Karlsruhe, Germany is shown in Somalia :-) Data fixed
  • How could I add a user’s image? Done.
    Freebase data-model weirdness? Why is there an empty picture_s field and a /common/image field for a user_profile?
  • I have to make two almost identical queries (searching name and my_full_name) because of a lack of an OR construct in MQL. This seems pretty inefficient. Can anybody suggest a better way? Done:Kurt suggested using “/type/reflect” reflection properties (not for the faint of heart!)
  • Is there a clean way of being informed when all MJT tasks have completed?

Feedback very welcome.

Update:

  • May 9th: Added button navigation and profile photos
  • May 11th: Various user interface improvements + Kurt’s solution for a single MQL query.
  • Nov 07: Fixed broken links

Freebase, a quick reality check

May 7, 2007

With Freebase you can do amazing queries that would be impossible with Google or the Wikipedia. As Jan commented on my BrusselsBarcamp talk, you could “Look for actors from Ghent who were born between 1970 and 1999“.

Well let’s give it a shot. Here’s the Javascript query to do just that:

{
"query":[{
"a:type":"/tv/tv_actor",
"date_of_birth":null,
"date_of_birth=":"1970",
"name":null,
"place_of_birth":"Ghent",
"type":"/people/person"
}]
}

(Drum roll please …) and here are the results:

{
"result":[],
"status":"/mql/status/ok"
}

Nada. There ain’t no data in Freebase on Belgian actors from Ghent in their twenties :-(

And that’s the frustrating thing about Freebase. The data just isn’t there yet and it’s going to take a massive amount of data-entry to fulfill it’s promise. On the positive side, their API allows write access and there are a lot of open data sources out there so at least it doesn’t all have to be entered manually.

( … a few minutes pass while Will does some manual data-entry … )

Let’s re-run that same query:

{
"result":[{
"a:type":"/tv/tv_actor",
"date_of_birth":"1970-06-19",
"name":"Franky Lanckacker",
"place_of_birth":"Ghent",
"type":"/people/person"
},{
"a:type":"/tv/tv_actor",
"date_of_birth":"1972",
"name":"Miguel Wiels",
"place_of_birth":"Ghent",
"type":"/people/person"
}],
"status":"/mql/status/ok"
}

Now the world knows about Franky Lanckacker and Miguel Wiels :-)

Freebase is an exciting idea and it may well be the next big thing, but it’s got to reach critical mass in terms of data and users to become really useful.


  • PS I cheated a little, Jan originally said 1980 (rather than 1970) but I just couldn’t find Ghent actors under 27 on the internet.
  • PPS Updated: Improved query as per Daniel’s suggestion below. (I couldn’t figure out how to constrain to type person AND type tv_actor)