dongmingxiang0312 2017-11-01 21:58
浏览 107

使用Curl存储会话cookie

I would like to use a PHP script to call a website using curl and read out data there. The problem is that there is no API for this website. So far, everything works fine, but the website requires authentication and checks if a cookie has been saved. From then on, the PHP script unfortunately does not work anymore. I have no idea how to check the cookie storage, but I have to somehow manage that Curl pretends that the cookie is being stored.

Does any of you have an idea?

$agent = "Mozilla/5.0 Gecko/13.0 Firefox/13.0";
    $header[] = "Accept: text/vnd.wap.wml,*.*";
    $ch = curl_init($url);

    if ($ch)
        {
        curl_setopt($ch,    CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,    CURLOPT_USERAGENT, $agent);
        curl_setopt($ch,    CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch,    CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch,    CURLOPT_COOKIEJAR, "");
        curl_setopt($ch,    CURLOPT_COOKIEFILE, "");
        $postdata = "?username=username&password=password";
        curl_setopt($ch,    CURLOPT_POST, 1);
        curl_setopt($ch,    CURLOPT_POSTFIELDS, $postdata);
        $tmp = curl_exec ($ch);
        curl_close ($ch);
        }
    return $tmp;
  • 写回答

1条回答 默认 最新

  • douyingbei1458 2017-11-01 22:24
    关注

    cURL will keep its session cookies until you close or re-initialize the handle, so don't do that and you should be fine. In this example, any cookies you receive from $url will be passed on to $new_url.

    $agent = "Mozilla/5.0 Gecko/13.0 Firefox/13.0";
    $header[] = "Accept: text/vnd.wap.wml,*.*";
    $postdata = ["username"=>"username", "password"=>"password"];
    $ch = curl_init($url);
    if (!$ch) {
        echo "ERROR!";
        return false;
    }
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERAGENT      => $agent,
        CURLOPT_HTTPHEADER     => $header,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_COOKIEFILE     => "",
        CURLOPT_POSTFIELDS     => $postdata,
        CURLOPT_POST           => true,
    ]);
    $tmp = curl_exec($ch);
    
    $curl_setopt_array($ch, [
        CURLOPT_URL        => $new_url,
        CURLOPT_GET        => true,
        CURLOPT_POSTFIELDS => null,
        CURLOPT_POST       => false,
    ]);
    $tmp = curl_exec($ch);
    
    curl_close($ch);
    
    return $tmp;
    

    A couple of notes: CURLOPT_POSTFIELDS is safer passed an array. This way PHP takes care of URL encoding everything for you. CURLOPT_COOKIEFILE set to an empty string will store cookies in memory, there's no need to specify CURLOPT_COOKIEJAR. Personal preference, but I find curl_setopt_array() much easier to use and read.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。