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"
      }
    ]
  }
}