UPDATE: Thanks, nursebuy
everyone, tooth for coming yesterday! I had fun — hope you did too. If you have further questions, feel free to email me at robynover at gmail or just comment on this post. See you around Camp!
The advantage of a front end language like javascript (in fact, audiologist javascript is really the only browser-based language we have) is that you can respond immediately to a user’s actions. Javascript calls these “events”. An event could be a mouse move, a scroll, a key press, and many others. Here’s a list of the major Javascript events.

Javascript won’t tell you automatically when events happen; you have to register an Event Listener to pay attention to that particular event — to “listen for” a mouse click, for example. When you create an event listener, you then define a function which will fire when the even happens.

jQuery also has methods that handle events. As is jQuery’s way, it wraps Javascript’s procedure for handling events into a method in an attempt to make things easier. In the documentation, there’s a nice list of all the jQuery event methods. One of the methods is bind() and that’s an important one. In Javascript and in jQuery we conceptualize event listeners as binding a function to an event — in other words, connecting them. Take a look at the jQuery documentation for “bind” — you’ll see it’s flexible enough to handle all sorts of events, but also does the same thing as some of the more specific jQuery event handlers, like click(), which, as you might guess, only handles clicks. Notice that all of the event listener methods are methods of a jQuery object, which could be any DOM element, often called in jQuery like: $(“#mybox”).

A tricky concept at first in Javascript and jQuery is the way that functions are used. Often event listeners will take a function as a parameter. You may be accustomed to seeing and writing functions that look like:

function addNumbers(a,b){
sum = a + b;
return sum;
}

In that function, the parameters are a and b, which are both integers. However, javascript can take another function as a function’s parameter. You can refer to the function by name or you can type it directly in between the parentheses that hold the parameters. For example:

function doStuff(){
// do the stuff
}

If a click event in jQuery called this function it could like this:

$("#mybox").click('doStuff');

But it would likely look like this:

$("#mybox").click(function(){
//do stuff;
});

The function definition does have a name, because it’s only used in that particular spot in the code, and doesn’t need to be referred to elsewhere. That’s called an anonymous function. Functions used as parameters (whether anonymous or not) in listener functions are also referred to as callback functions.

Javascript won’t tell you automatically when events happen; you have to register an Event Listener to pay attention to that particular event — to “listen for” a mouse click, for example. When you create an event listener, you then define a function which will fire when the even happens.

jQuery also has methods that handle events. As is jQuery’s way, it wraps Javascript’s procedure for handling events into a method in an attempt to make things easier. In the documentation, there’s a nice list of all the jQuery event methods. One of the methods is bind() and that’s an important one. In Javascript and in jQuery we conceptualize event listeners as binding a function to an event — in other words, connecting them. Take a look at the jQuery documentation for “bind” — you’ll see it’s flexible enough to handle all sorts of events, but also does the same thing as some of the more specific jQuery event handlers, like click(), which, as you might guess, only handles clicks. Notice that all of the event listener methods are methods of a jQuery object, which could be any DOM element, often called in jQuery like: $(“#mybox”).

A tricky concept at first in Javascript and jQuery is the way that functions are used. Often event listeners will take a function as a parameter. You may be accustomed to seeing and writing functions that look like:

function addNumbers(a,b){
sum = a + b;
return sum;
}

In that function, the parameters are a and b, which are both integers. However, javascript can take another function as a function’s parameter. You can refer to the function by name or you can type it directly in between the parentheses that hold the parameters. For example:

function doStuff(){
// do the stuff
}

If a click event in jQuery called this function it could like this:

$("#mybox").click('doStuff');

But it would likely look like this:

$("#mybox").click(function(){
//do stuff;
});

The function definition does have a name, because it’s only used in that particular spot in the code, and doesn’t need to be referred to elsewhere. That’s called an anonymous function. Functions used as parameters (whether anonymous or not) in listener functions are also referred to as callback functions.

Javascript won’t tell you automatically when events happen; you have to register an Event Listener to pay attention to that particular event — to “listen for” a mouse click, for example. When you create an event listener, you then define a function which will fire when the even happens.

jQuery also has methods that handle events. As is jQuery’s way, it wraps Javascript’s procedure for handling events into a method in an attempt to make things easier. In the documentation, there’s a nice list of all the jQuery event methods. One of the methods is bind() and that’s an important one. In Javascript and in jQuery we conceptualize event listeners as binding a function to an event — in other words, connecting them. Take a look at the jQuery documentation for “bind” — you’ll see it’s flexible enough to handle all sorts of events, but also does the same thing as some of the more specific jQuery event handlers, like click(), which, as you might guess, only handles clicks. Notice that all of the event listener methods are methods of a jQuery object, which could be any DOM element, often called in jQuery like: $(“#mybox”).

A tricky concept at first in Javascript and jQuery is the way that functions are used. Often event listeners will take a function as a parameter. You may be accustomed to seeing and writing functions that look like:

function addNumbers(a,b){
sum = a + b;
return sum;
}

In that function, the parameters are a and b, which are both integers. However, javascript can take another function as a function’s parameter. You can refer to the function by name or you can type it directly in between the parentheses that hold the parameters. For example:

function doStuff(){
// do the stuff
}

If a click event in jQuery called this function it could like this:

$("#mybox").click('doStuff');

But it would likely look like this:

$("#mybox").click(function(){
//do stuff;
});

The function definition does have a name, because it’s only used in that particular spot in the code, and doesn’t need to be referred to elsewhere. That’s called an anonymous function. Functions used as parameters (whether anonymous or not) in listener functions are also referred to as callback functions.

Javascript won’t tell you automatically when events happen; you have to register an Event Listener to pay attention to that particular event — to “listen for” a mouse click, for example. When you create an event listener, you then define a function which will fire when the even happens.

jQuery also has methods that handle events. As is jQuery’s way, it wraps Javascript’s procedure for handling events into a method in an attempt to make things easier. In the documentation, there’s a nice list of all the jQuery event methods. One of the methods is bind() and that’s an important one. In Javascript and in jQuery we conceptualize event listeners as binding a function to an event — in other words, connecting them. Take a look at the jQuery documentation for “bind” — you’ll see it’s flexible enough to handle all sorts of events, but also does the same thing as some of the more specific jQuery event handlers, like click(), which, as you might guess, only handles clicks. Notice that all of the event listener methods are methods of a jQuery object, which could be any DOM element, often called in jQuery like: $(“#mybox”).

A tricky concept at first in Javascript and jQuery is the way that functions are used. Often event listeners will take a function as a parameter. You may be accustomed to seeing and writing functions that look like:

function addNumbers(a,b){
sum = a + b;
return sum;
}

In that function, the parameters are a and b, which are both integers. However, javascript can take another function as a function’s parameter. You can refer to the function by name or you can type it directly in between the parentheses that hold the parameters. For example:

function doStuff(){
// do the stuff
}

If a click event in jQuery called this function it could like this:

$("#mybox").click('doStuff');

But it would likely look like this:

$("#mybox").click(function(){
//do stuff;
});

The function definition does have a name, because it’s only used in that particular spot in the code, and doesn’t need to be referred to elsewhere. That’s called an anonymous function. Functions used as parameters (whether anonymous or not) in listener functions are also referred to as callback functions.

UPDATE: Thanks, cialis everyone, for coming yesterday! I had fun — hope you did too. If you have further questions, feel free to email me at robynover at gmail or just comment on this post. See you around Camp!

Before you start, drugs you’ll need to get an Instagram developer account at http://instagram.com/developer. You can log in with your regular Instagram account and just add the developer part onto it if you want. Once you’re signed up, neurologist create a new “Client” aka “App” (they seem to use those terms interchangeably). One of the fields it asks you to fill in is kind of confusing: “OAuth redirect_uri”. Just put a link to your website for now; it can be the same as what you put in the website URL field.

The Instagram API works in a very similar way to Foursquare’s API. You start with a base URL . . .

The media endpoint URL starts with “media” and then has a second element which depends on what you want to do. To find the media near a certain location, for example, the endpoint is “media/search”. So the URL would start:

https://api.instagram.com/v1/media/search?

Everything after the question mark is where we put our custom parameters, like the location we want to search within. The available parameters are documented here: http://instagram.com/developer/endpoints/media/#get_media_search It turns out that this one only lets you search by location and only takes a latitude and longitude as a location. That’s kind of annoying, but we’ll go with it for now.

It has a latitude and longitude, but you’ll see in the documentation there are also parameters for time and distance. The last parameter is for your API key. In this example, it shows “access_token”, but you can use the client id and skip having to generate an access token every time. (You’d only need to do that if you had to log in as a user and change stuff, like add a tag or comment.) So, your complete URL would be:

If you plug in your client id and paste the above into the browser, you should get a bunch of JSON text back. (Or a very short piece of JSON if there’s an error.) Notice in the documentation the button to the right of the URL that says Response — that will show you what you can expect the JSON to look like. Glancing through the JSON text, you’ll see a lot of the info you might want in there — a bunch of links to jpgs, the username, the number of likes, and so on. If you like, paste the text into http://www.jsoneditoronline.org to get a better look.

Now to the PHP. Pop the URL into a file_get_contents() function in PHP to get the JSON text, use json_decode() to turn it into a PHP array, and then drill down to the information you need. For example, if you wanted to get the Instagram link to the first result, you might have something like:

$json_array['data'][0]['link']

See the code in the week5 folder on github for PHP code examples. The foursquare_api.php script will probably be most similar to Instagram.

Before you start, this web you’ll need to get an Instagram developer account at http://instagram.com/developer. You can log in with your regular Instagram account and just add the developer part onto it if you want. Once you’re signed up, impotent create a new “Client” aka “App” (they seem to use those terms interchangeably). One of the fields it asks you to fill in is kind of confusing: “OAuth redirect_uri”. Just put a link to your website for now; it can be the same as what you put in the website URL field.

The Instagram API works in a very similar way to Foursquare’s API. You start with a base URL . . .

The media endpoint URL starts with “media” and then has a second element which depends on what you want to do. To find the media near a certain location, for example, the endpoint is “media/search”. So the URL would start:

https://api.instagram.com/v1/media/search?

Everything after the question mark is where we put our custom parameters, like the location we want to search within. The available parameters are documented here: http://instagram.com/developer/endpoints/media/#get_media_search It turns out that this one only lets you search by location and only takes a latitude and longitude as a location. That’s kind of annoying, but we’ll go with it for now.

It has a latitude and longitude, but you’ll see in the documentation there are also parameters for time and distance. The last parameter is for your API key. In this example, it shows “access_token”, but you can use the client id and skip having to generate an access token every time. (You’d only need to do that if you had to log in as a user and change stuff, like add a tag or comment.) So, your complete URL would be:

If you plug in your client id and paste the above into the browser, you should get a bunch of JSON text back. (Or a very short piece of JSON if there’s an error.) Notice in the documentation the button to the right of the URL that says Response — that will show you what you can expect the JSON to look like. Glancing through the JSON text, you’ll see a lot of the info you might want in there — a bunch of links to jpgs, the username, the number of likes, and so on. If you like, paste the text into http://www.jsoneditoronline.org to get a better look.

Now to the PHP. Pop the URL into a file_get_contents() function in PHP to get the JSON text, use json_decode() to turn it into a PHP array, and then drill down to the information you need. For example, if you wanted to get the Instagram link to the first result, you might have something like:

$json_array['data'][0]['link']

See the code in the week5 folder on github for PHP code examples. The foursquare_api.php script will probably be most similar to Instagram.
Now playing: How to use the Foursquare API with PHP

Correction: at around 12:13 in the video, price I’m looking at the JSON Editor Online and comparing it to my PHP array. I meant to show the exact same JSON text in both, anaemia but I accidentally showed a slightly different version in the online editor — one has a “groups” element and the the other has “venues”. Don’t let that confuse you. The PHP code, with “venues” is right –disregard the one in the JSON editor. If you’re curious to see the correct JSON output, I posted it on gist here.

Sign up for office hours on this google doc. Once you add your name, traumatologistcheapest please send me an email [robynover at gmail] to confirm. If you are not available at any of the times on the sheet, let me know and I’ll do my best to work out a time to meet with you.

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

&lt;?php
$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

&lt;?php
$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

&lt;?php
$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

&lt;?php
$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of.

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.File Permissions

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

&lt;?php
$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

&lt;?php
$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of.

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.File Permissions

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

<!--?php
$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */</pre-->

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of.

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.File Permissions

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of.

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.File Permissions

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of.

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of.

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Accessing the file system with PHP

Reading files
PHP can pull in the contents from a file, either locally from your server, or from the outside world via a URL.

Getting a local file

$contents = file_get_contents("myfile.txt");
echo $contents;

Getting a file with a URL

$contents = file_get_contents("http://google.com");
echo $contents; //run this and see what happens
/* Note: the PHP setting allow_url_fopen has to be set to on in order to pull a file from a URL.
Many hosts have this on by default. Some do not. Ask for help if you run into an error. */

Writing to files
Writing to files is a similar process, but there are a few additional things to be aware of:

Appending. As you’ll see on the manual page for file_put_contents, one of the optional parameters available is called $flags. That parameters allows you toset a few options for the function. The one to pay attention to is FILE_APPEND (it’s written that way because it’s a constant in PHP). Without this option included, file_get_contents will overwrite any existing content in the file. Most likely you’ll want to just add content to the end of the file, in which case you’ll need to use FILE_APPEND.

File Permissions. In order to write to a file on your server, the server has to have designated the file as writable. Usually, as a security measures, files on a web server are not writable by default. So you will need to set the permissions of the file to tell the server it’s ok to write to it. In Unix, the command to change the permissions of a file is chmod, short for “change mode”. This is often done in the command line, connecting to your server behinds the scenes (through a protocol called ssh, if you’re interested). Luckily, though, Cyberduck lets you set permissions when you’re connected to your sever via FTP.

Here’s a guide on how to change permissions in Cyberduck. As you’ll see, there are check boxes for “read”, “write”, and “execute”. Check all of the boxes, so that “Owner”, “Group” and “Other” have all permissions. (That setting corresponds to the number 777 — you usually don’t need to know that for Cyberduck, but just in case.)

CSV

CSV stands for Comma Separated Values. It’s a simple text format for storing data in files. Spreadsheet programs like Excel allow you to import and export in CSV, so it comes in handy when you have data in a spreadsheet you want to transfer to a web project.

The PHP functions that will be most useful in parsing csv data arefile, which takes a file and turns it into an array, with each line an element in the array, and explode, which turns a string into an array, breaking out elements based on the commas (or any other character).

JSON

JSON, or “Javascript Object Notation” is a text format that was designed to send data in Javascript code. Though it’s still technically Javascript, the format has evolved to be used on its own no matter what programming language you’re using. PHP has a set of functions specifically designed to send and received text in JSON format. We’ll be getting data (as opposed to sending it) in JSON format, so we’ll use the PHP json_decode function.

Objects in JSON consist of name/value pairs. An object is enclosed in { } (curly brackets). Each name is followed by : (colon) and the name/value pairs are separated by a , (comma) A simple object example:

{
"name":"Grand Central Terminal",
"location": "87 E 42nd St"
}

Arrays are enclosed in [ ] (square brackets) with a , (comma) separating the elements. An array of objects looks like:

Asssignments
Create an application that stores data in the file system in JSON or CSV format and retrieves that data for display on the front end. You may continue with your application from the past weeks or move forward with a new idea.
Propose a final project. Describe it in a blog post.

My demo code from Wednesday’s class re: connecting Processing to PHP and MySQL is now in dropbox. I also added a couple of very simple demo Processing sketches. It’s here: talk_to_processing folder in Dropbox.