Jump to content

Send post data with php


Recommended Posts

Hey there, I'm wanting to send post-data to a page without having to go through a form. How can I do this?

 

I've searched google and found many functions using fsocketopen() but none of them (atleast the ones I've tried) seem to work.

 

Here is an example: Instead of having to fill out an email form and submitting it to mail.php, I just want to be able to be able to load mail.php with the POST vars already set. Similar to the GET mail.php?id=1&do=2, I want to be able to do something similar with POST data.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/112944-send-post-data-with-php/
Share on other sites

you can do it but it is ugly.

 

You need to manually open the socket and generate the request header (including the $POST vars), then send the request.  So look up socket connections and http headers and that should tell you what you need to do.

 

I have heard that the cURL extension makes it easier, but haven't tried it.

you can do it but it is ugly.

 

You need to manually open the socket and generate the request header (including the $POST vars), then send the request.  So look up socket connections and http headers and that should tell you what you need to do.

 

I have heard that the cURL extension makes it easier, but haven't tried it.

 

Yeah, here is the script I am using. I found it online. But it doesn't seem to be working ???

 

<?php
/* sendToHost
* ~~~~~~~~~~
* Params:
*   $host      - Just the hostname.  No http:// or 
                  /path/to/file.html portions
*   $method    - get or post, case-insensitive
*   $path      - The /path/to/file.html part
*   $data      - The query string, without initial question mark
*   $useragent - If true, 'MSIE' will be sent as 
                  the User-Agent (optional)
*
* Examples:
*   sendToHost('www.google.com','get','/search','q=php_imlib');
*   sendToHost('www.example.com','post','/some_script.cgi',
*              'param=First+Param&second=Second+param');
*/

function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method)) {
        $method = 'GET';
    }
    $method = strtoupper($method);
    $fp = fsockopen($host, 80);
    if ($method == 'GET') {
        $path .= '?' . $data;
    }
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp,"Content-type: application/x-www-form- urlencoded\r\n");
    fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent) {
        fputs($fp, "User-Agent: MSIE\r\n");
    }
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST') {
        fputs($fp, $data);
    }

    while (!feof($fp)) {
        $buf .= fgets($fp,128);
    }
    fclose($fp);
    return $buf;
}

// Do it (the arguments match what I need.. i just edited them for online purposes)
sendToHost("www.domain.com","post","/troy/mailer.php","[email protected]&subject=Hey Subject&message=My mess");
?>

Try something like this:

	$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
	if (!$fp)
		echo "$errstr ($errno)<br />\n";
	else
	{
		$out = "POST /dir/page.html HTTP/1.1\r\n";
		$out .= "Host: www.website.com\r\n";
		$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$out .= "Content-Length: ".$size."\r\n";
		$out .= "var=data";
		fwrite($fp, $out);
		while (!feof($fp))
			echo fgets($fp, 128);
		fclose($fp);
	}

Try something like this:

	$fp = fsockopen("www.website.com", 80, $errno, $errstr, 30);
	if (!$fp)
		echo "$errstr ($errno)<br />\n";
	else
	{
		$out = "POST /dir/page.html HTTP/1.1\r\n";
		$out .= "Host: www.website.com\r\n";
		$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$out .= "Content-Length: ".$size."\r\n";
		$out .= "var=data";
		fwrite($fp, $out);
		while (!feof($fp))
			echo fgets($fp, 128);
		fclose($fp);
	}

 

Will do. Thanks.

 

Suggestions still welcome

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.