le client http php5

35
Jean-Marie Renouard LightPath 2014©

Upload: jean-marie-renouard

Post on 03-Jul-2015

146 views

Category:

Engineering


1 download

DESCRIPTION

Présentation de l'utilisation du client HTTP de PHP 5

TRANSCRIPT

Page 1: Le client HTTP PHP5

Jean-Marie Renouard LightPath 2014©

Page 2: Le client HTTP PHP5

Le logo PHP est du domaine public http://commons.wikimedia.org/wiki/File:PHP-logo.svg

Ce document est licencié sous licence ◦ Attribution-NonCommercial-ShareAlike

◦ CC BY-NC-SA

Plus de détails: http://creativecommons.org/licenses/by-nc-sa/3.0/fr/

LightPath 2014© - http://www.jmrenouard.fr 2

Page 3: Le client HTTP PHP5

Base de l’accès HTTP

Récupération d’un page WEB

Récupération dans un fichier

Poster des données de formulaire

Nettoyage des données

Positionner des entêtes HTTP

Passer l’authentification

Sécurisation HTTPS

Accès multiples

LightPath 2014© - http://www.jmrenouard.fr 3

Page 4: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 4

Page 5: Le client HTTP PHP5

Curl_init: initialise une session HTTP

Curl_setopt: initialise les options ◦ Entête HTTP

◦ Authentification

◦ User-agent

◦ …

Curl_exec: exécution d’un requête HTTP

Curl_close: fermeture de la session HTTP

LightPath 2014© - http://www.jmrenouard.fr 5

Page 6: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 6

Page 7: Le client HTTP PHP5

<?php // Création d'une nouvelle ressource cURL $ch = curl_init(); // Configuration de l'URL et d'autres options curl_setopt($ch, CURLOPT_URL, "http://www.php.net/"); curl_setopt($ch, CURLOPT_HEADER, 0); // Récupération de l'URL et affichage sur le naviguateur curl_exec($ch); // Fermeture de la session cURL curl_close($ch); ?>

LightPath 2014© - http://www.jmrenouard.fr 7

Page 8: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 8

Page 9: Le client HTTP PHP5

Sortie par défaut, la sortie standard. Possibilité de stocker dans un fichier

$ch = curl_init("http://www.example.com/"); $fp = fopen("page.html", "w"); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);

LightPath 2014© - http://www.jmrenouard.fr 9

Page 10: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 10

Page 11: Le client HTTP PHP5

GET: par défaut

GET; curl_setopt($ch, CURLOPT_HTTPGET, true);

POST: curl_setopt($ch, CURLOPT_POST, true);

PUT :POST: curl_setopt($ch, CURLOPT_PUT, true);

TRACE / HEAD: pas disponible

LightPath 2014© - http://www.jmrenouard.fr 11

Page 12: Le client HTTP PHP5

<input type="text" name="firstName" value="Name">

<input type="text" name=« lastName" value="Name">

LightPath 2014© - http://www.jmrenouard.fr 12

Page 13: Le client HTTP PHP5

Urlencode: fonction d’encodage pour URL.

$post_data['firstName'] = ‘Jean-Marie';

$post_data[‘lastName'] = ‘Renouard‘;

foreach ( $post_data as $key => $value) {

$post_items[] = urlencode($key) . '=' . urlencode($value);

}

$post_string = implode ('&', $post_items);

LightPath 2014© - http://www.jmrenouard.fr 13

Page 14: Le client HTTP PHP5

curl_setopt( $curl_connection,

CURLOPT_POSTFIELDS,

$post_string);

LightPath 2014© - http://www.jmrenouard.fr 14

Page 15: Le client HTTP PHP5

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.php.net/");

curl_setopt($ch, CURLOPT_POST, true);

$post_data['firstName'] = ‘Jean-Marie';

$post_data[‘lastName'] = ‘Renouard‘;

foreach ( $post_data as $key => $value) {

$post_items[] = urlencode($key) . '=' . urlencode($value);

}

$post_string = implode ('&', $post_items);

curl_setopt($ch,CURLOPT_POSTFIELDS, $post_string);

curl_exec($ch);

curl_close($ch);

LightPath 2014© - http://www.jmrenouard.fr 15

Page 16: Le client HTTP PHP5

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.php.net/");

curl_setopt($ch, CURLOPT_POST, true);

$post_data['firstName'] = ‘Jean-Marie';

$post_data[‘lastName'] = ‘Renouard‘;

curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);

curl_exec($ch);

curl_close($ch);

LightPath 2014© - http://www.jmrenouard.fr 16

Page 17: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 17

Page 18: Le client HTTP PHP5

Urlencode: encodage des données POST

Les données postées par CURL doivent être url encodées afin de conserver le format des données.

LightPath 2014© - http://www.jmrenouard.fr 18

Page 19: Le client HTTP PHP5

htmlentities: encodage des caractères en entités HTML

html_entity_decode: décodage des entités HTML.

Idéal, lors de la réception de page Web.

LightPath 2014© - http://www.jmrenouard.fr 19

Page 20: Le client HTTP PHP5

Strip_tags: retrait des tags HTML.

Idéal pour la conservation au format TXT

LightPath 2014© - http://www.jmrenouard.fr 20

Page 21: Le client HTTP PHP5

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.php.net/");

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANFER, true);

$page=curl_exec($ch);

$page=strip_tags($page);

$page= html_entity_decode($page);

curl_close($ch);

file_put_contents(« result.txt », $page);

?>

LightPath 2014© - http://www.jmrenouard.fr 21

Page 22: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 22

Page 23: Le client HTTP PHP5

Indication du navigateur utilisé. ◦ curl_setopt($ch, CURLOPT_USERAGENT,

« Mozilla/5.0 »);

Indication de la page d’origine de l’appel ◦ curl_setopt($ch, CURLOPT_REFERER,

« http://www.google.com/ »);

LightPath 2014© - http://www.jmrenouard.fr 23

Page 24: Le client HTTP PHP5

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

Jusqu’à un certain point, je veux bien suivre:

curl_setopt($ch, CURLOPT_MAXRES, 10);

LightPath 2014© - http://www.jmrenouard.fr 24

Page 25: Le client HTTP PHP5

Il est possible d’ajouter ses entêtes HTTP

$headers= array (

"Content-Type: text/xml; charset=utf-8", "Expect: 100-continue" ) ;

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

LightPath 2014© - http://www.jmrenouard.fr 25

Page 26: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 26

Page 27: Le client HTTP PHP5

2 appels pour initialiser l’authentification type BASIC

Format des données d’authentification : ◦ [login] : [password]

Appels à réaliser: ◦ curl_setopt($curl, CURLOPT_HTTPAUTH,

CURLAUTH_BASIC ) ; ◦ curl_setopt($curl, CURLOPT_USERPWD,

"username:password");

LightPath 2014© - http://www.jmrenouard.fr 27

Page 28: Le client HTTP PHP5

Curl supporte plusieurs type d’authentification.

◦ curl_setopt($curl, CURLOPT_HTTPAUTH, XXXX ) ;

Types d’authentification possibles: ◦ CURLAUTH_BASIC

◦ CURLAUTH_DIGEST

◦ CURLAUTH_GSSNEGOTIATE

◦ CURLAUTH_NTLM

◦ CURLAUTH_ANY: idéal pour passer une authentification

◦ CURLAUTH_ANYSAFE.

LightPath 2014© - http://www.jmrenouard.fr 28

Page 29: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 29

Page 30: Le client HTTP PHP5

Activation du HTTPS sans vérification.

◦ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

LightPath 2014© - http://www.jmrenouard.fr 30

Page 31: Le client HTTP PHP5

Récupérer le certificat X509 de l’autorité de certification

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_CAINFO, SECURECA.crt");

LightPath 2014© - http://www.jmrenouard.fr 31

Page 32: Le client HTTP PHP5

LightPath 2014© - http://www.jmrenouard.fr 32

Page 33: Le client HTTP PHP5

Indication du proxy HTTP et de l’URL

options = array(

CURLOPT_HTTPPROXYTUNNEL=> 1,

CURLOPT_PROXYTYPE => 'URLPROXY_HTTP',

CURLOPT_PROXY => 'url_proxy:port'

);

curl_setopt_array($ch, $options);

LightPath 2014© - http://www.jmrenouard.fr 33

Page 34: Le client HTTP PHP5

$options = array(

CURLOPT_PROXYTYPE => 'URLPROXY_HTTP',

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

CURLOPT_PROXY => 'url_proxy:port',

CURLOPT_PROXYAUTH =>'URLAUTH_ANY',

CURLOPT_PROXYUSERPWD => $userpwd

);

curl_setopt_array($ch, $options);

LightPath 2014© - http://www.jmrenouard.fr 34

Page 35: Le client HTTP PHP5

LightPath: ◦ Société de conseil et d’ingénierie

◦ Formations, Conseil, Audit et mise en œuvre

[email protected]

Jean-Marie RENOUARD ◦ [email protected]

◦ Twitter: @jmrenouard

◦ http://www.jmrenouard.fr

LightPath 2014© - http://www.jmrenouard.fr 35