Welcome to WebmasterWorld Guest from 54.160.254.203

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

POSTing *from* php

do I have to write the functions myself?

     
7:41 pm on Nov 25, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 12, 2002
posts:885
votes: 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.

7:49 pm on Nov 25, 2002 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 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?
8:10 pm on Nov 25, 2002 (gmt 0)

New User

10+ Year Member

joined:Oct 29, 2002
posts:28
votes: 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.
8:17 pm on Nov 25, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 12, 2002
posts:885
votes: 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.

8:17 pm on Nov 25, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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.

8:27 pm on Nov 25, 2002 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7575
votes: 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>

9:09 pm on Nov 25, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 12, 2002
posts:885
votes: 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 :)
5:29 pm on Nov 27, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:May 2, 2002
posts:141
votes: 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().
2:32 pm on Nov 28, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 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]

1:39 pm on Nov 29, 2002 (gmt 0)

New User

10+ Year Member

joined:Aug 22, 2002
posts:38
votes: 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!");
}
?>

1:22 am on Nov 30, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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

1:36 am on Nov 30, 2002 (gmt 0)

New User

10+ Year Member

joined:Aug 22, 2002
posts:38
votes: 0


Oops!

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

2:34 pm on Dec 8, 2002 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 26, 2002
posts:173
votes: 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]

10:25 pm on Jan 5, 2003 (gmt 0)

New User

10+ Year Member

joined:Jan 5, 2003
posts:6
votes: 0


can you use a header like this

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

12:29 am on Jan 6, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 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

12:55 am on Jan 6, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 12, 2002
posts:885
votes: 0


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

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members