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

图片转代码服务由CSDN问答提供 功能建议

所以我试图绕过这个逻辑。 这是表单处理脚本上的代码。 我想要做的是随机将表单数据发送到这些网址之一。 我正在阅读使用switch case逻辑,但是当我使用下面看到的代码时,它会将表单数据提交给所有3个URL。 有没有办法只发送给其中一个?

  function post_to_url($ url,$ data){
 $ fields =''; 
foreach($ 数据为$ key => $ value){
 $ fields。= $ key。  '='。  $ value。  '&安培;';  
} 
rtrim($ fields,'&'); 
 
 $ post = curl_init(); 
 
 ncurl_setopt($ post,CURLOPT_URL,$ url); 
 ncurl_setopt($ post,CURLOPT_POST,1)  ; 
curl_setopt($ post,CURLOPT_POSTFIELDS,$ fields); 
 
 
 $ result = curl_exec($ post); 
 
 ncurl_close($ post); 
} 
 
 
 
返回$ result  ; 
 
 $ x = rand(1,3); 
 
switch($ x){
 case 1:
 post_to_url(“http://examplesite1.com/cgi-bin/maxuseradmin.cgi”  ,$ data2); 
 break; 
 
案例2:
 post_to_url(“http:// examplesite2?fid = 6646588e54”,$ data3); 
 break; 
 
案例3:
 post_to_url  (“http:// examplesite1?fid = 2fb44e3888”,$ data4); 
 break; 
} 
   
 
 

$ data变量是数组 - 谢谢 帮助

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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]);
    
    打赏 评论
  • duanji5116 2012-08-01 18:17

    The issue here is that you are calling the functions before the switch statement even happens. This code should work for you as it calls the functions only inside the switch statement:

    $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;
    }
    
    打赏 评论
  • douxi3404 2012-08-01 18:18

    In the first 3 lines you calling your post_to_url function

    Change your code to

    $urls = array(
       "http://examplesite1.com/cgi-bin/maxuseradmin.cgi",
       "http://examplesite2?fid=6646588e54",
       "http://examplesite1?fid=2fb44e3888"
    );
    
    # don't know what's in $data* but just to give you an idea
    $data = array(
        $data2,
        $data3,
        $data4
    );
    $x = rand(0,2);
    post_to_url($urls[x], $data[x]);
    
    打赏 评论
  • duanjuebin2519 2012-08-01 18:18

    Call to the function post_to_url should be inside the switch case statement. The reason it posts to all 3 is that you are calling this function 3 times even before going to switch case.

    打赏 评论
  • duanjiao6735 2012-08-01 18:19

    You are actually executing your post_to_url function three times on the first three lines. You would want to do something like:

    $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;
    }
    
    打赏 评论

相关推荐 更多相似问题