开发者

Using cookies with CURL

开发者 https://www.devze.com 2023-01-12 15:14 出处:网络
I\'m writing an \"API\" for a website which doesn\'t have it. Basically, my PHP code logs into the website and grabs the data I need (two different tran开发者_如何学JAVAsfers).

I'm writing an "API" for a website which doesn't have it.

Basically, my PHP code logs into the website and grabs the data I need (two different tran开发者_如何学JAVAsfers).

At login time, I'm getting a bit of a problem. The website sets a couple of cookies through HTTP, which I'm capturing using CURL's cookie mechanism. This seems to work out nicely, except that they are also trying to set a cookie via javascript in that same response.

I don't need to parse the javascript since the cookie they set is entirely predictable. What I need is to somehow tell CURL that this cookie exists, WHILE it stills maintains the other cookies.

Help? :)


After submitting the login details via curl POST, I get to these headers:

HTTP/1.1 200 OKDate: Fri, 20 Aug 2010 09:39:14 GMT
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 492
Set-Cookie: JSESSIONID=5DE1F32B3668DABB408BBEA10C28DBD5.testmmf1; Path=/merchantlogin
Set-Cookie: loginType=M
Connection: close

And this is the page content:

<script type="text/javascript">
    var nextyear = new Date();
    nextyear.setFullYear(nextyear.getFullYear() + 1);
    document.cookie = 'login=' + document.referrer + '; expires=' + nextyear.toGMTString();
</script>

Notice the Set-Cookie and document.cookie parts.


Generate cookie file via code, and before making request to location witch requires that cookie add it simply through setopt with option CURLOPT_COOKIEFILE


You could set the cookie using curl_setopt and the CURLOPT_COOKIE option first. Of course doing this will erase your other cookies, but they'll be gotten back, right?

If you could get a hold of the current value of CURLOPT_COOKIE, you could append your cookie with a semicolon. But PHP doesn't seem to have a curl_getopt function.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号