cURL / Mailing Lists / curl-and-php / Single Mail

curl-and-php

RE: cURL, form filling and redirection

From: Luc B <lucboja_at_hotmail.com>
Date: Sun, 5 Apr 2009 14:56:23 +0200

Fantastic! Many many thanks, Stephen!

Regards,
Luc

Date: Sun, 5 Apr 2009 01:11:23 -0400
Subject: Re: cURL, form filling and redirection
From: spynenburg_at_gmail.com
To: curl-and-php_at_cool.haxx.se

Reviewing the given page, it seems that it leads to a JavaScript redirect - looks like this:
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
<!--
window.location="/bordeaux/?q=latour&id=292&do=dom&-session=1855:0A00005609e3d11375sXN260B23D";

// -->
</script>
 
So we can do a simple regex search on the result like so:
<?PHP
   $post = array(
       'q' => "latour",
       'do' => "qsearch"

   );
   $ch = curl_init();
   curl_setopt($ch,CURLOPT_URL,'http://www.1855.com/');
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $ret = curl_exec($ch);
   curl_close($ch);

   preg_match("/window\.location=\"(.*)\";/", $ret, $matches);

   echo "http://www.1855.com".$matches[1];

?>
Leaving you with a usable HTTP link.

-Stephen
On Sat, Apr 4, 2009 at 10:28 AM, Luc Bojanowski <lucboja_at_hotmail.com> wrote:

Hi All,

I am having troubles with cURL, forms filling and redirections. Any help would be very much appreciated! Here are examples of what I am trying to do, and how it works (NOT!)... For illustration purposes, I will refer to the www.1855.com website, which indeed offers forms and redirections, e.g. if I type http://www.1855.com/?q=latour&do=qsearch in my address bar and press enter, I am redirected to http://www.1855.com/bordeaux/?q=latour&id=292&do=dom&-session=1855:0A00005609dd91BFD7Guw1AFEC2E.

1/ Now if I want to do the same thing but this time using cURL

Here's my code:

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,'http://www.1855.com/?q=latour&do=qsearch');

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    $ret = curl_exec($ch);

    curl_close($ch);

And the conclusion is: it does not work! On my screen, what appears is not the http://www.1855.com/bordeaux/?q=latour&id=292&do=dom&-session=1855:0A00005609dd91BFD7Guw1AFEC2E page, but the www.1855.com page on which the 'q' field of the research form has been set to "latour".

Note: nothing changes when I remove "curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);" from the above code.

2/ A non satisfactory way of being redirected (to the wrong page...)

I have apparently found a non-satisfactory way of being redirected. Please consider the following example.

My code (just a few things added to the previous example):

    $post = array(

        'q' => "latour",

        'do' => "qsearch"

    );

    $ch = curl_init();

    curl_setopt($ch,CURLOPT_URL,'http://www.1855.com/?q=latour&do=qsearch');

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);

    $ret = curl_exec($ch);

    curl_close($ch);

Note: I'm conscious of "using" both GET and POST method in this example... This is one of the reasons for this being "non-satisfactory"...

Result: on my screen appears exactly what would appear if I had manually put "latour latour" in the research form available on www.1855.com, pressed enter and waited for the redirection... meaning that this time, cURL has indeed followed the redirection, but has not properly filled the form (putting "latour latour" instead of "latour" in the form)...

3/ Getting closer to something more acceptable?

In the previous example (presented in 2/), if I replace 'http://www.1855.com/?q=latour&do=qsearch' by 'http://www.1855.com', then the system apparently fills the form correctly and tries to make a redirection but uses a relative address, as I get a 404 error with the following message "The requested URL /bordeaux/ was not found on this server", while in the address bar I can read "http://localhost/bordeaux/?q=latour&id=292&do=dom&-session=1855:0A00005609dd91FDF3JvQ1BBFA9F"

Better than nothing would be to intercept this "http://localhost/bordeaux/?q=latour&id=292&do=dom&-session=1855:0A00005609dd91FDF3JvQ1BBFA9F", as I could build a valid URL from it... but I have to admit I have no idea how I could do that...

Any help would be very much appreciated.

Have a great week-end!

Luc

_________________________________________________________________

Inédit ! Des Emoticônes Déjantées! Installez les dans votre Messenger !

http://www.ilovemessenger.fr/Emoticones/EmoticonesDejantees.aspx

_______________________________________________

http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php

_________________________________________________________________
Découvrez toutes les possibilités de communication avec vos proches
http://www.microsoft.com/windows/windowslive/default.aspx

_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2009-04-05