In this tutorial, we'll learn how a single line of PHP gets you access to all of the powerful utilities, algorithms, API calls, visualizations (and more) in the Blockspring community library.
First, download the Blockspring module. From your command line, in your working directory, enter:
You can also download the module here.
curl -o ./blockspring.php "https://raw.githubusercontent.com/blockspring/blockspring-php/master/blockspring.php"
Blockspring is installed. To initialize it, open your PHP environment and enter:
Your first run: get the weather.
Let's get the weather in one line of PHP:
$res = Blockspring::runParsed("weather-for-zip-code", array("zip_code" =>"94109"));
The weather in San Francisco doesn't change much, so you should probably see something like this:
print($res->params["weather"]); San Francisco, CA | 60.6° | Partly Cloudy
You ran your first Blockspring function. Let's explain how that worked.
Blockspring functions in a nutshell
When you ran
Blockspring::runParsed, here's what happened:
- Blockspring used your function name to find Get Weather for Zip Code, a simple weather scraper written in Python and submitted by user jtokoph.
- Get Weather for Zip Code quickly executed in the cloud, with your zip code passed through as an input argument.
- You got back results and printed the definition, found in
That instant replay should raise some questions. Let's answer them.
A quick Q+A
Where do I find more Blockspring functions?
Browse the community library to find utilities, algorithms, API calls, visualizations, and more. You're now a quick search away from programming just about anything.
Great, I found a function. How do I run it?
Click "Integrations" on the function's homepage, choose PHP, and copy and paste the given code snippet to wherever you need it. Try it yourself with Get Weather for Zip Code.
How do I know what inputs my function requires?
Again, click Get Weather for a Zip Code, go to "Integrations", click PHP, and you'll see in the given code snippet the only input required is zip code. The snippets are helpful for finding a function's required input arguments.
How do I know where to find the function output?
The output is found in the result's
$res->params. Explore that variable to find the output you're looking for. To learn more about the response, visit the api reference.
Your second run: interactive google map
Let's create an interactive Google map in one line of PHP.
$res = Blockspring::runParsed("interactive-google-map", array( "locations" => array( array("Latitude", "Longitude", "Tooltip"), array(37.4217542234, -122.100920271, "Somewhere"), array(41.895964876906, -87.632716978217, "Out"), array(28.58230778, 77.09399505, "There") ) ));
Now print out the response.
This function saves the interactive Google map as an HTML file to a temporary directory on your machine, and sets that location to
$res->params["map"]. That means you can open map as you would open any HTML file.
Go to your terminal and copy/paste the filepath you saw in
$res->params["map"] (remember - your filepath will be different) like so:
# remember - your path will be different! open /private/var/folders/6w/920995kj30d_tn8skl8ml5wr0000gn/T/qWLI2e-map.html
You should see an interactive map open in your browser that looks like the image below:
Your third run: summarize a URL
This time, let's summarize a URL in a line of PHP.
We'll also enable caching. If the summarization was done within the last 3,600 seconds, you'll receive it right away instead of needing to re-run the function.
$res = Blockspring::runParsed("summarize-text", array( "url" => "http://www.usatoday.com/story/life/music/2015/04/18/rock-hall-of-fame-2015-induction-ceremony/25913423/", "sentences" => 1 ), array( "cache" => true, "expiry" => 3600 )); print($res->params["summary"]); # Such was the case Saturday night at the 30th annual gathering, which saw the inductions of Ringo Starr, Bill Withers, Joan Jett and the Blackhearts, Stevie Ray Vaughan & Double Trouble, the Paul Butterfield Blues Band, Green Day, Lou Reed and the "5'' Royales.
Become a Blockspring pro
Blockspring lets you program just about anything with a single line of PHP and you're well on your way. You now know how to:
- Run Blockspring functions,
- Search for Blockspring functions, and
- A bit of background behind how Blockspring works.
You can also add your own public (or private) functions to Blockspring. Any function you add to Blockspring can be easily run across languages, chained, composed, cached, and shared with others, without any set up or maintenance. Feeling brave? Create your first Blockspring function with PHP.
Discover new functions Work at Blockspring