douxin1956 2012-02-08 04:51
浏览 41
已采纳

curl在passworded网站中进行多项操作

I'm using curl to login tumblr for some operations. I'm not using tumblr api because there are some other limitations.

As referred to some other posts. I using curl_init, then multiple curl_exec and close. But it doesn't seem to work. $result1 shows that I'm sucessfully login. But $result2 is empty.

   $request_data = http_build_query(
    array(
        'email'     => $tumblr_email,
        'password'  => $tumblr_password,
    )
);


$c1 = curl_init('http://www.tumblr.com/login');
curl_setopt($c1, CURLOPT_POST, true);
curl_setopt($c1, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c1, CURL_COOKIEFILE, '');  
$result1 = curl_exec($c1);

curl_setopt($c1, CURLOPT_URL, 'http://www.tumblr.com/likes/page/2');
$result2 = curl_exec($c1);

curl_close($c1);
  • 写回答

1条回答 默认 最新

  • dongshi1188 2012-02-08 05:16
    关注
    $request_data = http_build_query(
        array(
            'email'     => $tumblr_email,
            'password'  => $tumblr_password,
        )
    );
    
    
    $c1 = curl_init('http://www.tumblr.com/login');
    curl_setopt($c1, CURLOPT_POST, true);
    curl_setopt($c1, CURLOPT_POSTFIELDS, $request_data);
    curl_setopt($c1, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c1, CURL_COOKIEFILE, 'somefilename1.txt');  
    curl_setopt($c1,CURLOPT_COOKIEJAR, 'somefilename1.txt');  
    curl_setopt($c1,CURLOPT_FRESH_CONNECT,true);//No caching
    curl_setopt($c1,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($c1,CURLOPT_MAXREDIRS,1);
    $result1 = curl_exec($c1);
    
    curl_setopt($c1, CURLOPT_URL, 'http://www.tumblr.com/likes/page/2');
    curl_setopt($c1, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($c1, CURL_COOKIEFILE, 'somefilename1.txt');  
    curl_setopt($c1,CURLOPT_COOKIEJAR, 'somefilename1.txt');  
    curl_setopt($c1,CURLOPT_FRESH_CONNECT,true);//No caching
    curl_setopt($c1,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($c1,CURLOPT_MAXREDIRS,1);
    $result2 = curl_exec($c1);
    
    curl_close($c1);
    

    Check this , it will work , actually u should specify some filename to store cookies from the site , it may be used for some authentication

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应