├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── demo ├── 0_simpleAutoLogin.php ├── 10_zhihulogin.php ├── 11_cnblogLogin │ ├── 11_cnbloglogin.php │ ├── jsencrypt.min.js │ └── nodeServer.cnblogLogin.js ├── 12_zuocheWithCaptcha.php ├── 14_360doc.php ├── 15_downloadWizNote.php ├── 16_commonCurl │ ├── code.png │ ├── curlPage.cookie.txt │ └── curlPage.php ├── 17_dev.360.cn_STDINCaptcha.php ├── 19_www.tianyancha.com.php ├── 1_simpleAutoLogin.php ├── 20_u.mumayi.com_OnlineCaptcha.php ├── 22_tools.cnforex.com-phantomjs-parse-js.php ├── 26_publish.haodanku.com.php ├── 2_wxqq.php ├── 34_www.aliwx.com.cn │ ├── 34_www.aliwx.com.cn.js │ ├── 34_www.aliwx.com.cn.php │ └── books │ │ └── .gitignore ├── 38_upload.otar.im_image_upload.php ├── 3_preloadCookieAndVisit.php ├── 41_cli.im.php ├── 48_request_repeat.php ├── 49_format_cookie.php ├── 61_tophub.today.php ├── 62_curlmulti │ ├── .gitignore │ ├── 62_wiz.cn.php │ ├── cache │ │ └── .gitignore │ └── composer.json ├── 7_cnki.net.php ├── 8_webqq.php ├── parseCurlCode │ └── .gitignore └── parseCurlGetPhpCode.php ├── images ├── get-curl-text.png └── php-curl-qrcode.png ├── logs └── .htaccess ├── phpunit.xml └── src └── CurlAutoLogin.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | logs/*.log 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # php-curl 2 | PHP利用cURL实现模拟登录与采集,提供大量示例。(关键词:php curl login) 3 | 4 | 了解PHP cURL使用请参考:《[PHP cURL实现模拟登录与采集使用方法详解](http://www.zjmainstay.cn/php-curl)》 5 | 6 | 基于浏览器cURL命令快速生成PHP代码:[基于cURL命令解析获得PHP代码](http://demo.zjmainstay.cn/php/github/php-curl/demo/parseCurlGetPhpCode.php) 7 | 8 | 自动模拟登录+批量请求示例:[模拟登录后批量下载笔记](https://github.com/Zjmainstay/php-curl/blob/master/demo/62_curlmulti/62_wiz.cn.php) 9 | 10 | ## 依赖 11 | 12 | PHP: >=5.4 13 | 14 | ## 单独使用 15 | 16 | ```php 17 | git clone https://github.com/Zjmainstay/php-curl.git 18 | cd php-curl 19 | composer install 20 | #测试 21 | php demo/1_simpleAutoLogin.php 22 | ``` 23 | 24 | ## composer引入 25 | 26 | ```php 27 | composer require zjmainstay/php-curl 28 | 29 | #测试 30 | cd vendor/zjmainstay/php-curl 31 | composer install 32 | php demo/1_simpleAutoLogin.php 33 | ``` 34 | 35 | ## 直接引入类 36 | 37 | ```php 38 | require_once __DIR__.'/../src/CurlAutoLogin.php'; # 使用时注意路径 39 | $autologin = new PHPCurl\CurlAutoLogin(); 40 | 41 | # 参考demo/0_simpleAutoLogin.php 42 | ``` 43 | 44 | 45 | 46 | ## CurlAutoLogin 介绍 47 | 48 | ### 属性 49 | 50 | ```php 51 | //最后一次cookie存储文件 52 | protected $lastCookieFile = ''; 53 | //登录成功后,锁定cookie的更新 54 | protected $lockedLastCookieFile = false; 55 | //日志路径 56 | protected $logPath = ''; 57 | //全局opt,方便使用代理之类的请求 58 | protected $globalOpts = []; 59 | //最后一次请求参数,用于重放请求 60 | protected $lastExecParams = []; 61 | ``` 62 | 63 | ### 方法 64 | 65 | ```php 66 | // ================== 1.常用方法 ================== 67 | //根据curl信息执行并解析结果,核心方法常用方法之一 68 | public function execCurl($curlContent, $callbackBefore = false, $callbackAfter = false, $storeParams = true) 69 | 70 | //携带cookie执行curl命令,核心方法常用方法之一,直接利用curl命令里的header头cookie参数 71 | public function execCurlWithCookie($curlContent, $callbackBefore = false, $callbackAfter = false) 72 | 73 | // ================== 2.普通公用方法 ================== 74 | //设置全局请求opt(方便使用代理之类的请求) 75 | public function setGlobalOpts($opts = []) 76 | 77 | //重放请求,依赖登录的场景出现登录失败的情况,重新登录后,调用此方法重试上一次的请求 78 | public function repeatRequest() 79 | 80 | //获取最后一次请求参数,用于重放请求 81 | public function getLastExecParams() 82 | 83 | //主动销毁最后一次请求参数 84 | public function unsetLastExecParams() 85 | 86 | //解析curl信息,返回结果包含url/header/post/opt/cookie,post是post提交的数据,opt是curl备用扩展 87 | public function parseCurl($curlContent) 88 | 89 | //设置日志路径 90 | public function setLogPath($logPath) 91 | 92 | //获取上一次存储cookie的文件 93 | public function getLastCookieFile() 94 | 95 | //获取最后一次存储的cookie内容 96 | public function getLastCookieContent() 97 | 98 | //手动追加cookie内容到最后一次存储的cookie文件 99 | public function appendCookieContent($content) 100 | 101 | //设置上一次存储cookie的文件 102 | public function setLastCookieFile($cookieFile) 103 | 104 | //清空上次存储的cookie 105 | public function removeLastCookie() 106 | 107 | //登录成功后,锁定上一次存储cookie的文件,避免覆盖 108 | public function lockLastCookieFile() 109 | 110 | //解锁上一次存储cookie的文件 111 | public function unlockLastCookieFile() 112 | 113 | //登录成功后,锁定cookie,可以基于get方式获取url信息 114 | public function getUrl($url, $header = false) 115 | 116 | //登录成功后,锁定cookie,可以基于post方式获取url信息 117 | public function postUrl($url, $postData = false, $header = false) 118 | 119 | //断言内容中包含某个字符(判断登录信息,如“退出”字眼) 120 | public function assertContainStr($content, $substr) 121 | 122 | //获取换行符,用于输出信息显示换行 123 | public function getLineBreak() 124 | 125 | //格式化header头cookie成文件存储格式 126 | public function formatHeaderCookieToFileContent($headerCookie, $domain) 127 | 128 | //从curl内容解析cookie内容并追加保存到cookie文件 129 | public function addCookieFromCurl($curlContent, $domain) 130 | 131 | // ================== 3.底层方法 ================== 132 | //执行curl请求,底层核心方法,内置了请求的cookie存储与跟踪上一次请求的cookie,实现模拟登录cookie依赖 133 | protected function _execCurl($parseCurlResult) 134 | 135 | //记录日志,底层方法,出现异常时记录日志 136 | protected function _log($msg) 137 | ``` 138 | 139 | 140 | 141 | ## demo介绍 142 | 143 | ```php 144 | //简易模拟登录示例,直接引用CurlAutoLogin类进行调用 145 | 0_simpleAutoLogin.php 146 | 147 | //简易模拟登录示例,基于composer引用CurlAutoLogin类进行调用 148 | //简介:包含execCurl回调$callbackBefore实现curl请求前替换用户名 149 | 1_simpleAutoLogin.php 150 | 151 | //浏览器登录微信,利用登录后cookie进行后续访问 152 | //简介:【代码已不可用,参考思路】 153 | //方法一:利用execCurl前置回调添加cookie到header中 154 | //方法二:直接调用parseCurl对curl命令进行解析,并组装包含cookie的header头进行请求 155 | //方法三:直接调用execCurlWithCookie调用 156 | 2_wxqq.php 157 | 158 | //预加载cookie,然后访问其他页面 159 | //简介:模拟登录有时候依赖首页或者其他页面的cookie,所以需要调用execCurl先请求一次,存下该页面的cookie,再做后续的请求 160 | 3_preloadCookieAndVisit.php 161 | 162 | //中国知网下载 163 | //简介:增加CURLOPT_HEADER属性返回请求头信息,然后解析下载地址进行下载请求 164 | 7_cnki.net.php 165 | 166 | //webqq发送消息 167 | //简介:【功能已不可用】基于web2 qq 发送群消息和个人消息(注:频繁发送会导致账号锁定,慎用!!!) 168 | 8_webqq.php 169 | 170 | //博客园模拟登录 171 | //简介:【网站改版,功能已不可用】 172 | //基于nodejs提供js加密处理,返回结果给php请求登录使用 173 | 11_cnblogLogin 174 | 175 | //(验证码)坐车网登录 176 | //简介:坐车网网页显示验证码,用户手动录入后提交给程序,实现模拟登录 177 | 12_zuocheWithCaptcha.php 178 | 179 | //模拟登录360doc 180 | //简介:360doc登录,中间有一层cookie依赖,类似demo 3_preloadCookieAndVisit.php 181 | 14_360doc.php 182 | 183 | //为知笔记内容下载 184 | //简介:利用js在控制台运行获取所有笔记采集链接,然后下载对应笔记 185 | 15_downloadWizNote.php 186 | 187 | //页面采集通用方法 188 | //简介:日常使用curl请求可用 189 | 16_commonCurl/curlPage.php 190 | 191 | //(验证码)命令行下输入验证码登录dev.360.cn 192 | //简介:命令行执行获取验证码,写入到文件,命令行等待录入验证码,录入后提交给程序,实现模拟登录 193 | 17_dev.360.cn_STDINCaptcha.php 194 | 195 | //天眼查关键词搜索 196 | //简介:【功能已不可用】天眼查关键词搜索,最终数据页有token依赖,提取后通过$callbackBefore传入 197 | 19_www.tianyancha.com.php 198 | 199 | //(验证码)木蚂蚁用户登录 200 | //简介:木蚂蚁用户登录,网页显示验证码,用户手动录入后提交给程序,实现模拟登录 201 | 20_u.mumayi.com_OnlineCaptcha.php 202 | 203 | //环球外汇网即时行情数据采集 204 | //简介:【页面改版已不需要这么复杂】 205 | //加载页面,解析出js,调用phantomjs执行js获取cookie加密结果,交给php模拟程序调用 206 | 22_tools.cnforex.com-phantomjs-parse-js.php 207 | 208 | //(验证码)好单库登录 209 | //简介:命令行执行获取验证码,写入到文件,命令行等待录入验证码,录入后提交给程序,实现模拟登录 210 | 26_publish.haodanku.com.php 211 | 212 | //阿里文学js解密 213 | //简介:阿里文学网小说解析,基于phantomjs执行js进行内容解析 214 | 34_www.aliwx.com.cn 215 | 216 | //图床图片上传 217 | //简介:【网站502功能已不可用】访问初始页获取x-csrf-token,解析后通过$callbackBefore传入,实现模拟请求上传图片到图床网站 218 | 38_upload.otar.im_image_upload.php 219 | 220 | //草料图片上传二维码解析 221 | //简介:上传图片到草料,然后利用上传结果调用解析接口,获得解析结果 222 | 41_cli.im.php 223 | 224 | //重放请求 225 | //简介:首先模拟无登录态请求,异常后模拟登录,然后重放请求得到正常结果 226 | 48_request_repeat.php 227 | 228 | //tophub.today抓取demo 229 | //简介:知乎热榜网站抓取 230 | 61_tophub.today.php 231 | 232 | //【精华推荐】为知笔记模拟登录后,批量下载笔记 233 | //简介:本示例实现模拟登录与ares333/php-curl批量执行类库结合,演示如何模拟登录后,基于登录态进行批量下载笔记 234 | 62_curlmulti/62_wiz.cn.php 235 | 236 | //基于curl解析得到PHP代码 237 | parseCurlGetPhpCode.php 238 | 239 | ``` 240 | 241 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "zjmainstay/php-curl", 3 | "description": "PHP利用cURL实现模拟登录与采集", 4 | "type": "library", 5 | "keywords": [ 6 | "php", 7 | "php curl", 8 | "php curl login", 9 | "php 模拟登录", 10 | "php 模拟登录采集" 11 | ], 12 | "homepage": "http://www.zjmainstay.cn", 13 | "license": "Apache-2.0", 14 | "authors": [ 15 | { 16 | "name": "Zjmainstay", 17 | "email": "zjmainstay@163.com", 18 | "role": "lead" 19 | } 20 | ], 21 | "autoload": { 22 | "psr-4" : { 23 | "PHPCurl\\" : "src/" 24 | } 25 | }, 26 | "require": { 27 | "php": ">=5.4.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /demo/0_simpleAutoLogin.php: -------------------------------------------------------------------------------- 1 | getLineBreak(); 17 | 18 | //0. 未登录 19 | $getDataUrl = 'http://demo.zjmainstay.cn/js/simpleAjax/loginResult.php'; 20 | echo 'Before Login: ' . $autologin->getUrl($getDataUrl) . $lineBreak; 21 | 22 | //1. 初始化登录页 23 | $curl1 = "curl 'http://demo.zjmainstay.cn/js/simpleAjax/' -H 'Host: demo.zjmainstay.cn' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'Cookie: Hm_lvt_1526d5aecf5561ef9401f7c7b7842a97=1468327822,1468327904,1468341636,1468411918; Hm_lpvt_1526d5aecf5561ef9401f7c7b7842a97=1468421526' -H 'Connection: keep-alive' -H 'If-Modified-Since: Mon, 27 Oct 2014 08:31:18 GMT' -H 'If-None-Match: \"32e-453-506635ac5e180\"' -H 'Cache-Control: max-age=0'"; 24 | $content = $autologin->execCurl($curl1); 25 | 26 | //2. 提交登录表单 27 | $curl2 = "curl 'http://demo.zjmainstay.cn/js/simpleAjax/doPost.php' -H 'Host: demo.zjmainstay.cn' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'Accept: application/json, text/javascript, */*; q=0.01' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Accept-Encoding: gzip, deflate' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'X-Requested-With: XMLHttpRequest' -H 'Referer: http://demo.zjmainstay.cn/js/simpleAjax/' -H 'Cookie: Hm_lvt_1526d5aecf5561ef9401f7c7b7842a97=1468327822,1468327904,1468341636,1468411918; Hm_lpvt_1526d5aecf5561ef9401f7c7b7842a97=1468421526' -H 'Connection: keep-alive' --data 'username=demousername'"; 28 | $realUsername = 'Zjmainstay'; 29 | //前置处理,替换错误的用户名(也可以在上面填充curl的时候,使用变量替换对应内容) 30 | $content = $autologin->execCurl($curl2, function($parseCurlResult) use ($realUsername) { 31 | $parseCurlResult['post'] = str_replace('=demousername', "={$realUsername}", $parseCurlResult['post']); 32 | return $parseCurlResult; 33 | }); 34 | 35 | //3. 登录成功,锁定cookie的更新,直接访问已登录页面内容(类似采集内容),演示cookie锁定多次采集效果与cookie失效效果 36 | $autologin->lockLastCookieFile(); 37 | echo 'After Login 1(with_cookie): ' . $autologin->getUrl($getDataUrl) . $lineBreak; 38 | echo 'After Login 2(with_cookie): ' . $autologin->getUrl($getDataUrl) . $lineBreak; 39 | $autologin->removeLastCookie(); 40 | echo 'After Login 3(without_cookie): ' . $autologin->getUrl($getDataUrl) . $lineBreak; 41 | -------------------------------------------------------------------------------- /demo/10_zhihulogin.php: -------------------------------------------------------------------------------- 1 | execCurl($curl1); 29 | 30 | //偶然发现csrf没有用,所以直接赋值了useless 31 | $email = urlencode($email); 32 | $curl2 = "curl 'https://www.zhihu.com/login/email' -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3' -H 'Connection: keep-alive' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -H 'Cookie: q_c1=0daf021c52de4ef3b6a529ff4f1a6811|1470651590000|1470651590000; _xsrf=4e599680f57fee89a4dd1aebe0e38128; l_cap_id=\"NzFlNjRmZGE3ODJhNDJkMGI1NDc2YzQxYjliZDkwZTE=|1470651590|9d2ff3f78f14a0e32c7938f1d86023736b25a5bd\"; cap_id=\"OWFjNDkzYWYxNmZhNDBjN2JlZWJjNDhiNGUxYTJjNWI=|1470651590|652577038670a9b544e91d6cbcb5a355dfc29eee\"; n_c=1; d_c0=\"ABCAsUV-WgqPTnNCtsTsgzk-RdfrxZVFb8g=|1470651591\"; __utma=51854390.248018333.1470651592.1470651592.1470651592.1; __utmb=51854390.2.10.1470651592; __utmc=51854390; __utmz=51854390.1470651592.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=51854390.000--|3=entry_date=20160808=1; __utmt=1; _za=ee1a9bde-d629-4f05-b670-7adb1ee17a5f; _zap=64a770d4-5a33-4744-9602-4608f0f6f551' -H 'Host: www.zhihu.com' -H 'Referer: https://www.zhihu.com/' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:47.0) Gecko/20100101 Firefox/47.0' -H 'X-Requested-With: XMLHttpRequest' -H 'X-Xsrftoken: 4e599680f57fee89a4dd1aebe0e38128' --data '_xsrf=useless&password={$password}&captcha_type=cn&remember_me=true&email={$email}'"; 33 | $content = $autologin->execCurl($curl2); 34 | 35 | $autologin->lockLastCookieFile(); 36 | 37 | $homeUrl = 'https://www.zhihu.com/'; 38 | $isLogin = $autologin->assertContainStr($autologin->getUrl($homeUrl), '退出'); 39 | 40 | if($isLogin) { 41 | echo "已登录"; 42 | } else { 43 | exit('未登录,请检查账号密码'); 44 | } 45 | 46 | file_put_contents($loginCookieFile, $autologin->getLastCookieContent()); 47 | } 48 | 49 | $autologin->setLastCookieFile($loginCookieFile); 50 | 51 | //获取我的收藏(未做登录缓存过期判断处理) 52 | $url = 'https://www.zhihu.com/collections'; 53 | $content = $autologin->getUrl($url); 54 | 55 | $pattern = '#
151 | 如何从浏览器获取cURL命令
152 |
153 | 利用浏览器的开发者工具(控制台),Firefox如下图:
154 |
155 |
156 |
158 | 更多PHP cURL内容,请查看博客《PHP cURL实现模拟登录与采集使用方法详解》 159 |
160 | 161 | 162 | -------------------------------------------------------------------------------- /images/get-curl-text.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zjmainstay/php-curl/7a55c6f4f3bcc2d52b82227d9bea9cc450227de5/images/get-curl-text.png -------------------------------------------------------------------------------- /images/php-curl-qrcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zjmainstay/php-curl/7a55c6f4f3bcc2d52b82227d9bea9cc450227de5/images/php-curl-qrcode.png -------------------------------------------------------------------------------- /logs/.htaccess: -------------------------------------------------------------------------------- 1 | deny from all 2 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 |