简体   繁体   English

将$ _GET或$ _POST数据传递给使用wget运行的PHP脚本

[英]Passing $_GET or $_POST data to PHP script that is run with wget

I have the following line of PHP code which works great: 我有以下PHP代码行很好用:

exec( 'wget http://www.mydomain.com/u1.php > /dev/null &' ); exec('wget http://www.mydomain.com/u1.php > / dev / null&');

u1.php acts to do various types of maintenance on my server and the above command makes it happen in the background. u1.php用于在我的服务器上进行各种类型的维护,上面的命令使它在后台运行。 No problems there. 没有问题。

But I need to pass variable data to u1.php before it's executed. 但是我需要在执行之前将变量数据传递给u1.php。 I'd like to pass POST data preferably, but could accommodate GET or SESSION data if POST isn't an option. 我想优先传递POST数据,但如果POST不是一个选项,则可以容纳GET或SESSION数据。 Basically the type of data being passed is user-specific and will vary depending on who is logged in to the site and triggering the above code. 基本上,传递的数据类型是特定于用户的,具体取决于登录到站点的用户并触发上述代码。 I've tried adding the GET data to the end of the URL and that didn't work. 我已经尝试将GET数据添加到URL的末尾,但这不起作用。

So how else might I be able to send the data to u1.php? 那么我怎样才能将数据发送到u1.php呢? POST data preferred, SESSION data would work as well (but I tried this and it didn't pick up the logged in user's session data). 首选POST数据,SESSION数据也可以正常工作(但我尝试了这个并且它没有获取登录用户的会话数据)。 GET would be a last resort. GET将是最后的选择。

Thanks! 谢谢!

To do it the way you have you would do this, probably using http_build_query 要按照你的方式做到这一点,可能会使用http_build_query

exec("wget --post-data 'a=b&c=d' http://www.mydomain.com/u1.php > /dev/null &");

However, I would be curious to know why you are choosing to use wget over cURL or, if the file is on the same server, calling it directly. 但是,我很想知道为什么你选择在cURL上使用wget,或者如果文件在同一台服务器上,则直接调用它。 It seems a bit clumsy to use wget for this. 使用wget似乎有点笨拙。

Why dont you use cURL (if it's an option)? 为什么不使用cURL(如果它是一个选项)? You can pass POST data very easy. 您可以非常轻松地传递POST数据。

curl -d "your=postdata&more=postdata" http://www.mydomain.com/u1.php

Why not use the cURL implementation directly? 为什么不直接使用cURL实现?

$c = curl_init('http://www.mydomain.com/u1.php');
curl_setopt($c, CURL_POSTFIELDS, $post_data);
curl_exec($c);

EDIT: To submit $post_data , you construct a string like a GET query string: 编辑:要提交$post_data ,您构造一个类似GET查询字符串的字符串:

$vData = array(
  'foo' => 'bar',
  'zoid' => 'frob',
  'slursh' => 'yargh',
);

$post_data = array();

foreach ($vData as $k => $v)
   {
       $post_data[] = urlencode($k) . '=' . urlencode($v);
   }

$post_data = implode('&', $post_data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM