curl-and-php
cURL, form filling and redirection
Date: Sat, 4 Apr 2009 16:28:47 +0200
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
Received on 2009-04-04