最近需要批量向织梦后台导入一些数据,但是遇到了一个头疼的问题。 环境: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是否起作用了】