Pass $_post Variables Without Forms
by GizzmoAsus on Jan.11, 2008, under PHP Code
Intro
Ever wanted to send variables to another script without clogging up the URI? Well the following is a function which allows you to do just that:
Usage
To use this function just call it
$something = sendPost($URL_of_the_file_you_want_to_pass_the_variables_to, $an_array_containing_the_variables_you_wish_to_send)
Note: The array needs to be made up in the following way:
$context = array( 'post_variable1' => 'post_value1', 'post_variable2' => 'post_value2', 'post_variable3' => 'post_value3' );
Code
function sendPost($url, $context)
{
$context = array_change_key_case($context, CASE_LOWER);
$contextSize = sizeof($context);
$contextKeys = array_keys($context);
$data = "";
foreach ($contextKeys as $key)
{
$amp = "&";
if (next($contextKeys) === FALSE) { $amp = ""; }
$data .= urlencode($key)."=".urlencode($context[$key]).$amp;
}
list($host,$url) = explode('/',$url,2);
$sock = fsockopen($host,80,$errno,$errstr,1);
if (!$sock) { die("$errstr ($errno) in ".__FILE__." (".__LINE__.")\n"); }
$http_request = "POST /".$url." HTTP/1.1\r\n";
$http_request .= "Host: ".$host."\r\n";
$http_request .= "Content-type: application/x-www-form-urlencoded\r\n";
$http_request .= "Content-length: " . strlen($data) . "\r\n";
$http_request .= "Accept: */*\r\n";
$http_request .= "\r\n";
$http_request .= "$data\r\n";
$http_request .= "\r\n";
fwrite($sock,$http_request);
$response_headers = array();
while ($str = trim(fgets($sock)))
array_push($response_headers,$str);
fclose($sock);
$errors = true;
$errorMsg = "";
foreach($response_headers as $response_header){
if($response_header == 'HTTP/1.1 200 OK'){
$errors = false;
}elseif($response_header == 'X-UFU: OK'){
$errors = false;
}
}
if (!$errors)
return true;
else
return false;
}
Enjoy and please free to leave a comment if you use this code
June 29th, 2008 on 10:54 am
>$context = array(
> ‘post_variable1′ = ‘post_value1′,
> ‘post_variable2′ = ‘post_value2′,
> ‘post_variable3′ = ‘post_value3′
>)
Is this realy correct? Shouldn’t there be a ; in the end? When I’m running the code it complains about =.
error msg: Parse error: syntax error, unexpected ‘=’, expecting ‘)’
I’ve tryed to put “” around the array elements like this. “‘post_variable1′ = ‘post_value1′”, This makes it run but stops whith this
error msg: Parse error: syntax error, unexpected T_VARIABLE poiting to the line where I have $something = sendPost($URL, $context)
my code
________________________________
start
…..
include “../scripts/sendPost.php”;
$test1=test1
$test2=test2
$test3=test3
$context = array (
“‘test1′ = $test1″,
“‘test2′ = $test2″,
“‘test3′ = $test3″
);
$URL=”127.0.0.1/print.php”
$something = sendPost($URL, $context)
….
end
________________
June 29th, 2008 on 15:08 pm
You are indeed correct the array does need the ; at the end of it. The other thing I forgot to add into it was the > signs which are used to assign the values to the keys.
So the context array should look like:
$context = array (
‘test1′ => $test1,
‘test2′ => $test2,
‘test3′ => $test3
);
But you can also define it in the following way.
$context['test1'] = $test1;
$context['test2'] = $test2;
$context['test3'] = $test3;
The quotes around the values denotes that the values are strings if you are using variables then you don’t need the quotes. So your variable declarations need to be:
$test1 = ‘test1′;
$test2 = ‘test2′;
$test3 = ‘test3′;
PHP.net offers some good documentation on arrays that you might find useful