Using cURL (in PHP) to access http s url is often not as simple as using the proper url. Utilisation de cURL (en PHP) pour l'accès http s url n'est souvent pas aussi simple que d'utiliser la bonne url. Using it for authentication is also not very clearly documented. L'utiliser pour l'authentification n'est pas non plus très clairement documenté. This is a mini tutorial for both accessing https url’s as well as for http authentication. Il s'agit d'un mini tutoriel pour les accès aux URL https ainsi que pour l'authentification HTTP.

The following is a simple example which show the most common options you will ever need to use to access https url’s as well as for http authentication. Ce qui suit est un exemple simple qui montrent le plus commun des options dont vous aurez besoin d'utiliser https pour accéder à l'url ainsi que pour l'authentification HTTP.

// The usual - init a curl session and set the url / / Comme d'habitude - init curl une session et mettre l'url
$ch = curl_init(); $ ch = curl_init ();
curl_setopt($ch, CURLOPT_URL, $base_url); curl_setopt ($ ch, CURLOPT_URL, $ base_url);

// Set your login and password for authentication / / Choisir votre login et mot de passe pour l'authentification
curl_setopt($ch, CURLOPT_USERPWD, ‘login:pasword’); curl_setopt ($ ch, CURLOPT_USERPWD, 'login: mot de passe');

// You can use CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, / / Vous pouvez utiliser CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE,
// CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE / / CURLAUTH_NTLM, CURLAUTH_ANY et CURLAUTH_ANYSAFE
// / /
// You can use the bitwise | (or) operator to combine more than one method. / / Vous pouvez utiliser le binaire | (ou) l'opérateur de combiner plus d'une méthode.
// If you do this, CURL will poll the server to see what methods it supports and pick the best one. / / Si vous faites cela, CURL interrogera le serveur pour voir quelles méthodes il soutient et choisir la meilleure.
// / /
// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | / / CURLAUTH_ANY est un alias pour CURLAUTH_BASIC | CURLAUTH_DIGEST |
// CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM / / CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
// / /
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | / / CURLAUTH_ANYSAFE est un alias pour CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE |
// CURLAUTH_NTLM / / CURLAUTH_NTLM
// / /
// Personally I prefer CURLAUTH_ANY as it covers all bases / / Personnellement, je préfère CURLAUTH_ANY car il couvre toutes les bases
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt ($ ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

// This is occassionally required to stop CURL from verifying the peer’s certificate. / / Ce sont parfois nécessaires pour arrêter CURL de vérifier le certificat par les pairs.
// CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if / / CURLOPT_SSL_VERIFYHOST mai doivent également être TRUE ou FALSE si
// CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a / / CURLOPT_SSL_VERIFYPEER est désactivée (valeur par défaut est de 2 - vérifier l'existence d'un
// common name and also verify that it matches the hostname provided) / / Nom commun et vérifier qu'il correspond au nom d'hôte fourni)
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false);

// Optional: Return the result instead of printing it / / Facultatif: Retourne le résultat au lieu de l'imprimer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1);

// The usual - get the data and close the session / / Comme d'habitude - récupérer les données et de fermer la session
$data = curl_exec($ch); $ data = curl_exec ($ ch);
curl_close($ch); curl_close ($ ch);

Use the above as a template for your code to simplify your data access using cURL. Utilisez le dessus comme un modèle pour votre code afin de simplifier votre accès aux données en utilisant cURL.

PS. The challenge with cURL documentation in PHP is that it is hard to find what you need from hundreds of available options and without enough examples of common use cases. Le défi cURL avec la documentation en PHP est qu'il est difficile de trouver ce dont vous avez besoin de centaines d'options disponibles et sans suffisamment d'exemples de cas d'utilisation commune. What is needed is a series of How-To’s like the mini-tutorial above. Ce qu'il faut, c'est une série de How-To's comme le mini-tutoriel ci-dessus.