PHP CURL请求后存储cookie

时间:2016-08-27 19:39:50

标签: php curl cookies

function login($username, $password) {
        $curl = curl_init("http://loginapi.com");
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, "username=".$user."&password=".$password;
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        curl_setopt($curl, CURLOPT_COOKIEJAR, "cookies.txt");
        $header = curl_exec($curl);
        curl_close($curl);

        if(preg_match('errormessagelink\?msg\=(.*?)\n/', $header)){
        return false;  // error exists
        } else {
        return true; // error doesnt exist
        }
    }       

我做了一个调用登录api并存储cookie的curl函数,我的问题是如果没有显示错误信息,我如何使它存储cookie?如果最后的if语句为真,我希望存储cookie。

0 个答案:

没有答案