How can I send variables from a PHP script to another URL?
Results 1 to 4 of 4

Thread: How can I send variables from a PHP script to another URL?

  1. #1
    Junior Member
    Join Date
    Sep 2004
    Posts
    7

    How can I send variables from a PHP script to another URL?

    Hello,

    Please tell me How can I send variables from a PHP script to another URL using POST without using forms and hidden variables?

    Thanks
    Laxy
    www.funspot.com

  2. #2
    Senior Member
    Join Date
    Feb 2004
    Posts
    270
    I believe the point of POST is to be hidden. Maybe you are looking for GET ?

    If you don't know what that is look it up in the documentation.
    Since the beginning of time, Man has searched for the answers to the big questions: \'How did we get here?\' \'Is there life after death?\' \'Are we alone?\' But today, in this very theatre, you will be asked to answer the biggest question of them all...WHO LIVES IN A PINEAPPLE UNDER THE SEA?

  3. #3
    Junior Member
    Join Date
    Sep 2004
    Posts
    7
    Thanks MoonWolf for your quick reply i will check and get back to you............

  4. #4
    Senior Member
    Join Date
    Mar 2002
    Posts
    502
    You would have to open a direct connection with the server you would want to connect to, and manually send the HTTP POST request containing the variables.

    Here's a small function which should be able to handle this.
    Code:
    function post_page($host, $path, $post_data)
    {
    	// Opens a connection to the server
    	$fp = fsockopen($host, 80, &$error_number, &$error_string, 120);
    
    	// Trigger an error if we failed to connect, else continue
    	if (!$fp)
    	{
    		trigger_error('Connection error: ' . $error_string . ' (' . $error_number . ')', E_USER_ERROR);
    		return FALSE;
    	}
    
    	// See how many bytes our POST data is
    	$data_length = strlen($post_data);
    
    	// Send our HTTP POST request to the server
    	fputs($fp, "POST $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: $data_length\r\n");
    	fputs($fp, "Connection: close\r\n\r\n");
    	fputs($fp, $post_data);
    
    	// As long as the server returns data, keep getting it in 1k chunks
    	$output = '';
    	while(!feof($fp))
    	{
    		$output .= fgets($fp, 1024);
    	}
    
    	// Close the connection
    	fclose($fp);
    
    	// Return any data returned by the server
    	return $output;
    }
    The above function accepts 3 variables. The $host variable should contain the hostname of the server, ie "www.example.com" or "example.com" without the protocol or leading slash. The $path variable should contain the path to the script you want the data to be posted to. For instance, "/foo/bar/script.php" or "/cgi/process_this.cgi". Finally, the $post_data variable should contain a string which specifies the data you want to post. This is easier to explain by showing an example.

    For instance, if you would use the following function with the following $post_data value:
    Code:
    variable_name=value&variable_name2=578&variable_name3=me
    The script you'd contact would have access to 3 POST variables called "variable_name", "variable_name2", and "variable_name3", with the values "value", "578", and "me" respectively.

    I have not tested the function, but it should work. If you have any problems, reply or PM me.
    Bleh.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •