国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 php讀取CURL模擬登錄時(shí)生成Cookie文件的步驟

php讀取CURL模擬登錄時(shí)生成Cookie文件的步驟

Apr 02, 2017 pm 01:22 PM
cookie curl

這篇文章主要介紹了PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法,包括了curl的使用及cookie的操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了PHP讀取CURL模擬登錄時(shí)生成Cookie文件的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

在使用PHP中的CURL模擬登錄時(shí)會(huì)保存一個(gè)Cookie文件,例如下面的代碼

代碼如下:

$login_url = 'XXX';  
  
$post_fields['email'] = 'XXXX';  
$post_fields['password'] = 'XXXX';  
$post_fields['origURL'] = 'XXX';  
$post_fields['domain'] = 'xxx.com';  
//cookie文件存放在網(wǎng)站根目錄的temp文件夾下  
$cookie_file = tempnam('./temp','cookie');  
  
$ch = curl_init($login_url);  
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);  
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
curl_setopt($ch, CURLOPT_POST, 1);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);  
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
curl_exec($ch);  
curl_close($ch);  
  
//帶上cookie文件,訪問需要訪問的頁面  
$send_url='xxx.com';  
$ch = curl_init($send_url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  
$contents = curl_exec($ch);  
curl_close($ch);  
  
//清理cookie文件  
unlink($cookie_file);  
  
//輸出網(wǎng)頁內(nèi)容  
print_r($contents);

在temp文件夾下保存一個(gè)cookie前綴的臨時(shí)文件,例如:coo3A98.tmp文件
打開這個(gè)文件得到如下代碼:

要使用php來格式化該文件,使用以下代碼就能實(shí)現(xiàn)

代碼如下:

<?php   
$cookie_folder = dirname(__FILE__)."/temp";  
$lines = file($cookie_folder.&#39;/coo3A98.tmp&#39;);  
  
$trows = &#39;&#39;;  
  
foreach($lines as $line) {  
    if($line[0] != &#39;#&#39; && substr_count($line, "\t") == 6) {  
        $tokens = explode("\t", $line);  
        $tokens = array_map(&#39;trim&#39;, $tokens);  
        $tokens[4] = date(&#39;Y-m-d h:i:s&#39;, $tokens[4]);  
        $trows .= &#39;<tr><td>&#39; . implode(&#39;</td><td>&#39;, $tokens) . &#39;</td></tr>&#39; . PHP_EOL;  
    }  
}  
echo &#39;<table>&#39;.PHP_EOL.&#39;<tbody>&#39;.PHP_EOL.$trows.&#39;</tbody>&#39;.PHP_EOL.&#39;</table>&#39;;  
?>

運(yùn)行之后就如下圖所示,已經(jīng)被寫入到table當(dāng)中

大功告成,如果只讀取其中字段可自行修改即可。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP8.1 released: Introducing curl for concurrent processing of multiple requests PHP8.1 released: Introducing curl for concurrent processing of multiple requests Jul 08, 2023 pm 09:13 PM

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

How to handle 301 redirection of web pages in PHP Curl? How to handle 301 redirection of web pages in PHP Curl? Mar 08, 2024 am 11:36 AM

How to handle 301 redirection of web pages in PHPCurl? When using PHPCurl to send network requests, you will often encounter a 301 status code returned by the web page, indicating that the page has been permanently redirected. In order to handle this situation correctly, we need to add some specific options and processing logic to the Curl request. The following will introduce in detail how to handle 301 redirection of web pages in PHPCurl, and provide specific code examples. 301 redirect processing principle 301 redirect means that the server returns a 30

What are the dangers of cookie leakage? What are the dangers of cookie leakage? Sep 20, 2023 pm 05:53 PM

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

See all articles