le client http php5

Post on 03-Jul-2015

146 Views

Category:

Engineering

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Jean-Marie Renouard LightPath 2014©

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

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

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

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

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

<?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

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

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

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

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

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

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

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

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

curl_setopt( $curl_connection,

CURLOPT_POSTFIELDS,

$post_string);

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

$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

$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

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

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

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

Strip_tags: retrait des tags HTML.

Idéal pour la conservation au format TXT

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

<?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

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

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

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

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

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

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

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

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

Activation du HTTPS sans vérification.

◦ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

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

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

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

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

$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

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

◦ Formations, Conseil, Audit et mise en œuvre

◦ jmrenouard@lightpath.fr

Jean-Marie RENOUARD ◦ jmrenouard@gmail.com

◦ Twitter: @jmrenouard

◦ http://www.jmrenouard.fr

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

top related