Welcome to WebmasterWorld Guest from 54.161.25.213

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How to send post data to a url with php

but not using a form submit button

     
5:08 am on Nov 22, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Apr 30, 2006
posts:298
votes: 0


I want to send post data to a url, but don't know how. It seems there should be a simple php funciton like this:

post_to_url ($url, $data);

But my searches have turned up 0.

9:14 am on Nov 22, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member dreamcatcher is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 30, 2003
posts:3719
votes: 0


Hi pixeltierra,

Sounds like a job for Curl [uk2.php.net].

dc

9:20 pm on Nov 22, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Nov 26, 2003
posts:1133
votes: 0


or just use:

<form name="test" action="test.php" method="GET">...

Regards
Michal

1:01 am on Nov 23, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Apr 30, 2006
posts:298
votes: 0


Mcibor:

This won't work for a couple reasons. 1) I need post data, not GET, and 2)I wan't to send the data to the url from php, not from a web page click.

I think curl is the way to go. I've looked into it. It can do so much more than just send post data, which makes it intimidating.

2:15 am on Nov 23, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 15, 2005
posts:79
votes: 0


If you wanted to just use PHP you could have the script write the data to a .txt file ex:

$a = '1';
$b = '2';

...

Then have the url read the .txt file in it's script.

Willaim.

3:49 am on Nov 23, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:June 20, 2003
posts:65
votes: 0


Try this:

$server= 'www.someserver.com';
$url = '/path/to/file.php';
$content = 'name1=value1&name2=value2';
$content_length = strlen($content);
$headers= "POST $url HTTP/1.0\r\nContent-type: text/html\r\nHost: $server\r\nContent-length: $content_length\r\n\r\n";
$fp = fsockopen($server, $port, $errno, $errstr);
if (!$fp) return false;
fputs($fp, $headers);
fputs($fp, $content);
$ret = "";
while (!feof($fp)) {
$ret.= fgets($fp, 1024);
}
fclose($fp);
print $ret;

I think there's an easier way in php5 (using stream_context_create) but I haven't upgraded yet.
9:59 am on Nov 23, 2006 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 11, 2003
posts:1379
votes: 0


Yeah, Wez Furlong recently wrote the PHP5 version on his blog (titled HTTP POST from PHP, without cURL):

function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array(
'method' => 'POST',
'content' => $data
));
if ($optional_headers!== null) {
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp) {
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}

In the post he mentions that he always has to look up how to do this. Funny because he's one of the core developers!