homepage Welcome to WebmasterWorld Guest from 184.73.104.82
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
POSTing *from* php
do I have to write the functions myself?
dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 7:41 pm on Nov 25, 2002 (gmt 0)

Anyone know if there is a way to send an HTTP POST request from within a PHP script, short of opening a socket connection and writing HTTP out to it? I don't know of such a thing, and all I find in the PHP docs is a function that makes GET requests.

Making POSTs from a wab script is probably a fairly unusual thing to do, but it seems like I shouldn't be the first person to need it. Of course, I can implement it myself if I have to, but I'd rather use a canned solution if there is one.

 

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 948 posted 7:49 pm on Nov 25, 2002 (gmt 0)

I'm not sure what the answer is but I'm curious as to what you're trying to accomplish by executing a POST without normal form controls?

aaronc

10+ Year Member



 
Msg#: 948 posted 8:10 pm on Nov 25, 2002 (gmt 0)

I think you're looking for Curl.
You'll have to make sure php is complied to use it though. I don't think it's standard.

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 8:17 pm on Nov 25, 2002 (gmt 0)

I know I could do it by making an external call to curl. I didn't know that curl could be used from within PHP, though.

Lorax - what I have in mind is making a 'post' request to a script on another server, and processing the results into my own output. Kinda like PayPal IPN.

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 8:17 pm on Nov 25, 2002 (gmt 0)

Hey Andrew, I just found this in one of my law text books. ;)
It might help. Client URL Library Functions [php.net]
Dinnerīs ready. Gotta go.
But I`ll just eat in front of my computer and read some more.
Andreas

Should have submitted this before finishing dinner. Now AaronC beat me to it.

lorax

WebmasterWorld Administrator lorax us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 948 posted 8:27 pm on Nov 25, 2002 (gmt 0)

dingman,
As aaronc and Andreas said, cURL would be the easiest way to execute it. You might be able to hack together another solution though it would probably be far less elegant.

<added>I don't suppose the data the form returns is in XML format. If it were you might be able to use XML-RPC.</added>

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 9:09 pm on Nov 25, 2002 (gmt 0)

Thanks, everyone. I hadn't realized there was a lubcurl interface from PHP. I think that's just what I'm looking for.

<added>I don't suppose the data the form returns is in XML format. If it were you might be able to use XML-RPC.</added>

I wish :)

transistor

10+ Year Member



 
Msg#: 948 posted 5:29 pm on Nov 27, 2002 (gmt 0)

Two methods I have used:
1- CURL
2- HTML/JavaScript: Use PHP to write a form and submit automaticaly with JavaScript document.form.submit().

Allen

10+ Year Member



 
Msg#: 948 posted 2:32 pm on Nov 28, 2002 (gmt 0)

If your trying to submit POST data to another server (and/or get the response), I wrote a script which does this for NoChex APC system. There's a link on there for the code snippet I found which allowed me to do this.

Allen

[edited by: jatar_k at 6:09 pm (utc) on Nov. 28, 2002]

cyberax

10+ Year Member



 
Msg#: 948 posted 1:39 pm on Nov 29, 2002 (gmt 0)

You could always build up the POST header yourself using the header() function e.g...

<?php
// function to read server name
function servername($txt)
{
if (substr(strtoupper($txt),0,4)=="WWW.")
$txt="HTTP://".$txt;
if (substr(strtoupper($txt),0,7)!="HTTP://";)
return 0;
eregi("^(http://([^/ ]+))",$txt,$arr);
return $arr[2];
}

$remote_url = "http://www.domain.com/script.php";

// built up POST data
$request = "?name=Paul&greatness=high";

// Build the header
$header = "POST $remote_url HTTP/1.0\r\n";
$header .= "Content-type: application/x-www-form-urlencoded\r\n";
$header .= "Content-length: " . strlen($request) . "\r\n\r\n";

// Open the connection
$fp = fsockopen(servername($remote_url), 80);

if ($fp) {
// Send HTTP request
fputs($fp, $header . $request);

// Get the response
$response="";
while (!feof($fp))
$response .= fgets($fp, 128);

fclose($fp);

print $response;
} else {
die ("Cannot connect!");
}
?>


andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 1:22 am on Nov 30, 2002 (gmt 0)

In the original post Andrew wrote:
short of opening a socket connection and writing HTTP out to

Now you are telling him to open a socket connection and to write HTTP out to ;)

Andreas

cyberax

10+ Year Member



 
Msg#: 948 posted 1:36 am on Nov 30, 2002 (gmt 0)

Oops!

Maybe I should read posts properly before I reply rather than just scan over them!

ruserious

10+ Year Member



 
Msg#: 948 posted 2:34 pm on Dec 8, 2002 (gmt 0)

Here is a httpClient class written in PHP which will let you do a lot more stuff:

lwest.free.fr/doc/php/lib/index.php3?page=net_http_client&lang=en

I found it extremely useful.

[edited by: jatar_k at 6:38 pm (utc) on Dec. 9, 2002]
[edit reason] delinked [/edit]

kindacheezy

10+ Year Member



 
Msg#: 948 posted 10:25 pm on Jan 5, 2003 (gmt 0)

can you use a header like this

$redirect=("Location:http://whatever.php?2=".$myVar1."&1=".$myVar2);
header($redirect);

andreasfriedrich

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 12:29 am on Jan 6, 2003 (gmt 0)

Sure.

Note: HTTP/1.1 requires an absolute URI as argument to Location

[php.net...]

The query string is a valid part of a URI and may therefor be used as a URI with the Location header.

Andreas

dingman

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 948 posted 12:55 am on Jan 6, 2003 (gmt 0)

Though it is worth noting that this will result in a 'GET' request rather than a 'POST' request. Different beasts.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved