Alright, I'm trying to get a script to visit a website and copy the contents. The problem is that I must fill out a form before I can get to the page I want, but that's not the issue really. The url basically does this.
www.mysite.com/page.php // form is submitted
www.mysite.com/page.php&id=83920 // something here
www.mysite.com/page.php // the page I need

For some reason I can't get from the second url to the third. I figured it might be a redirect link, but I'm not sure now because even with followlocation set to true, I still end up getting the contents of the second url. Here's the code I have in case that might be the issue.

<?php
// INIT CURL
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'www.mysite.com/page.php&id=83920');

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_HEADER, true);
curl_setopt ($ch, CURLOPT_POST, 1);

// Something here possibly, not sure

$output = curl_exec($ch);

$handle = fopen("name.txt", "w");
fwrite($handle,$output);
fclose($handle);

curl_close ($ch);
?>

So, am I using it wrong? What does followlocation do actually (since I can't seem to find that anywhere)? The site mentions that Javascript must be enabled to view the page, would that affect how my script behaves?

Interesting problem, maybe they are setting some variable into hidden inputs using JS and looking for that on page submit.

On the page that you actually receive, does it include any errors or notices printed from the other site?

Not from what I can see. With the code I get, it is loading the page but doesn't actually direct me to the next page, or if it does, for some reason it copies the contents of the one before it. One person I was talking to suggested using two executes so one would submit the form and the second would get the contents.

Is that necessary for a situation like this? If not, when would you actually need to do that?

Member Avatar for diafol

Does the site have a downloadable API? OAuth? It could have a lot of hassle. You may like to try PIPES from Yahoo! This allows you to remote access sites - loads of examples.

Does the site have a downloadable API? OAuth? It could have a lot of hassle. You may like to try PIPES from Yahoo! This allows you to remote access sites - loads of examples.

That might be a better option because I tried my code on another site similar to it, just to see if perhaps it was a missed redirect, and it worked fine there. I'll give it a try and see how it works. Thanks.

There ended up being a hidden variable that, once entered, allowed me to go straight to the page with the url only. I overlooked it cause of how the page looked. Thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.