Escolar Documentos
Profissional Documentos
Cultura Documentos
❖ Atomic
❖ Cacheable
❖ A uniform interface
❖ Layered
❖ Code on demand
Now, what does
that sound like?
REST!
And, that’s all I have
to say about that,
too.
Our focus today...
❖ Methods
❖ Status Codes
❖ HTTP in PHP
Defining safe &
idempotent
methods
Safe methods
❖ Retrieval of information
❖ Idempotent
HEAD
❖ Identical to GET, except...
❖ Idempotent
POST
❖ Not safe
❖ Non-idempotent
PUT
❖ Opposite of GET
❖ Storage of information
❖ Idempotent
DELETE
❖ Idempotent
Other methods
❖ OPTIONS
❖ TRACE
❖ CONNECT
Status codes
❖ Informational (1xx)
❖ Successful (2xx)
❖ Redirection (3xx)
HTTP/1.x 200 OK
Date: Thu, 21 May 2009 23:08:35 GMT
Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0RC2
X-Powered-By: PHP/5.3.0RC2
Content-Type: video/mp4
Content-Length: 115910000
{binary data}
201 Created
1
HEAD /2390/2253727548_a413c88ab3_s.jpg
HTTP/1.1
Host: farm3.static.flickr.com
2
HTTP/1.0 200 OK
Date: Mon, 05 May 2008 00:33:14 GMT
Server: Apache/2.0.52 (Red Hat)
Accept-Ranges: bytes
Content-Length: 3980
Content-Type: image/jpeg
3
{binary data}
Redirection (3xx)
303 See Other
❖ The response to your request can be
found at another URL identified by the
Location header
❖ The client should make a GET request
on that URL
❖ The Location is not a substitute for this
URL
307 Temporary
Redirect
❖ The resource resides temporarily at the
URL identified by the Location
❖ The Location may change, so don’t
update your links
❖ If the request is not GET or HEAD, then
you must allow the user to confirm the
action
302 Found
❖ The resource has been found at another
URL identified by the Location header
❖ The new URL might be temporary, so the
client should continue to use this URL
❖ Redirections SHOULD be confirmed by
the user (in practice, browsers don’t
respect this)
301 Moved
Permanently
❖ The resource has moved permanently to
the URL indicated by the Location
header
❖ You should update your links accordingly
❖ 410 Gone
❖ 411 Length Required
HTTP/1.1 200 OK
Date: Thu, 21 May 2009 21:01:06 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.5 mod_ssl/2.2.9
OpenSSL/0.9.8g
X-Powered-By: PHP/5.2.5
Set-Cookie: PHPSESSID=eeeff50d3b6ae241c934a5c2671b0005;
expires=Sun, 21 Jun 2009 21:01:07 GMT; path=/;
domain=.phparch.com
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate,
post-check=0, pre-check=0
Pragma: no-cache
Content-Type: text/html; charset=utf-8
❖ Streams
http://php.net/streams
header('HTTP/1.x 201 Created');
header('Content-Type: application/xml');
header('Location: http://example.org/content/videos/1234');
echo $htmlBody;
?>
HTTP/1.1 201 Created
Date: Fri, 12 Jun 2009 13:53:38 GMT
Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.0RC2
X-Powered-By: PHP/5.3.0RC2
Location: http://example.org/content/videos/1234
Content-Length: 120
Content-Type: text/html
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,
"http://twitter.com/direct_messages/new.json");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $dm);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD,
"{$username}:{$password}");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Suppress the Expect: 100-continue header that
// cURL tries to send
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
$response = curl_exec($curl);
curl_close($curl);
{"text":"Hi! I'm using your curl code!","sender":
{"notifications":false,"profile_text_color":"333333","profile_image_url":"http:\/\/
s3.amazonaws.com\/twitter_production\/profile_images\/66941217\/
phpc_normal.png","description":"PHPC is a gathering place for the PHP community. It is about
community and friendship.","profile_background_image_url":"http:\/\/s3.amazonaws.com\/
twitter_production\/profile_background_images\/3561102\/
php2.png","utc_offset":-18000,"time_zone":"Eastern Time (US & Canada)","created_at":"Tue Dec 09
18:05:32 +0000
2008","profile_link_color":"333366","screen_name":"phpc","profile_background_tile":false,"follow
ers_count":2163,"profile_background_color":"000000","url":"http:\/\/phpcommunity.org
\/","name":"PHP Community","friends_count":2146,"protected":false,"statuses_count":
213,"profile_sidebar_fill_color":"d4d5e8","profile_sidebar_border_color":"333333","following":fa
lse,"favourites_count":0,"location":"#phpc on Freenode IRC","id":
17997273,"verified_profile":false},"created_at":"Fri Jun 12 14:08:34 +0000 2009","sender_id":
17997273,"sender_screen_name":"phpc","recipient_screen_name":"ramsey","recipient_id":
7794552,"id":170497722,"recipient":
{"notifications":false,"profile_text_color":"000000","profile_image_url":"http:\/\/
s3.amazonaws.com\/twitter_production\/profile_images\/81619004\/bramsey-
square_normal.png","description":"Dad, Software Architect, PHP, XML, web services, beer drinker,
libertarian","profile_background_image_url":"http:\/\/static.twitter.com\/images\/themes\/
theme1\/bg.gif","utc_offset":-18000,"time_zone":"Eastern Time (US & Canada)","created_at":"Sun
Jul 29 02:44:40 +0000
2007","profile_link_color":"0066CC","screen_name":"ramsey","profile_background_tile":false,"foll
owers_count":817,"profile_background_color":"666666","url":"http:\/\/benramsey.com
\/","name":"Ben Ramsey","friends_count":187,"protected":false,"statuses_count":
5062,"profile_sidebar_fill_color":"99ff66","profile_sidebar_border_color":"33cc00","following":
0,"favourites_count":23,"location":"Atlanta, GA, US","id":7794552,"verified_profile":false}}
Questions?
❖ My website is benramsey.com
For uses not covered under this license, please contact the
author.