down00111 2012-08-01 18:13
浏览 20
已采纳

使用PHP Switch语句将表单数据发送到随机URL

So i am trying to wrap my head around this logic. this is code that is on a form processing script. What i want to do is send form data to one of these urls randomly. I was reading to use the switch case logic, but when i use the code you see below, it submits the form data to all 3 URLS. Is there a way so that it only sends to one of them?

function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) { 
  $fields .= $key . '=' . $value . '&'; 
}
rtrim($fields, '&');

$post = curl_init();

curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, 1);
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);


$result = curl_exec($post);

curl_close($post);
}



return $result;

$x = rand(1,3);

switch ($x) {
    case 1:
        post_to_url("http://examplesite1.com/cgi-bin/maxuseradmin.cgi", $data2);
        break;

    case 2:
        post_to_url("http://examplesite2?fid=6646588e54", $data3);
        break;

    case 3:
        post_to_url("http://examplesite1?fid=2fb44e3888", $data4);
        break;
    }

the $data variables are arrays --Thanks for the help

  • 写回答

5条回答 默认 最新

  • donglu5000 2012-08-01 18:18
    关注

    You're posting to all 3 pages, then setting the value of what it returns to $ar1 $ar2 and $ar3.

    Store your options in an array, then call post_to_url() once.

    $urls = array(
        array("http://examplesite1.com/cgi-bin/maxuseradmin.cgi", $data2),
        array("http://examplesite2?fid=6646588e54", $data3),
        array("http://examplesite1?fid=2fb44e3888", $data4)
    );
    
    $x = rand(0,2);
    post_to_url ($urls[$x][0], $urls[$x][1]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!