[SOLVED] - Sending POST auto returns header Location URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

[SOLVED] - Sending POST auto returns header Location URL

Post by anjanesh »

Hi

I have a function that simulates HTTP POST.

Code: Select all

echo httpPOST("www.somewhere.com", "/somepage.php", $Data)
It works but but I keep getting the result

Code: Select all

HTTP/1.1 302 Found 
Date: Tue, 26 Jul 2005 09:34:34 GMT 
Server: Apache 
Location: http://www.somewhere.com/someredirect.html?data=somedata 
Content-Type: text/html; charset=iso-8859-1 X-Cache: MISS from www.somewhere.com 
Connection: close
Found
The document has moved here.
I thought header was taken care by Apache - how do I get this to work correctly ? Like as if a user has manually posted the data.

Thanks
Last edited by anjanesh on Tue Jul 26, 2005 7:07 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Looks like it's working fine to me. The page you're requesting is returning an HTTP 1.1 302 code.. That means the page has moved permanently. You need to follow the link in the header to where the page now resides and get it from there instead.

If you were using the CURL library it would do all that for you.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I tried using CURL too.

Code: Select all

function httpPOST_Curl($Host, $Path, $Data)
 {
        $handle = curl_init();
        curl_setopt ($handle, CURLOPT_URL, $Host."/".$Path);
        curl_setopt ($handle, CURLOPT_POST, TRUE);
        curl_setopt ($handle, CURLOPT_POST, TRUE);
        curl_setopt ($handle, CURLOPT_POSTFIELDS, $Data);
        curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
        $file_contents = curl_exec($handle);
        curl_close($handle);
        return $file_contents;
}
Gives :

Code: Select all

Found
The document has moved here.
But not redirecting !
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You could try http://www.lastcraft.com/browser_documentation.php which can be configured to follow redirections...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

anjanesh wrote:I tried using CURL too.

Code: Select all

function httpPOST_Curl($Host, $Path, $Data)
 {
        $handle = curl_init();
        curl_setopt ($handle, CURLOPT_URL, $Host."/".$Path);
        curl_setopt ($handle, CURLOPT_POST, TRUE);
        curl_setopt ($handle, CURLOPT_POST, TRUE);
        curl_setopt ($handle, CURLOPT_POSTFIELDS, $Data);
        curl_setopt ($handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($handle, CURLOPT_CONNECTTIMEOUT, 1);
        $file_contents = curl_exec($handle);
        curl_close($handle);
        return $file_contents;
}
Gives :

Code: Select all

Found
The document has moved here.
But not redirecting !
You didn't set the CURLOPT_FOLLOWLOCATION option. Which makes it follow (some) redirections.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

onion2k wrote: You didn't set the CURLOPT_FOLLOWLOCATION option. Which makes it follow (some) redirections.
Thanks !

And Thanks tim for sharing the new method.
Post Reply