Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 02-10-2009, 01:16 AM CURL Help
Corey's Avatar
Super Talker

Latest Blog Post:
Pre-Pop Offers
Posts: 142
Name: Corey
Location: United States
Trades: 0
Hey guys,

OK, here's what I am trying to do: I need to log into another website, and download their reports. Seems easy right? Well, it is... because I've done it with numerous other websites.

However, this one is tricky. The only way I can download the report at this website is by having the correct "key" at the end of the URL. The key is generated by their server and carries over with each link that you click on their site once logged in. Sort of like a session ID, but it's not a session ID at all.

Anyway, this key is unique to the current cookie session for that user. If they were to log out, none of those links with that unique trailing "key" would work. They would have to log back in and use the new links. The trailing key is nothing but 10 numbers long.

So, I've devised a small curl script that does everything... it logs into the site, grabs that trailing "key" from a hidden text field, and it downloads the CSV report. However, when I look at the downloaded report, it shows a "This page does not exist" custom apache page from their server.

The problem is that it is either losing the cookie when it goes to download the report URL, or it logs out/back in when it goes to download the report URL.

Here's the code (I've removed the URL's):
PHP Code:
$graburl = **URL OF CSV REPORT**;
$cookiefile tempnam("/tmp""cookies"); 
$ch curl_init();
curl_setopt($chCURLOPT_URL$loginurl);
curl_setopt($chCURLOPT_COOKIEFILE$cookiefile);
curl_setopt($chCURLOPT_COOKIEJAR$cookiefile);
curl_setopt($chCURLOPT_POST1);
curl_setopt($chCURLOPT_POSTFIELDS"$usernamefield=$username&$passwordfield=$password");

ob_start();
$output curl_exec($ch);
ob_end_clean();

***************
ADD CODE***************
$link '**URL OF REPORT PAGE TO FETCH THE KEY**';
curl_setopt($chCURLOPT_URL$link);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$page curl_exec($ch);

$sk '%<input type="hidden" id="window_name" value="(.*)">%';
preg_match_all($sk$page$resultsPREG_PATTERN_ORDER);
***************
ADD CODE***************

if (
$output == '1') {
curl_setopt($chCURLOPT_URL"$graburl&session_key=".$results[1][0]);
$putcsv "report.csv";
curl_setopt($chCURLOPT_HEADER0);
curl_setopt($chCURLOPT_COOKIEFILE$cookiefile);
curl_setopt($chCURLOPT_COOKIEJAR$cookiefile);
curl_setopt($chCURLOPT_RETURNTRANSFER1);
$result curl_exec($ch);
file_put_contents("**MY URL**".$putcsv."""$result");
}
curl_close($ch); 
Note: This code works beautifully on other sites where I get reports from as well. It's just the extra code marked in "**ADD CODE**" and the "&session_key=".$results[1][0]" added to the graburl that is giving me problems.

It may have something to do with grabbing to URL's in the same curl session, I can't figure it out.

Any help is appreciated,

Thanks,
Corey
Corey is offline
Reply With Quote
View Public Profile Visit Corey's homepage!
 
 
Register now for full access!
Old 02-11-2009, 03:28 AM Re: CURL Help
Corey's Avatar
Super Talker

Latest Blog Post:
Pre-Pop Offers
Posts: 142
Name: Corey
Location: United States
Trades: 0
Just an update:

I've tried it with just one instance of the cURL cookie options and I've tried it with init(). EX:
PHP Code:
$ch curl_init ("http://somedomain.com/"); 
Neither worked.

If any one has any suggestions or knows a better to do this, let me know.

Thanks,
Corey

Last edited by Corey; 02-11-2009 at 04:21 AM..
Corey is offline
Reply With Quote
View Public Profile Visit Corey's homepage!
 
Old 02-11-2009, 04:33 AM Re: CURL Help
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
I had a similar problem once.
After logging in to the site, and then visiting another page, it wouldn't stay logged in.

I fixed this by including a PHPSESSID=something at the end of each link I visited.
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 02-11-2009, 05:07 AM Re: CURL Help
Corey's Avatar
Super Talker

Latest Blog Post:
Pre-Pop Offers
Posts: 142
Name: Corey
Location: United States
Trades: 0
Quote:
Originally Posted by Insensus View Post
I had a similar problem once.
After logging in to the site, and then visiting another page, it wouldn't stay logged in.

I fixed this by including a PHPSESSID=something at the end of each link I visited.
Thanks for the reply.

I tried that and it didn't work. The thing is that it's not a PHP session that is being carried. It's some sort of key that comes from the server, almost like a session, but not a PHPSESSION.

Right now I'm at the point where I can login, go to the page where the reports are and grab the key but the problem is that I can't write the result because
PHP Code:
curl_setopt($chCURLOPT_RETURNTRANSFER1); 
is at 1 instead of 0.

At 1 it puts the result into a string so that it can grab out the session key but at 0 it can't grab the key but can write to the CSV file, so I'm trying to figure out a solution to that.

If you have any ideas let me know.

Thanks,
Corey

Last edited by Corey; 02-11-2009 at 05:14 AM..
Corey is offline
Reply With Quote
View Public Profile Visit Corey's homepage!
 
Reply     « Reply to CURL Help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38405 seconds with 12 queries