using header to POST

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

using header to POST

Post by kendall »

hey guys,

I need some help with identifying a problem... I am using Ajax to post to a proxy on my server which uses header() to POST to a "mock" webservice on another server. the web services uses a header("Location") to redirect information BACK to my proxy. However I do not seem to be getting any response. doesn't seem like my header POST is reaching to the destination but thats still to be confirm...

when using header() to POST what is the usual result/ response? when developing a process using header() to POST what should I be aware of?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: using header to POST

Post by John Cartwright »

You cannot post with header("Location: "). Redirects are limited to GET.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: using header to POST

Post by kendall »

John Cartwright wrote:You cannot post with header("Location: "). Redirects are limited to GET.
I know this... i may not be clear to what is going on so here is the pseudo scenario
AJAX POST -> myproxy.php --- header($_POST) ---> web-service server -- process $_POST then header("Location: myprox.php?infor=data") ---> my server myproxy.php echos $_GET --> AJAX callBack

I'm not getting any responseText on the callBack

checks with the service indicate that it didnt process the $_POST so I realise maybe i am not passing the variables...hence i probably not setting the correct header information...So i'm trying to just do a simple request and response test and i am not getting anything
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: using header to POST

Post by AbraCadaver »

kendall wrote: I know this... i may not be clear to what is going on so here is the pseudo scenario
What is this then? header($_POST)? You can't use header() to POST.

The way to POST data with PHP in general is to build a proper post request, fsockopen() to the server and fputs() your data, then fgets() the response. Or, take your pick of classes or functions here: http://us3.php.net/manual/en/book.http.php which may be easier.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: using header to POST

Post by John Cartwright »

Your 2nd explanation didn't clarify anything for me.
What is this then? header($_POST)?
My thoughts exactly.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: using header to POST

Post by kendall »

sorry If i was not clear i was just using pseudo talk

but the code being used in myproxy.php

Code: Select all

 
//....in an if condition
header("POST $path HTTP/1.1\r\n" );
    header("Host: $host\r\n" );
    header("Content-type: application/x-www-form-urlencoded\r\n" );
    header("Content-length: " . strlen($data) . "\r\n" );
    header("Keep-Alive: 300");
    header("Connection  keep-alive");
    header($data);
 
where host and path is the webservice

the code on the webservice is

header("Location: http://" . $referer['host'] . $redirect_path . '?' . $query);

where referer['host'] and redirecth_path is the location of myrpoxy.php

thus myproxy.php has a

Code: Select all

 
//...in an if condition
header("Content-type: text/html" );
    header("Connection: close\r\n\r\n" );
    echo http_build_query($_GET);
note that all this is happening via AJAX

I'm wondering if If the ajax request is being rejected because of the cross browser policy? you think?
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: using header to POST

Post by kendall »

AbraCadaver wrote: The way to POST data with PHP in general is to build a proper post request, fsockopen() to the server and fputs() your data, then fgets() the response. Or, take your pick of classes or functions here: http://us3.php.net/manual/en/book.http.php which may be easier.
Tell me something as I was trying to build a portable platform but my stupid host provider has ftp fsockopen and curl disabled in certain websites and im tired of the red tape process that i need to go through to get them enabled...the header() approach to POSTING seems to have been compatible enough

what does (PECL pecl_http >= 0.1.0) ? mean...as I am not familiar with the PECL is this something ALWAYS compiled with PHP? to what version should i have ?


ok i researched and realize this is not compiled. sorry I cant use this if its not pre-compiled with PHP already...
Last edited by kendall on Tue Oct 20, 2009 3:42 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: using header to POST

Post by John Cartwright »

I'm not sure if you can issue a POST through header() functions, never seen or tried it. You are far better off using a library such as cURL. In fact, the more I think about it, it probably won't work because the example you have given would represent invalid headers.

Generally a POST request would look like

Code: Select all

POST /foo HTTP1.1\r\n
Host: domain.com\r\n
Content-type: application/x-www-form-urlencoded\r\n
Content-length: 1024\r\n\r\n
foo=bar
Pay extra attention to the extra newlines prior to the actual post data, and since you do not pass header() newlines (because it adds them internally), I'm not sure it's possible. Not to mention the post data is usually streamed through the socket and not send with the initial request, I highly doubt this is workable.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: using header to POST

Post by John Cartwright »

kendall wrote:
AbraCadaver wrote:Tell me something as I was trying to build a portable platform but my stupid host provider has ftp fsockopen and curl disabled in certain websites and im tired of the red tape process that i need to go through to get them enabled...the header() approach to POSTING seems to have been compatible enough
If your host cannot accomodate your requirements, then the choice is obvious. Sianara.

Shared hosts/VPS hosts are a dime a dozen.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: using header to POST

Post by kendall »

John Cartwright wrote:I'm not sure if you can issue a POST through header() functions, never seen or tried it. You are far better off using a library such as cURL. In fact, the more I think about it, it probably won't work because the example you have given would represent invalid headers.
I myself thought so too until i was searching the php.net and found this

http://www.php.net/manual/en/function.header.php#89447

turns out alot of payment gateways use this...I was like wow... 8O
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: using header to POST

Post by John Cartwright »

If that is the case, then like I said, make sure you implement the newlines properly.
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Re: using header to POST

Post by kendall »

Well i went to do a simple test... but now i get a prompt to download the php script :crazy: Im not sure how come its doing that
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: using header to POST

Post by John Cartwright »

kendall wrote:Well i went to do a simple test... but now i get a prompt to download the php script :crazy: Im not sure how come its doing that
Try testing with a HTTP debugging tool, such as fiddler2, to see exactly what headers are being sent.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: using header to POST

Post by AbraCadaver »

You can chase this as long as you wish, but the fact remains that you cannot post with header(). It is used to send response headers to the client.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: using header to POST

Post by Jenk »

That snippet in the comment is flawed, as it is assuming you already have a connection open to $host. As AbraCadaver/Shawn is posting, you cannot POST to a new connection with header(), it is only for client responses.
Post Reply