HTTP basic authentication using CURL PHP

CURL is a library that allows you to connect and communicate to many different types of servers with many different types of protocols. Or wecan say CURl is library through which we can make HTTP request in PHP.

In order to use CURL funcion you need to install the libcurl package.
The basic steps of using CURl function in PHP are:

1) Initialize a curl session i.e. (curl_init())
2) Set various options for the session i.e.(curl_setopt()).
3) Execute and fetch/send data from/to server i.e.(curl_exec())
4) Close the session i.e. (curl_close())

            $request = 'req';
	    $u_id = 12345;
	    $webkey = 'asddAA12hhsh';	    
            param = array('request'=>$request,'uid'=>$u_id,'webkey'=>$webkey);            
            $cookies = 'CookieName1=Value;CookieName2=Value';
            $curl = 'https://www.localhost/abc';                	
            $data_string = json_encode($param);   
            curl_setopt($ch, CURLOPT_URL,$curl);  

            curl_setopt($ch, CURLOPT_COOKIE, $cookies);
            curl_setopt($ch, CURLOPT_HEADER, false);          
            curl_setopt($ch, CURLOPT_POST, true);                                                                    
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);         

            $output = @curl_exec($ch); // execute
            $record = json_decode($output, true);
                  echo 'Request Error:' . curl_error($ch);
Pankaj Singh Sugara

Author Pankaj Singh Sugara

More posts by Pankaj Singh Sugara