Php Post

Discussion in 'PHP' started by Dehisce, Nov 3, 2007.

Thread Status:
Not open for further replies.
  1. #1
    I originally was using a simple php script which used wget to load another php page with variables, however this is not longer working and I have to use POST instead. Is there a way to get php to POST variables to a URL ?
     
    Dehisce, Nov 3, 2007 IP
  2. Dehisce

    Dehisce Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Didn't explain it very well, but I hope you understand :D
     
    Dehisce, Nov 3, 2007 IP
  3. brendandonhue

    brendandonhue Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    brendandonhue, Nov 3, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    nico_swd, Nov 3, 2007 IP
  5. Dehisce

    Dehisce Peon

    Messages:
    234
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I'll give it a go
     
    Dehisce, Nov 4, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Needs PHP 5: (Only use if you don't have cURL)

    
    function http_post($url, array $data, $get_response = false)
    {
    	$data = http_build_query($data, '', '&');
    	$urlinfo = @parse_url($url);
    	
    	if (!$fp = @fopen($url, 'rb', false, stream_context_create(array(
    		'http' => array(
    			'method'     => 'POST',
    			'header'     => "Content-Type: application/x-www-form-urlencoded\r\n" .
    							"Content-Length: " . strlen($data) . "\r\n" .
    							"Referer: {$urlinfo['scheme']}://{$urlinfo['host']}/",
    			'content'    => $data,
    			'user_agent' => $_SERVER['HTTP_USER_AGENT']
    		)
    	))))
    	{
    		trigger_error(sprintf('Unable to open URL %s', $url), E_USER_WARNING);
    		return false;
    	}
    	
    	if ($get_response)
    	{
    		$response = '';
    		while (!feof($fp) AND !connection_aborted())
    		{
    			$response .= fgets($fp);
    		}
    	}
    	
    	fclose($fp);
    	return $get_response ? $response : true;
    }
    
    
    PHP:
    Usage example:
    
    $url = 'http://example.com/login.php';
    $return_response = true;
    $data = array(
        'username' => 'foo',
        'password' => 'bar'
    );
    
    echo http_post($url, $data, $return_response);
    
    PHP:
     
    nico_swd, Nov 4, 2007 IP
Thread Status:
Not open for further replies.