How To Use cURL (in PHP) For Authentication And SSL Communication Comment utiliser cURL (en PHP) pour l'authentification et la communication SSL
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.
Filed under Classé sous Computer Security La sécurité informatique , Headline News Headline News , How To Comment , PHP , Tech Note Note technique , Web , Web Services Services Web | |
| |
RSS 2.0 RSS 2,0 | |
Trackback this Article | cet article |
Email this Article Envoyer cet article
You may also like to read Vous mai également à lire |





October 31st, 2006 at 10:06 pm Octobre 31st, 2006 at 10:06 pm
[...] Let’s take a PHP script that does a number of CURL calls as an example. [...] Prenons un script PHP qui fait un certain nombre de CURL appels à titre d'exemple. PHP gives you access to libcurl a really powerful tool for calling up other web pages, web services, RSS feeds, and whatever else you can dream up, right in your PHP code. PHP vous donne accès à libcurl vraiment un outil puissant pour appeler d'autres pages Web, services Web, flux RSS, et quelle que soit d'autre que vous pouvez imaginer, dans votre code PHP. This article is not a general introduction to CURL, so I won’t go into detail, but basically the CURL functions allow your code to make requests and get responses from web sites just like a browser. Cet article n'est pas une introduction générale à CURL, donc je ne vais pas entrer dans les détails, mais essentiellement les fonctions CURL votre code permettra de faire des demandes et d'obtenir des réponses de sites Web comme un navigateur. You can then parse the results use the data on your site. Vous pouvez ensuite analyser les résultats utiliser les données sur votre site. [...]
November 2nd, 2006 at 4:48 am 2 novembre 2006 à 4:48 am
[...] [...] [...] [...]
November 28th, 2007 at 12:02 pm Novembre 28th, 2007 at 12:02 pm
Thank you for this article and all of the information you’ve provided. Merci pour cet article et toutes les informations que vous nous avez fournie.
After I had my prototype remote log-in system working, I moved it to a secure server, and nothing worked anymore. Après j'ai eu mon prototype journal distant en système de travail, je suis passé à un serveur sécurisé, et rien de plus travaillé.
Then I Googled for a couple of hours, until I found this page. Puis j'ai googlé pour un couple d'heures, jusqu'à ce que je trouve cette page. Awesome! Génial! Everything is working again. Tout fonctionne à nouveau.
Yes, you are absolutely right: It’sa jungle out there when you’re trying to find which CURL options are applicable and will actually work with any given situation. Oui, vous avez absolument raison: C'est une jungle là-bas lorsque vous êtes en train d'essayer de trouver des options qui CURL sont applicables et en fait travailler avec une situation donnée.
Hats off to you! Coup de chapeau à vous! You made my day! Vous avez fait ma journée!
January 15th, 2008 at 2:46 pm Janvier 15, 2008 à 2:46 pm
Thanks for the info. Merci pour l'info.
One small thing: curl_exec() should have $ch as the parameter: Une petite chose: curl_exec () aurait dû $ ch comme paramètre:
$data = curl_exec($ch);$ data = curl_exec ($ ch);January 15th, 2008 at 10:50 pm 15 janvier 2008 à 10:50 PM
Thanks. Merci. Corrected. Rectifié.
March 29th, 2008 at 6:22 am 29 mars 2008 à 6:22 am
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | / / CURLAUTH_ANYSAFE est un alias pour CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE |
// CURLAUTH_NTLM / / CURLAUTH_NTLM
// / /
problem pls check it… problème PLS vérifier…
May 7th, 2008 at 9:00 am 7e mai, 2008 à 9:00 am
hello, Bonjour,
I tried ur code for a different website but it says there is syntax error(unexpected ‘:’ in that specific line in the line which we are supposed to edit our username and password to that site.. as i looking fro such similar login codes i would be thankful if u could help, J'ai essayé ur code pour un autre site Web, mais il y est dit erreur de syntaxe (inattendu ':' que dans la ligne spécifique dans la ligne qui nous sommes censés modifier notre nom d'utilisateur et mot de passe pour ce site .. i vient chercher ces mêmes codes d'accès Je vous serais reconnaissant si u pourrait aider,
Thanks and regards, Merci et salutations,
Rahul.. Rahul ..