php使用curl带cookie访问一直失败求助
最近需要批量向织梦后台导入一些数据,但是遇到了一个头疼的问题。 环境:xampp + 别人的dede后台。 首先,利用curl发送post请求登录login.php,成功,并且保存了cookie文件。 第二步,post发送数据包到co_get_corule.php,失败,得到登录页面的源码。 于是
最近需要批量向织梦后台导入一些数据,但是遇到了一个头疼的问题。
环境:xampp + 别人的dede后台。
首先,利用curl发送post请求登录login.php,成功,并且保存了cookie文件。
第二步,post发送数据包到co_get_corule.php,失败,得到登录页面的源码。
于是,尝试简单的get请求,get获取index.php,同上,依旧是cookie没带上的效果。
第三步,检查cookie文件,确实存在,且内容也是正确的。
经过多次重复试验,依旧失败,于是发帖求助。
<span>php </span><span>header</span>("Content-type: text/html; charset=utf-8"<span>); </span>!<span>extension_loaded</span>('curl') && <span>die</span>('The curl extension is not loaded.'<span>); </span><span>$cookie_jar</span> = <span>tempnam</span>('./cookies','cookie');<span>//</span><span>存放COOKIE的文件 //登陆认证</span> <span>$url</span> = 'http://localhost/dede/login.php'<span>; </span><span>//</span><span>本来封装了函数,后来一直不成功,于是还原成了最原始的代码积木</span> <span>$post_data</span>='gotopage=index.php&dopost=login&adminstyle=newdedecms&userid=admin&pwd=admin'<span>; </span><span>$ch</span> =<span> curl_init(); curl_setopt(</span><span>$ch</span>, CURLOPT_URL, <span>$url</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_HEADER, 0<span>); curl_setopt(</span><span>$ch</span>, CURLOPT_POST, 1); <span>//</span><span>设置post方式 </span> curl_setopt ( <span>$ch</span>, CURLOPT_RETURNTRANSFER, 1 ); <span>//</span><span> 1相当于return,0相当于echo </span> <span> curl_setopt(</span><span>$ch</span>,CURLOPT_COOKIESESSION,<span>true</span><span>); curl_setopt(</span><span>$ch</span>, CURLOPT_POSTFIELDS, <span>$post_data</span>); <span>//</span><span>设置需要post的数据 </span> curl_setopt(<span>$ch</span>, CURLOPT_COOKIEJAR, <span>$cookie_jar</span>); <span>//</span><span>保存cookie信息 </span> <span> curl_setopt(</span><span>$ch</span>, CURLOPT_FOLLOWLOCATION,1); <span>//</span><span>是否抓取跳转后的页面,1是自动跳转 //curl_setopt($ch, CURLOPT_NOBODY,1); //输出的不包含body部分 </span> <span>$output1</span> = curl_exec(<span>$ch</span><span>); </span><span>//</span><span>写入结果到文件</span> <span>file_put_contents</span>('login.txt', <span>$output1</span><span>); curl_close(</span><span>$ch</span><span>); </span><span>//</span><span>这里本来封装了函数,后来一直不成功,于是还原成了最原始的代码积木</span> <span>$url2</span> = "http://localhost/dede/index.php"<span>; </span><span>$ch2</span> =<span> curl_init(); curl_setopt(</span><span>$ch2</span>, CURLOPT_URL, <span>$url2</span><span>); curl_setopt(</span><span>$ch2</span>, CURLOPT_HEADER, 0<span>); curl_setopt ( </span><span>$ch2</span>, CURLOPT_RETURNTRANSFER, 1 ); <span>//</span><span> 1相当于return,0相当于echo</span> curl_setopt(<span>$ch2</span>, CURLOPT_BINARYTRANSFER, <span>true</span><span>); curl_setopt(</span><span>$ch2</span>, CURLOPT_TIMEOUT, 30); <span>//</span><span>超时时间30s </span> curl_setopt(<span>$ch2</span>,CURLOPT_COOKIESESSION,<span>true</span><span>); curl_setopt(</span><span>$ch2</span>, CURLOPT_HTTPGET, 1); <span>//</span><span> 发送一个常规的get请求</span> curl_setopt(<span>$ch2</span>, CURLOPT_AUTOREFERER, 1); <span>//</span><span> 自动设置Referer </span> curl_setopt(<span>$ch2</span>,CURLOPT_COOKIEFILE,<span>$cookie_jar</span>);<span>//</span><span>发送cookie文件</span> curl_setopt(<span>$ch2</span>, CURLOPT_COOKIEJAR, <span>$cookie_jar</span>); <span>//</span><span>保存cookie信息</span> <span> curl_setopt(</span><span>$ch2</span>, CURLOPT_FOLLOWLOCATION,1); <span>//</span><span>是否抓取跳转后的页面,1是自动跳转 //curl_setopt($ch, CURLOPT_NOBODY,1); //输出的不包含body部分</span> <span>$output</span> = curl_exec(<span>$ch2</span><span>); </span><span>file_put_contents</span>('index.txt', <span>$output</span><span>); curl_close(</span><span>$ch2</span><span>); </span><span>unlink</span>(<span>$cookie_jar</span><span>); </span>?>
以上是拆掉函数后的原始代码,不具可读性,只求能解决这个坎儿,期待大家的帮忙,谢谢!
【PS:实在不知道如何验证cookie是否起作用了】

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
