Php uses CURL to retrieve cookies without relying on COOKIEJAR. the curlcookiejar_PHP tutorial

Source: Internet
Author: User
Php uses curlcookiejar, which does not rely on COOKIEJAR to obtain cookies. Php uses CURL to retrieve cookies without relying on COOKIEJAR. this example describes how php uses CURL to retrieve cookies without relying on COOKIEJAR. Share it with you for your reference. Specifically, php uses CURL to retrieve cookies without relying on COOKIEJAR. curlcookiejar

This example describes how php uses CURL to retrieve cookies without relying on COOKIEJAR. Share it with you for your reference. The specific analysis is as follows:

In PHP, the CURL class is a very awesome tool class.

The CURL class also has good support for cookies, but it is not flexible enough. Instead, it cannot be obtained using a ready-made method as a variable, but it must be implemented using the following methods.

// Save cookieto cookie.txt curl_setopt ($ ch, CURLOPT_COOKIEFILE, 'cookie.txt '); curl_setopt ($ ch, CURLOPT_COOKIEJAR, 'cookie.txt ');

First, save the COOKIE and read the file when calling it. This means that the two I/O operations are efficient, and everyone knows about the efficiency.
Is there a way to bypass writing and reading files? Do not sell off the customs, directly add the code:

// Initialize CURL $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); // Obtain the header information curl_setopt ($ ch, CURLOPT_HEADER, 1 ); // return the native (Raw) output curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // execute and obtain the returned result $ content = curl_exec ($ ch ); // disable CURL curl_close ($ ch); // Parse the HTTP data stream list ($ header, $ body) = explode ("\ r \ n ", $ content); // Parse COOKIE preg_match ("/set \-cookie :( [^ \ r \ n] *)/I", $ header, $ matches ); // you can directly use curl_setopt ($ ch, CURLOPT_COOKIE, $ cookie) when using CURL for later submission; $ cookie = $ matches [1];

I hope this article will help you with php programming.

Examples in this article describes how php uses CURL to retrieve cookies without relying on COOKIEJAR. Share it with you for your reference. Details...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.