java 将字符串变量从java传递给php函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12474721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:02:49  来源:igfitidea点击:

Pass string variable from java to php function

javaphpandroid

提问by user1494142

I am a beginner java and php I must pass a string variable from Java client to php server using WebView

我是初学者 java 和 php 我必须使用 WebView 将字符串变量从 Java 客户端传递到 php 服务器

Is what I am doing right?

我做的对吗?

In java side:

在java端:

String Coords=CoordsString;
String PHPPagePath="http://10.0.2.2/ReceiveLocation.php?Coords=" + CoordsString"; 

and on the php side: ReceiveLocation.php

在 php 端:ReceiveLocation.php

 <?php
include('ConnectionFunctions.php');
Connection(); 
$x=$_GET['Coords'];
GetCoords($x);
function GetCoords($x)
{
   echo $x;
}   
?>

Is this the right way to pass the parameter Coords from Java Client to PHP Server Function?

这是将参数 Coords 从 Java Client 传递到 PHP Server Function 的正确方法吗?

回答by gd1

To pass a parameter to PHP using the URL of the page, just append ?key=valueto the URL, where keyis the parameter key (i.e. the name) and valueis its value (i.e. the data you are trying to transfer), and then in the PHP script you can retrieve the parameter like this:

要使用页面的 URL 将参数传递给 PHP,只需将参数附加?key=value到 URL,其中key是参数键(即名称)和value它的值(即您尝试传输的数据),然后在 PHP 脚本中您可以像这样检索参数:

<?php
    echo 'I have received this parameter: '.$_GET['key'];
?>

replacing 'key'with the actual name of the parameter.

替换'key'为参数的实际名称。

This is the way PHP reads HTTP GET variables. See this for more information: http://www.php.net/manual/en/reserved.variables.get.php

这就是 PHP 读取 HTTP GET 变量的方式。有关更多信息,请参阅:http: //www.php.net/manual/en/reserved.variables.get.php

Please, be careful when accepting variables from outside: they have to be "sanitized", expecially if they are going to be used in database queries or printed in the HTML page.

从外部接受变量时请小心:它们必须被“清理”,特别是如果它们将用于数据库查询或打印在 HTML 页面中。

回答by Garbage

Modify your code as following

修改你的代码如下

Java code:

爪哇代码:

String Coords=CoordsString;
String PHPPagePath="http://10.0.2.2/ReceiveLocation.php?Coords=10";

PHP code:

PHP代码:

<?php
include('ConnectionFunctions.php');
Connection(); 

function GetCoords(x)
{
echo 'Coords = '.$_GET['Coords'];

}   
?>

回答by Yogesh Suthar

use $_GET['parameter_name']in PHPif you send parameter using GETmethod

使用$_GET['parameter_name']PHP,如果您发送的参数使用GET方法

OR

或者

use $_POST['parameter_name']in PHPif you send parameter using POSTmethod

使用$_POST['parameter_name']PHP,如果您发送的参数使用POST方法

and its alternate is also $_REQUESTif you send parameter using POST OR GET.

$_REQUEST如果您使用 POST 或 GET 发送参数,它的替代方法也是如此。

回答by PeterMmm

On Java side you have to make a request. As this is http you should use a library like httpclient. Your request will contain the data like this:

在 Java 方面,您必须提出请求。由于这是 http,您应该使用像httpclient这样的库。您的请求将包含如下数据:

HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://10.0.2.2/ReceiveLocation.php");
method.addParamezter("Coord","1x3x4"); // or whatever
int statusCode = client.executeMethod(method);

Work on the tutorials at httpclient side.

在 httpclient 端学习教程。

On PHP side youe have to read the posted data...

在 PHP 方面,您必须阅读发布的数据...

right now I found this that will help you: http://www.coderblog.de/sending-data-from-java-to-php-via-a-post-request/

现在我发现这可以帮助你:http: //www.coderblog.de/sending-data-from-java-to-php-via-a-post-request/

回答by vilas

This trick is to process java variable in php !!!'

这个技巧是在php中处理java变量!!!'

<?php
$java_variable_in_php = 'string'; //Define our  variable.  
?>          
 <script> js_variable_name = "<?php echo $java_variable_in_php; ?>";
 string=' this is my trick to process java variable in php !!!';
 document.write(js_variable_name);//will print java variable name 'string'
 </script>



<?php echo '<script>document.write(js_variable_name)</script>';?>
<?php echo '<script>document.write('.$java_variable_in_php.')'.'</script>';?>

check view source of the output !! Hope this will help someone...

检查输出的查看源!希望这会帮助某人...