Web Service REST: Client
Web Service REST: Client
Client
Plan
En Java
Prérequis
Exemples
Etapes
En PHP
Prérequis
Exemples
Etapes
2
En Java - Prérequis
3
En Java - Prérequis
Jersey Client
Pour utiliser Jersey Client; il faudrait ajouter sa librairie au
projet.
4
En Java - Exemples
MyBean bean =
target.request(MediaType.APPLICATION_XML_TYPE)
.post(Entity.entity(form,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
MyBean.class);
5
En Java - Exemples
Exemple détaillé
ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MyClientResponseFilter.class);
clientConfig.register(new AnotherClientFilter());
Invocation.Builder invocationBuilder =
helloworldWebTargetWithQueryParam.request(MediaType.TEXT_PLAIN_TYPE);
invocationBuilder.header("some-header", "true");
6
En Java - Exemples
Exemple compact
Client client = ClientBuilder.newClient(new ClientConfig()
.register(MyClientResponseFilter.class)
.register(new AnotherClientFilter()));
7
En Java - Etapes
2) Cibler la ressource
WebTarget webTarget = client.target("http://example.com/rest");
3) Identifier la ressource
WebTarget resourceWebTarget = webTarget.path("resource");
8
Plan
En Java
Prérequis
Exemples
Etapes
En PHP
Prérequis
Exemples
Etapes
9
En PHP - Prérequis
10
En PHP - Exemples
Exemple GET
$service_url =
'http://example.com/api/conversations/125/messages&apikey=145';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' .
var_export($info));
}
curl_close($curl);
echo $curl_response)
echo 'response ok!';
11
En PHP - Exemples
Exemple POST
$service_url = 'http://example.com/api/conversations';
$curl = curl_init($service_url);
$curl_post_data = array(
'message' => 'test message',
'useridentifier' => '[email protected]',
'department' => 'departmentId001',
'subject' => 'My first conversation',
'recipient' => '[email protected]',
'apikey' => 'key001'
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;
12
En PHP - Exemples
Exemple PUT
$service_url = 'http://example.com/api/conversations/cid123/status';
$ch = curl_init($service_url);
13
En PHP - Exemples
Exemple DELETE
$service_url = 'http://example.com/api/conversations/25';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$curl_post_data = array(
'note' => 'this is spam!',
'useridentifier' => '[email protected]',
'apikey' => 'key001'
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
$response = curl_exec($ch);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
echo $curl_response;
14
En PHP - Etapes
1) Initialisation de la connexion
$curl = curl_init($service_url);
15
Références
Protocole HTTP
https://fr.wikipedia.org/wiki/Hypertext_Transfer_Protocol
Liste des codes de réponse HTTP
https://fr.wikipedia.org/wiki/Liste_des_codes_HTTP
16