How to get cookies from curl into a variable in PHP ? Last Updated : 31 Dec, 2019 Comments Improve Suggest changes Like Article Like Report The cURL standing for Client URL refers to a library for transferring data using various protocols supporting cookies, HTTP, FTP, IMAP, POP3, HTTPS (with SSL Certification), etc. This example will illustrate how to get cookies from a PHP cURL into a variable. The functions provide an option to set a callback that will be called for each response header line. The function will receive the curl object and a string with the header line. Along with their purpose, required for this example are described below: curl_init(): Used to initialize a curl object. curl_setopt(object, parameter, value): Used to set the value of parameter for a certain curl object. curl_exec(object): Used to execute the current curl session. Called after setting the values for desired curl parameters. preg_match_all(regExp, inputVariable, OutputVariable): Used to perform a global regular expression check. Example 1: This example illustrates how to fetch cookies from www.amazon.com php <?php // URL to fetch cookies $url = "https://www.amazon.com/"; // Initialize cURL object $curlObj = curl_init(); /* setting values to required cURL parameters. CURLOPT_URL is used to set the URL to fetch CURLOPT_RETURNTRANSFER is enabled curl response to be saved in a variable CURLOPT_HEADER enables curl to include protocol header CURLOPT_SSL_VERIFYPEER enables to fetch SSL encrypted HTTPS request.*/ curl_setopt($curlObj, CURLOPT_URL, $url); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_HEADER, 1); curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($curlObj); // Matching the response to extract cookie value preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $match_found); $cookies = array(); foreach($match_found[1] as $item) { parse_str($item, $cookie); $cookies = array_merge($cookies, $cookie); } // Printing cookie data print_r( $cookies); // Closing curl object instance curl_close($curlObj); ?> Note: Each website has its own format of storing cookies according to its requirements. Therefore, any specific format is not available for cookies. Output: Example 2: This example illustrates how to fetch cookies from www.google.com php <?php $url = "https://www.google.com/"; $curlObj = curl_init(); curl_setopt($curlObj, CURLOPT_URL, $url); curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curlObj, CURLOPT_HEADER, 1); curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($curlObj); preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $match_found); $cookies = array(); foreach($match_found[1] as $item) { parse_str($item, $cookie); $cookies = array_merge($cookies, $cookie); } print_r( $cookies); curl_close($curlObj); ?> Output: Note: Personal data is blurred out. Comment More infoAdvertise with us Next Article How to get cookies from curl into a variable in PHP ? chitrankmishra Follow Improve Article Tags : Web Technologies PHP PHP-cURL PHP-Misc Similar Reads How to create and read value from cookie ? The web servers host the website. The client-server makes a request for data from the webserver and the webserver fetches the required pages and responds to the client by sending the requested pages. The web server communicates with the client-server using HTTP (HyperText Transfer Protocol). HTTP is 4 min read How to create and destroy cookies in PHP ? Cookies are used to store the user information in the browser. If you store the cookie in the browser then every time you logged in to the browser then you can log in with the help of stored user information. We can understand this by saying when a user sends the request to the server then the cooki 3 min read How to get complete current page URL in PHP ? In PHP. there is a superglobal variable that can provide you with the URL of the current working page. The $_SERVER is an inbuilt variable that can access the page URL, after that, you have to check that the URL is following which type of HTTP protocol. What is Superglobal? Superglobals are already 1 min read How to get Geolocation using PHP-cURL from IP Address ? Geolocation refers to the identification of the geographical location of a user or computer device. In this article, we will create a web page where the user can enter the IP Address of any device, and then the server will provide Geolocation of the IP address fetching the following details using t 2 min read How to read write and delete cookies in jQuery? In this article, we will learn how to read, write and delete cookies in jQuery. This can be done using the cookie() and removeCookie() methods of the jquery-cookie library. We will first understand what exactly is a cookie.Cookie: Cookies are small blocks of data created by a web server when a user 3 min read Like