PHP Snippet : Redirect URL with Send POST data

Dika
Written by Dika on

Selamat siang bro, kali ini saya akan memposting code snippet buat redirect url bersamaan dengan post data ke url yang akan di direck.. oke coba lihat code di bawah :

<?php
$data_array =array('name'=>'Ferdhika Yudira','domain'=>'www.dika.web.id');
$data = http_build_query($data_array);

echo redirectPost('http://localhost/latihan/dika/redirect-post-data/baca.php', $data); //lokasi si file baca.php (link tujuan redirect)

function redirectPost($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;
}
?>

dan buat satu file lagi untuk tujuan redirect tadi.. beri nama baca.php

<?php 
	/**
		* @Author				: Localhost {Ferdhika Yudira}
		* @Email				: [email protected]
		* @Web					: http://dika.web.id
		* @Date					: 2015-02-18 13:07:25
	**/
	echo $_POST['name'];
	echo $_POST['domain'];
?>

Semoga bermanfaat.. :D

Comments

comments powered by Disqus