普通网友 2012-09-21 17:21
浏览 21
已采纳

使用cURL阅读,完成一些步骤,然后发布[关闭]

Solved: Yay. In addition to what you wrote Drew, I found this: http://www.smooka.com/blog/2009/07/24/maintaining-php-session-when-using-curl/ session_write_close(); before initializing the next set. Now I just need to figure out AJAX/jquery, because it looks like I can't register resource variables (e.g. Curl's handle) in a session.


I want to login to a site using cURL. Their login process uses a token which I am able to parse using file_get_contents(). However, when I try to access the site again using cURL, the token is no longer good. Is there a way read the file using cURL, pause to do the calculations/steps, and then post the fields? Is there a better way?

  • 写回答

1条回答 默认 最新

  • dpp80346 2012-09-21 18:01
    关注

    The token is probably no longer good because it is associated with a session cookie that gets set when you access the site. Since file_get_contents has no cookie tracking, you should use cURL to request the initial URL, grab the token, do your calculations, and then post back using cURL again.

    To get cURL to keep track of the cookies, use the CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR options for the cURL handle.

    $cookies = '/tmp/cookies.txt';  // path to cookie file
    $ch = curl_init('http://site.com');
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
    //...
    $res = curl_exec($ch);
    
    // grab token here...
    
    // do calculations here...
    
    curl_setopt($ch, CURLOPT_URL, 'http://site.com/next-url');
    
    $res = curl_exec($ch); // execute next request on same handle
    

    Here are some other answers of mine that use cURL to login to various websites and perform actions; looking at the code may be of help to you.

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

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀