已经登录的PHP cURL到已验证的站点 [英] PHP cURL into authenticated site when already logged in

查看:150
本文介绍了已经登录的PHP cURL到已验证的站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个脚本,获得传递一个URL作为获取参数,然后尝试将该页面的内容写入文件。在非验证页面上,它可以正常工作,但是当我尝试在需要验证的页面上使用它时会中断。

So I have a script that gets passed a URL as a get param, and consequently tries to write the contents of that page to a file. On non-authenticated pages, it works fine, but it breaks when I try to use it on pages that require authentication.

具体来说,这些页面是 I已经登录到,所以我假设做一个标准的cURL请求与所有我当前设置的cookie将工作,但它不会。最初,我收到302响应,但如果我将FOLLOWLOCATION设置为true,我会在网站的登录页面而不是我想要的页面。

Specifically, these are pages that I have already logged in to, so I assumed making a standard cURL request with all my currently set cookies would work, but it doesn't. Initially, I receive a 302 response, but if I set 'FOLLOWLOCATION' to true, I end up at the site's log-in page instead of the page I wanted.

$client = curl_init();

//curl_setopt($client, CURLOPT_HEADER, true);
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
curl_setopt($client, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($client, CURLOPT_URL, $pageToLoad);     

/* Supposedly libcurl loads all cookies auto-magically if you don't set CURL_COOKIESESSION
if (isset($_SERVER['HTTP_COOKIE'])) {
  curl_setopt($client, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']);
}
*/

$response = curl_exec($client);
curl_close($client);

我有沉没的感觉,我需要设置一个cookiejar文件,因为我的脚本在开发服务器上没有写入权限),但你的想法是什么?

I have the sinking feeling that I'll need to set a cookiejar file for this to work ('sinking', because my script doesn't have write permissions on the dev server), but what are your thoughts?

编辑:
所以,作为一个白痴,原来,我有一些写访问...反正,在我的主目录中的一个临时文件夹,我已经验证我的脚本可以写:

So, being sort of an idiot, turns out I do have some write access... anyway, made a temp folder in my home directory, where I've verified my script can write:

$fname = '/home/temp/cookies.txt';
if( $save = fopen($fname, 'w') ) {
  fwrite($save, 'testing');
}

运行脚本,低并注意,'testing'

我在我的cURL请求中添加了以下两行:

Run the script, and low and behold, 'testing' shows up on line one.
I've added the following 2 lines to my cURL request:

curl_setopt($ch, CURLOPT_COOKIEJAR, '/home/temp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/home/temp/cookies.txt');

在我的场景中没有什么改变。 cookies.txt文件仍然只是说测试,没有任何cookie存储在其中,也没有最后访问时间更改,这对我意味着这些行不做任何东西。

And nothing has changed in my scenario. The cookies.txt file still just says 'testing' and doesn't have any cookies stored in it, nor does the 'last access' time change, which to me means these lines aren't doing anything.

还有什么想法吗?我有点怨恨:\

Any further thoughts? I'm kinda stumped :\

推荐答案

所以我拿出了原来的cURL代码,我已经复制了一些Zend的具体代码从一个不同的项目,这似乎工作的大部分(不是真的确切为什么...)。我遇到的唯一的其他问题处理CoSign(http://cosign.sourceforge.net/)和使用代理cookie。感谢那些提供答案的人!

So I took out the raw cURL code I had and copied in some Zend specific code from a different project, and that seemed to work for the most part (not really sure why...). The only other problems I've run into deal with CoSign (http://cosign.sourceforge.net/) and using proxy cookies. Thanks to those who offered answers!

这篇关于已经登录的PHP cURL到已验证的站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆