Login

Factual Blog /

Factual PHP Driver

We are pleased to report the availability of the official PHP driver for the Factual API. This driver wraps our Read, Crosswalk, Resolve, and Schema APIs, providing five-line access to our 55 million global places and other structured data.

Examples

Getting Started:

First, get an API key from Factual. Add your oauth key and secret to the constructor:

	require_once('Factual.php');
	$factual = new Factual("yourOauthKey","yourOauthSecret");

Follow with syntax specific to the selected API:

Read [docs]

The driver provides the same powerful abilities to query, filter, order, and sort as provided via the REST API:

	//Free text query
	$query = new FactualQuery;
	$query->search("Sushi Santa Monica");  //searches across numerous fields
	$query->field("country")->equal("US"); //limit results to the US
	$res = $factual->fetch("places", $query);
	print_r($res->getData());	

	//Search near a point
	$query = new FactualQuery;
	$query->within(new FactualCircle(34.06018, -118.41835, 5000)); //5k meter radius
	$query->limit(10); //only get ten results
	$query->sortAsc("\$distance"); //order results by distance
	$res = $factual->fetch("places", $query);
	print_r($res->getData());

We also allow for nested conditions and the complete range of parametric operators. If we have it, you can find it.

Resolve [docs]

Provided to meet all of your entity enrichment and matching needs:

	//Find a match at a specific lon/lat
	$query = new ResolveQuery();
	$query->add("name", "Buena Vista Cigar Club");
	$query->add("latitude", 34.06);
	$query->add("longitude", -118.40);
	$res = $factual->fetch("places", $query);
	print_r($res->getData());

Crosswalk [docs]

Find third-party identifiers and URLs:

	//Get all crosswalks for a specific Factual ID
	$query = new CrosswalkQuery();
	$query->factualId("97598010-433f-4946-8fd5-4a6dd1639d77");
	$res = $factual->fetch("places", $query);
	print_r($res->getData());

Schema [docs]

Table introspection:

	//Get the schema for the Global Places table
	$res = $factual->schema("places");
	print_r($res->getColumnSchemas());
	exit;

Geocoding

And we’ve thrown in some cheeky geocoding:

	//geocode (convert an address to a longitude and latitude)
	$res = $factual->geocode("425 Sherman Ave, Palo Alto, CA, USA");
	print_r($res);

	//reverse geocode (convert an address to a longitude and latitude)
	$res = $factual->reverseGeocode(-122.143895,37.425674);
	print_r($res);

Factual does not provide a geocoding service, so these use a third-party; we’ve included these methods as convenient, if simple, hooks. Like the MPAA’s claims that online piracy is ruining America’s economy, they are unsupported.

Get It Here

The driver is open source and can be downloaded from https://github.com/Factual/factual-php-driver.  Example code and detailed commentary are provided in the PHP Driver Readme. Driver support and feature requests can be pinged to the Factual Developer Group. Rockin.

-Tyler Bell
Director of Product