Asked 8 years ago
19 Oct 2016
Views 3225
ching

ching posted

How To Use cURL with PHP For Authentication And SSL Communication

i want to connect with remote window server and so i am using cURL for it but simple connection not working well but i think it need SSL connection , so how to connect with SSL site and make Authentication by cURL ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

check below code



$url="https://something.com";//place host here
$username="";//username for authentication
$password="";//password for authentication
$conn= curl_init();
curl_setopt($conn, CURLOPT_RETURNTRANSFER, true);
curl_setopt($conn, CURLOPT_URL, $url);
curl_setopt($conn, CURLOPT_HTTPHEADER, array("METHOD : POST"));//put other header like content-type etc..
curl_setopt($conn, CURLOPT_POSTFIELDS, $data_string);//if post method
curl_setopt($conn, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($conn, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($conn, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($conn);
curl_close ($conn);



i used here CURLAUTH_NTLM for HTTP AUTH . HTTP NTLM authentication. A proprietary protocol invented and used by Microsoft.
https://curl.haxx.se/libcurl/c/CURLOPT_HTTPAUTH.html


curl_setopt($conn, CURLOPT_USERPWD, $username.":".$password);


CURLOPT_USERPWD used to set username and password and format is "[username]:[password]" to use for the connection.


curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false); why it is false , it should be true to check ssl isnt it ? - ching  
Oct 19 '16 13:51
Mahesh Radadiya

Mahesh Radadiya
answered Nov 30 '-1 00:00


curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, false);


CURLOPT_SSL_VERIFYPEER says to cURL to verify the peer certificate , its default value is true need to override by setting it false .either it certificates to verify against can be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option.
Post Answer