【问题标题】:Curl: can't save cookies in cookiejarCurl:无法在 cookiejar 中保存 cookie
【发布时间】:2013-10-10 10:03:02
【问题描述】:

我正在尝试登录 kiala 网站。 Kiala 是一家“航运”公司,我喜欢用 php 获得一个新的航运令牌。由于没有用于执行此操作的 api,因此我尝试使用 curl。现在我对 curl 没有太多经验,我不能让 curl 将饼干保存到罐子里。我已经尝试了很多事情,但现在我到了想要扯掉头发的地步。我需要这些 cookie 来提出进一步的请求。

我创建了一个用于测试的虚拟帐户

网站: http://www.kialaverzendservice.be/

登录表单: http://www.kialaverzendservice.be/login.required.action?os_destination=%2Fsender%2Fstart.action

电子邮件: kialatest@mailinator.com

密码: test123

我得到以下标题

HTTP/1.1 200 OK Date: Thu, 10 Oct 2013 09:46:37 GMT Server:
Apache-Coyote/1.1 Content-Type: text/html;charset=ISO-8859-15 Vary:
Accept-Encoding Set-Cookie:
berkano-seraph-login=eGV5ZcKEZXhlwoZlwoJkfGJ5Y31jemTCgGJ7YsKGYsKCYnhifmLCgmLChmN6YsKGY3dmwoNiwoZjwoNjeGh+YnhjfWJ5Yn1mwoFmwoFm;
Expires=Fri, 10-Oct-2014 09:46:37 GMT; Path=/ Set-Cookie:
kiala-c2c-language=nl; Expires=Tue, 28-Oct-2081 13:00:44 GMT; Path=/
Transfer-Encoding: chunked

我的简化 php 代码:我可以登录,但标题中的 cookie 未在我的 cookiejar 文件中设置?顺便说一句,我在 localhost (wamp) 上,但我认为这并不重要。

loginToKiala();


function loginToKiala(){


            $url = 'http://kialaverzendservice.be/sender/start.action';

            //POST vars
            $fields = array(
                                    'os_username' => urlencode('kialatest@mailinator.com'),
                                    'os_password' => urlencode('test123'),
                                    'os_cookie'=>urlencode('true')//remember me
                            );

            //url-ify the data for the POST
            $fields_string='';
            foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
            rtrim($fields_string, '&');

            $ch=curl_init();

            $cookie_file = './cookies.txt';

            if (! file_exists($cookie_file) || ! is_writable($cookie_file))
            {
                echo 'Cookie file missing or not writable.';
                exit;
            }
            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL, $url);
            curl_setopt($ch,CURLOPT_POST, count($fields));
            curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);


            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
            //curl_setopt($ch,CURLOPT_AUTOREFERER, true);
            //curl_setopt($ch, CURLOPT_REFERER, 'http://www.kialaverzendservice.be/');//set referer for first request
            //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
            curl_setopt($ch, CURLOPT_HEADER, 1);
            //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);




            curl_exec ($ch); // execute the curl command

            curl_close ($ch);
            unset($ch);

        }

感谢任何帮助!

【问题讨论】:

    标签: php cookies curl


    【解决方案1】:

    首先你没有正确使用CURLOPT_POST。 cURL 期待 TRUEFALSE10。您在那里拥有的函数count($fields) 将(可能)返回一个大于一的数字。

    改为这样做:

    curl_setopt( $ch, CURLOPT_POST, 1 );
    

    至少,如果我理解正确的话,您想使用 POST 而不是 GET。

    【讨论】:

      猜你喜欢
      • 2016-11-24
      • 2023-03-13
      • 2014-03-24
      • 2011-08-23
      • 1970-01-01
      • 2018-12-26
      • 2015-04-18
      • 2017-10-29
      • 1970-01-01
      相关资源
      最近更新 更多