jQuery, REST, mashup, cURL
(cenni)
2
JavaScript framework
Esistono dei framework di sviluppo per evitare di
“reinventare la ruota”
Esempi di framework
jQuery
Bootstrap
Angular.js
Node.js
Inoltre, questi framework risolvono problemi
cross-platform e supportano diversi browsers
3
jQuery
È una libreria JavaScript
Semplifica lo scripting client-side
È free e open source
È rilasciata sotto licenza MIT e GPL v.2
4
jQuery: ne fanno uso
Dell
Major League Baseball
Bank of America
Drupal.org
Digg
Wordpress
NBC
e altri ancora ...
5
jQuery: installazione
Download the jQuery library from jQuery.com
<head>
<script src="jquery-1.12.2.min.js"></script>
</head>
Include jQuery from a CDN, like Google
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"
>
</script>
</head>
6
jQuery: sintassi
La sintassi di jQuery permette di selezionare
elementi HTML e di eseguire azioni su di essi
$(selector).action()
$ per definire/accedere a jQuery
(selector) per selezionare gli elementi HTML
action() per specificare l'azione da svolgere
Si può agire solo su elementi HTML già caricati nella pagina corrente Vedi: http://www.w3schools.com/jquery/jquery_syntax.asp
7
jQuery: selettori
Vedi: http://www.w3schools.com/jquery/jquery_selectors.asp
8
jQuery: eventi
In jQuery, la maggior parte degli eventi del DOM hanno un metodo jQuery equivalente
Eventi DOM
9
jQuery: eventi
In jQuery, la maggior parte degli eventi del DOM hanno un metodo jQuery equivalente
Metodi jQuery
10
Document Ready Event
Spesso i metodi jQuery sono all'interno dell'evento ready per evitare al codice jQuery di essere essere eseguito prima del caricamento del documento
$(document).ready(function(){
// metodi jQuery qui...
})
11
$.ajax() Function
La funzione $.ajax() permette di fare chiamate HTTP asincrone (http://api.jquery.com/jquery.ajax/)
$.ajax(url [, options] )
$.ajax([options])
url: indirizzo della chiamata
success: funzione da invocare se la richiesta va a buon fine
error: funzione da invocare se fallisce la richiesta
type: GET o POST
data: dati da inviare come parametro della richiesta
13
REST
Con Representational State Transfer (REST) si intende un tipo di architettura software per i sistemi distribuiti
Il termine è stato introdotto nel 2000 nella tesi di dottorato di Roy Fielding, uno dei principali autori delle specifiche del protocollo HTTP
http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm
14
REST
“...The World Wide Web represents the largest implementation of a system conforming to the REST architectural style.
REST-style architectures conventionally consist of clients and servers. Clients initiate requests to servers; servers process requests and return
appropriate responses. Requests and responses
are built around the transfer of representations of
resources.”
15
REST
I servizi vengono invocati usando i metodi del protocollo HTTP
Generalmente i risultati sono restituiti in XML o JSON
Il parsing dei risultati può essere fatto lato
client (con Ajax e JavaScript) oppure lato
server
16
REST
17
Mashup
L'uso di API REST permette di realizzare dei mashup
“... A mashup, in web development, is a web page, or web application, that uses and combines data, presentation or functionality from two or more sources to create new
services. The term implies easy, fast integration,
frequently using open application programming interfaces (API) and data sources to produce enriched results that were not necessarily the original reason for producing the raw source data.”
Fonte: http://en.wikipedia.org/wiki/Mashup_web_application_hybrid
18
Mashup lato server e lato client
19
Esempio di mashup lato client
Uso di geolocation HTML5
API di GoogleMaps
API REST di openWeatherMap
http://openweathermap.org/
20
Mashup lato server
I programmi sul server devono invocare servizi remoti, ricevere i dati e trasformarli in HTML per il client
La comunicazione server-to-server in PHP si può ottenere usando le librerie per cURL
“PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP
uploading (this can also be done with PHP's ftp extension), HTTP form based upload,” proxies, cookies, and user+password
21
Scenario cURL
Richiesta HTTP Risposta HTTP
web server 1
web server 2 cURL permette di
recuperare il contenuto di una pagina web (scraping)
effettuare il submit di un form effettuare autenticazioni
connettersi a servizi remoti
22
Scenario cURL
curl_init()
inizializza una sessione cURL
curl_setopt()
specifica le opzioni per la sessione
curl_exec()
esegue la chiamata vera e propria
curl_close()