How To Use cURL (in PHP) For Authentication And SSL Communication �p��ϥα����]�bPHP �^�i�����ҩMSSL�q�H
Using cURL (in PHP) to access http s url is often not as simple as using the proper url.�ϥα����]�bPHP �^�ӳX�ݪ�HTTP s���}�������O����²��A�ϥΥ��T�����}�C Using it for authentication is also not very clearly documented.�Υ��i�����ҡA�]�S���ܲM�����O��C This is a mini tutorial for both accessing https url's as well as for http authentication.�o�O�@�Ӥp���е{���q�Lhttps�X�ݺ��}���A�H�ά�HTTP�{�ҡC
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.�H�U�O�@��²�檺�Ҥl��ܡA�̱`������ܡA�z�N�H������ɭԳ��ݭn�ϥΦs��q�Lhttps URL���A�H�ά�HTTP�{�ҡC
// The usual - init a curl session and set the url / /�D�`-i nit���@�����|ij�A�ó]�m���}
$ch = curl_init();��CH = curl_init �] �^ ;
curl_setopt($ch, CURLOPT_URL, $base_url); curl_setopt �]�����Ϸ|��A curlopt_url �A��base_url �^ ;
// Set your login and password for authentication / /�]�m�z���n��W�M�K�X����
curl_setopt($ch, CURLOPT_USERPWD, 'login:pasword'); curl_setopt �]�����Ϸ|��A curlopt_userpwd �A '�n��G pasword ' �^ ;
// You can use CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, / /�z�i�H�ϥ�curlauth_basic �A curlauth_digest �A curlauth_gssnegotiate �A
// CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE / / curlauth_ntlm �A curlauth_any �A curlauth_anysafe
// / /
// You can use the bitwise | (or) operator to combine more than one method. / /�z�i�H�ϥΦ�B���| �]�Ρ^�g��̵��X�_�ӡA����@�ؤ�k�C
// If you do this, CURL will poll the server to see what methods it supports and pick the best one. / /�p�G�A�o�˰��A�����A�N�լd�A�Ⱦ��A�ݬݦ������k�A�����M�D��䤤�̦n���@�ӡC
// / /
// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | / / curlauth_any�O�@�ӧO�W��curlauth_basic | curlauth_digest |
// CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM / / curlauth_gssnegotiate | curlauth_ntlm
// / /
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | / / curlauth_anysafe�O�@�ӧO�W��curlauth_digest | curlauth_gssnegotiate |
// CURLAUTH_NTLM / / curlauth_ntlm
// / /
// Personally I prefer CURLAUTH_ANY as it covers all bases / /�ڭӤH���wcurlauth_any �A�]�����[�\�Ҧ���a
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt �]�����Ϸ|��A curlopt_httpauth �A curlauth_any �^ ;
// This is occassionally required to stop CURL from verifying the peer's certificate. / /�o�O�����|�n�D������q�ֹ�P�������ҡC
// CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if / / curlopt_ssl_verifyhost�i���ٻݭn�o��u�ΰ��A�p�G
// CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a / / curlopt_ssl_verifypeer�Q�T�Ρ]�q�{�Ȭ�2 -�ˬd�s�b�@��
// common name and also verify that it matches the hostname provided) / /�q�ΦW�١A�]���ҥ��O�_�ŦX�Ҵ��Ѫ��D���W�^
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt �]�����Ϸ|��A curlopt_ssl_verifypeer �A�갲�^ ;
// Optional: Return the result instead of printing it / /�i��G��^�����G�A�Ӥ��O�����L��
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt �]�����Ϸ|��A curlopt_returntransfer �A 1 �^ ;
// The usual - get the data and close the session / /�D�`-���ƾڨ������|ij
$data = curl_exec($ch);���ƾ�= 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.�ϥΤW�z�@���d���A���z���N�X�H²�Ʊz���ƾڦs��ϥα����C
PS.�O�w���`�����Ѫ�C 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.�D�ԻP���������bPHP�O�A�o�O������z�һݭn���q�Ʀʭӥi�ѿ�ܡA�èS���������Ҥl�A�@�P�ϥΪ��ץ�C What is needed is a series of How-To's like the mini-tutorial above.�һݭn���O�@�t�C���p��쪺�@�ˡA�p�ɲߥH�W�C
Filed under���_�U Computer Security�p����w�� , �A Headline News�Y��s�D , �A How To�p�� , �A PHP PHP�� , �A Tech Note�N���� , �A Web���� , �A Web Services Web�A�� | |
| |
RSS 2.0 2.0 | |
Trackback Trackback��踪 this Article |���峹|
Email this Article�q�l�l�峹
You may also like to read�z�]�i�H�QŪ |





































October 31st, 2006 at 10:06 pm 2006�~10��31��b�U��10��06��
[...] Let��s take a PHP script that does a number of CURL calls as an example. [ �K �K ]��ڭ̱Ĩ�PHP�}���A��o�@�DZ����I�~�@���@�ӨҤl�C 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����z�ϥ�libcurl�@�ӯu���j�j���u��A�n�D��L�������A Web�A�ȡA RSS Feeds�\��A�L�ק_�h�A�i�H�ڷQ�A�b�z��PHP�N�X�C 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.�o�g�峹���ت��ëD���ױ����A�ҥH�ڤ��|�i�J�ԲӡA��W�O�������\��A��z���N�X�H���X�ШD�A����o�^���q�����N���@���s��C You can then parse the results use the data on your site.�M��A�z�N�i�H�ѪR���G�Q�γo�Ǽƾڦb�z�������W�C [...] [ �K �K ]
November 2nd, 2006 at 4:48 am 2006�~11��2��b�W��04��48��
[...] [...] [ �K �K ] [ �K �K ]
November 28th, 2007 at 12:02 pm 2007�~11��28��b�U��12��02��
Thank you for this article and all of the information you��ve provided. �P�§A���ڳo�����峹�M�Ҧ��z�Ҵ��Ѫ��H���C
After I had my prototype remote log-in system working, I moved it to a secure server, and nothing worked anymore.��A�ڦ��ڪ��쫬�A���{�n��b�t�Τu�@�A�ڴ��X���@�Ӧw�����A�Ⱦ����A�èS���u�@�F�C
Then I Googled for a couple of hours, until I found this page.�M���googled���X�Ӥp�ɡA����ڵo�{�o�@���C Awesome!�i�Ȫ��I Everything is working again.�@���u�@�C
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.�O���A�z�O���勵�T���G�o�O�O�L���A����z�չϧ�X�䤤��������ܾA�ΡA�÷|��ڤu�@�P����S�w�����p�C
Hats off to you!�b�Ʈ��p�����p�d���z�I You made my day!�A�ڪ��ѡI
January 15th, 2008 at 2:46 pm 2008�~1��15��b�U��2��46��
Thanks for the info.�P�«H���C
One small thing: curl_exec() should have $ch as the parameter:�@�p���ơG curl_exec �] �^���Ӧ�����CH�@���ѼơG
$data = curl_exec($ch);���ƾ�= curl_exec �]��CH �^ ;January 15th, 2008 at 10:50 pm 2008�~1��15��b�U��10��50��
Thanks.���¡C Corrected.�ȥ��C
March 29th, 2008 at 6:22 am 2008�~3��29��b�W��06��22��
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | / / curlauth_anysafe�O�@�ӧO�W��curlauth_digest | curlauth_gssnegotiate |
// CURLAUTH_NTLM / / curlauth_ntlm
// / /
problem pls check it�K�~�S�����լd�����D�i���ˬd�K
May 7th, 2008 at 9:00 am 2008�~5��7��W��9:00
hello,��A
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,�ڴ����կQ�Ԧc�^�X�u�h���@�Ӥ��P�������A��A���y�k��~�]�N�Q���쪺' �G '�b�o�譱��������u�A�b���u�A�ڭ̬O���F�ڭ̪��s��Τ�W�M�K�X�n���Ӻ�����..�ڬݨӨө������P���n��u�h�ڷ|�P�E�A�p�Gu�i�H���U�A
Thanks and regards,�P�©M�ݭԡA
Rahul..�ԭJ��..