HTTP basic authentication using CURL PHP

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

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

1) Initialise 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())

Example:



            $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);
            if(curl_errno($ch)){
                  echo 'Request Error:' . curl_error($ch);
            }
            else{
                  curl_close($ch);
                  print_r($record);
            } 
Pankaj Singh Sugara

Author Pankaj Singh Sugara

Experienced Software Engineer with a demonstrated history of working in the marketing and advertising industry. Skilled in SQL, Web Applications, PHP, WordPress, and Joomla. Strong engineering professional with a B-TECH focused in Information Technology from JCDM College of Engineering Sirsa, Haryana.

More posts by Pankaj Singh Sugara