尝试使用curl()在其他服务器上提交表单

时间:2014-01-20 11:19:16

标签: php forms curl form-submit

我搜索了很多论坛以及@ stackoverflow,但我不能让我的脚本提交表单。我可以看到其他人管理这项工作但不是我自己。 页面上没有任何更新/更改。

RESULT

Array ( 
[url] => http://xxxxxxx.nu/login/index.php' 
[content_type] => text/html; charset=iso-8859-1 
[http_code] => 404 
[header_size] => 145 
[request_size] => 467 
[filetime] => -1 
[ssl_verify_result] => 0 
[redirect_count] => 0 
[total_time] => 0.01751 
[namelookup_time] => 0.001108 
[connect_time] => 0.004769 
[pretransfer_time] => 0.005047 
[size_upload] => 264 
[size_download] => 214 
[speed_download] => 12221 
[speed_upload] => 15077 
[download_content_length] => 214 
[upload_content_length] => 264 
[starttransfer_time] => 0.017168 
[redirect_time] => 0 
[certinfo] => Array ( ) 
[redirect_url] => ) 0-

这是我的尝试

<?
$www = "http://xxxxxxxxxxxxxxxxxx.se/index.php";
//create array of data to be posted
$post_data['mandag_1'] = 'måndag 1';
$post_data['tisdag_1'] = 'tisdag 1';
$post_data['onsdag_1'] = 'onsdag 1';
$post_data['torsdag_1'] = 'torsdag 1';
$post_data['fredag_1'] = 'fredag 1';
$post_data['week'] = '4';
$post_data['food'] = 'y';
$post_data['usersettings'] = 'y';
$post_data['status'] = '1';
$post_data['adress'] = 'Falugatan';
$post_data['stad'] = 'Falun';
$post_data['old_menu'] = '1';
$post_data['menysort'] = '1';
$post_data['telefon'] = '023';
$post_data['hemsida'] = '';
$post_data['epost'] = '';
$post_data['changepassword'] = '';
$post_data['oldpass'] = '';
$post_data['newpass1'] = '';
$post_data['newpass2'] = '';
$post_data['lunchinfo'] = '';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = 
  curl_init('http://xxxxxxxxxxx.nu/login/index.php');

//set options
curl_setopt($curl_connection, CURLOPT_POST, 1); 

curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . 
                curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
echo '<br>TTT'.$post_string;
?>

以下是远程页面上表单的外观

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lunchguide.nu</title>
    <link rel="stylesheet" type="text/css" href="../style.css?19972" />
    <link rel="stylesheet" type="text/css" href="login.css?90004" />
<script type="text/javascript" src="//use.typekit.net/srs7uku.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="../js/showSingleMap.js"></script>

<link rel="stylesheet" type="text/css" media="only screen and (max-width: 1220px) and (min-width: 1px), 
    only screen and (max-device-width: 1220px)" href="../style-1220.css?67124" />
    <link rel="stylesheet" type="text/css" media="only screen and (max-width: 920px) and (min-width: 1px), 
    only screen and (max-device-width: 920px)" href="../style-940.css?8540" />
    <link rel="stylesheet" type="text/css" media="only screen and (max-width: 620px) and (min-width: 1px), 
    only screen and (max-device-width: 620px)" href="../style-620.css?89448" />


    <meta name="viewport" content="width=device-width" />
    <!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="style-ie.css" />
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->


</head>
<body>

    <script type="text/javascript" src="../js/showhide.js"></script>
    <script type="text/javascript" src="../js/jquery.placeholder.js"></script>
<div id='container'>
<div id='header'>

    <a id="logo" href="index.php"><img src="../img/header_logo_02_2x.png"/></a>
    <a id="title" href="index.php"><img src="../img/header_title_02_x2.png"/></a>
    <div class="smallclear"></div>
    <div id="settingspanel" class="slidingDiv">
    <a href="logout.php">
    <div class="logout"><img src="../img/logout.png" /></div>
    </a>

        <div class="clear"></div>
</div>
    <div id="holder">


<div id='single' style='margin-top:20px;'><form enctype='multipart/form-data' action='' method='POST'><div class='heading'><div class='left'><h3>Lägg till/ändra menyer</h3></div><div class='weekover'><div class='left'><span>Vecka </span><a href='#' id='4' class='active populatedWeek'>4</a><a href='#' id='5' class=''>5</a><a href='#' id='6' class=''>6</a><a href='#' id='7' class=''>7</a><a href='#' id='8' class=''>8</a><a href='#' id='9' class=''>9</a><a href='#' id='10' class=''>10</a><a href='#' id='11' class=''>11</a><a href='#' id='12' class=''>12</a><a href='#' id='13' class=''>13</a></div></div><input type='submit' class='submit' value='Spara meny' /></div><div class='weekunder'><div class='heading'><div class='left'><span>Vecka </span><a href='#' id='4' class='active populatedWeek'>4</a><a href='#' id='5' class=''>5</a><a href='#' id='6' class=''>6</a><a href='#' id='7' class=''>7</a><a href='#' id='8' class=''>8</a><a href='#' id='9' class=''>9</a><a href='#' id='10' class=''>10</a><a href='#' id='11' class=''>11</a><a href='#' id='12' class=''>12</a><a href='#' id='13' class=''>13</a></div></div></div><div class='day' style='background-color:#fff;'>
<h4>Måndag 20/1</h4><p>Maträtt 1 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='1' name='mandag_1' autocomplete='off'>...</textarea><p>Maträtt 2 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='2' name='mandag_2' autocomplete='off'></textarea><p>Maträtt 3 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='3' name='mandag_3' autocomplete='off'></textarea><div class='clear'></div></div><div class='day' style='background-color:#fff;'>
<h4>Tisdag 21/1</h4><p>Maträtt 1 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='4' name='tisdag_1' autocomplete='off'></textarea><p>Maträtt 2 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='5' name='tisdag_2' autocomplete='off'></textarea><p>Maträtt 3 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='6' name='tisdag_3' autocomplete='off'></textarea><div class='clear'></div></div><div class='day' style='background-color:#fff;'>
<h4>Onsdag 22/1</h4><p>Maträtt 1 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='7' name='onsdag_1' autocomplete='off'></textarea><p>Maträtt 2 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='8' name='onsdag_2' autocomplete='off'></textarea><p>Maträtt 3 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='9' name='onsdag_3' autocomplete='off'></textarea><div class='clear'></div></div><div class='day' style='background-color:#fff;'>
<h4>Torsdag 23/1</h4><p>Maträtt 1 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='10' name='torsdag_1' autocomplete='off'></textarea><p>Maträtt 2 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='11' name='torsdag_2' autocomplete='off'></textarea><p>Maträtt 3 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='12' name='torsdag_3' autocomplete='off'></textarea><div class='clear'></div></div><div class='day' style='background-color:#fff;'>
<h4>Fredag 24/1</h4><p>Maträtt 1 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='13' name='fredag_1' autocomplete='off'></textarea><p>Maträtt 2 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='14' name='fredag_2' autocomplete='off'></textarea><p>Maträtt 3 - <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>60</span></p><textarea tabindex='15' name='fredag_3' autocomplete='off'></textarea><div class='clear'></div></div>
<input type='hidden' name='week' value='4' />
<input type='hidden' name='food' value='y' />
<script type="text/javascript">



    $(".day textarea").keyup(function () {
        var maxchars = 60;
     var tlength = $(this).val().length;
     if(tlength>maxchars)
    {
        $(this).css("background-color","#fcc").fadeOut(10).fadeIn(10).fadeOut(10).fadeIn(10);
          $(this).val($(this).val().substring(0, maxchars));

    }


    var tlength = $(this).val().length;
   var remain = maxchars - parseInt(tlength);
    $(this).prev('p').find('span').next().html(remain);




});

$("textarea").focusout(function () {
    $(this).css("background-color","#fff");
});


$("textarea").keydown(function (evt) {
if (evt.keyCode == 13) {
iname = $(this).val();
if (iname !== 'Submit'){
var fields = $('input:text,input:checkbox,select,textarea');
var index = fields.index( this );
if ( index > -1 && ( index + 1 ) < fields.length ) {
fields.eq( index + 1 ).focus();
}
return false;
}
}
});


$(document).ready(function() {

$.ajaxSetup ({  
    cache: false  
}); 

    $( ".left a" ).click(function() {

              pos = $(this).attr('id');

             $.ajax({
                 type:"POST",
    url: "",
    data: { week:pos, aq:'1' },
   context: document.body,
    success: function(s,x){
        $(this).html(s);

    }
});
        });



     $( ".day textarea" ).each(function( i ) {
         var tlength = $(this).val().length;

                    $(this).prev('p').find('span').next().html(60-tlength);

                 });


});

</script></div><div id='single'>    <div class='heading'>
    <div class='left'>
    <h3>Restauranginställningar</h3>
    </div>
     <input type='submit' class='submit' value='Spara inställningar' />
    </div>
    <div id="sidebar">
        <img src='../img/logo/falun/xxxxxxxxxx.png' />        <h3>xxxxxxxxx</h3>

    </div>

    <div id="singleholder">
   <div class="menuitem">


    <input type="text" id="adress" name="adress" value="Falugatan 3" />
    <p>Gatuadress</p>

    <input type="button" value='Visa gatuadress på karta' onclick="getAdress();" />
    <div id='map-canvas'></div>

    <script>initialize('60.606424, 15.62957');</script><p style='margin-top:3px;font-family: arial;'><span style='font-size:12px;font-style:italic;'>Stämmer inte kartmarkören? Flytta på den!</span></p>       


        <input type="hidden" name="latlong" value="" />
  </div>  

    <!-- HÄR KANSKE MAN SKULLE KUNNA GÖRA NÅGON SORTS DROPDOWN 
    <p>Lunchtider:</p>
    <input name='lunchtider' value='' size='41' />
    -->

  <div class="menuitem">

    <input type="text" name='pris' value='89' style='width:18px;' />
    <p>Lunchpris</p>
    <p>Lunchinformation <span class='remainingChars'>Återstående tecken: </span><span class='remainingChars'>70</span></p>
    <textarea id="lunchinfo" name='lunchinfo'>Inkl salladsbuffe, bröd kaffe kaka, pris 85 kr. Avhämtning 79 kr. månd-fred 11.00-14.00. Välkommen!</textarea>

    <input type='hidden' name='usersettings' value='y' />

    <p>Typ av meny</p>

    <input class='radiobtn' type='radio' name='menysort' value='1' checked /><p>Ny meny varje vecka</p><input class='radiobtn' type='radio' name='menysort' value='2'/><p>Fast veckomeny</p><input class='radiobtn' type='radio' name='menysort' value='3'/><p>Samma meny alla dagar</p>    </div>
    <input type='hidden' name='old_menu' value='1' />

    <div class="menuitem">

    <input type="text" name='telefon' value='xxxxxxxxxxxx'  />
        <p>Telefon</p>

    <input type="text" name='hemsida' value='http://www.xxxxxxxxxxxxxxxxxxxx.se' />
 <p>Hemsida</p>

    <input type="text" name='epost' value=''  />
    <p>E-postadress</p>


             <input type="checkbox" class='radiobtn' id="changepassword" name='changepassword' value='y'  />
    <p>Jag vill byta mitt lösenord</p>
    <input id="password" type='password' name='oldpass' disabled="disabled" value='' />
    <p>Gammalt lösenord</p>
    <input id="password" type='password' name='newpass1' disabled="disabled" value='' />
    <p>Nytt lösenord</p>
    <input id="password" type='password' name='newpass2' disabled="disabled" value='' />
    <p>Upprepa nytt lösenord</p>

    <input type='hidden' name='stad' value='Falun' />
    <input type='hidden' name='status' value='1' />



    </div>

    </form>

    </div>
    <script>

    $('input[type=file]').change(function(e){

        $(".upload").css("background-color", "green");
        $(".upload").css("color", "white");
        $(".upload span").text("Ny logotyp vald");
    });

    $('#changepassword').change(function(){

        $('.menuitem #password').each(function(i) {

        if($(this).prop('disabled'))
        {
            $(this).prop('disabled', false);
        }
        else
        {
            $(this).prop('disabled', true);
        }

        });

    });


         var tlength = $("#lunchinfo").val().length;
        $("#lunchinfo").prev('p').find('span').next().html(70-tlength);




    $("#lunchinfo").keyup(function () {
        var maxchars = 70;
     var tlength = $(this).val().length;
     if(tlength>maxchars)
     {
         $(this).css("background-color","#fcc").fadeOut(10).fadeIn(10).fadeOut(10).fadeIn(10);
        $(this).val($(this).val().substring(0, maxchars));
     }
    var tlength = $(this).val().length;
   var remain = maxchars - parseInt(tlength);
    $("#lunchinfo").prev('p').find('span').next().html(remain);
});

$("textarea").focusout(function () {
    $(this).css("background-color","#fff");
});

getAdress = function(){
    var adress=$("#adress").val();
    var stad= "Falun";
    initialize(adress+", "+stad);
}

</script></div>

    <div class="clear"></div>
</div>

</div>

<div id="footer">
    <p><img src="../img/mittmedialogo.png" width="140px"></p>
</div>
<script>

$( "#menu1 .selectable" ).click(function() {
            var option = $(this).html();
            window.location.href = 'index.php#'+option;
            $('#menu1 .active').removeClass('active').addClass('selectable');
            $(this).removeClass('selectable').addClass('active');
            $('.stad').html(option);
        });

</script>



    <script>
        $('input, textarea').placeholder();
    </script> <!-- /Placeholder IEfix -->


</body>
</html>

3 个答案:

答案 0 :(得分:2)

好像你没有设置 cURL 参数..

curl_setopt($curl_connection, CURLOPT_POST, 1);

答案 1 :(得分:0)

你错过了这一行

curl_setopt($curl_connection, CURLOPT_POST, 1);

TRUE定期HTTP POST。这个POST是普通的application / x-www-form-urlencoded类型,最常用于HTML表单。

这是一个简单的例子

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "http://www.example.com/yourscript.php",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'field1' => 'some date',
        'field2' => 'some other data',
    ),
);
curl_setopt_array($ch, $curlConfig)
$result = curl_exec($ch);
curl_close($ch);

// result sent by the remote server is in $result

答案 2 :(得分:0)

这将有效:

<?
$www = "http://xxxxxxxxxxxxxxxxxx.se/index.php";
//create array of data to be posted
$post_data['mandag_1'] = 'måndag 1';
$post_data['tisdag_1'] = 'tisdag 1';
$post_data['onsdag_1'] = 'onsdag 1';
$post_data['torsdag_1'] = 'torsdag 1';
$post_data['fredag_1'] = 'fredag 1';
$post_data['week'] = '4';
$post_data['food'] = 'y';
$post_data['usersettings'] = 'y';
$post_data['status'] = '1';
$post_data['adress'] = 'Falugatan';
$post_data['stad'] = 'Falun';
$post_data['old_menu'] = '1';
$post_data['menysort'] = '1';
$post_data['telefon'] = '023';
$post_data['hemsida'] = '';
$post_data['epost'] = '';
$post_data['changepassword'] = '';
$post_data['oldpass'] = '';
$post_data['newpass1'] = '';
$post_data['newpass2'] = '';
$post_data['lunchinfo'] = '';

//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//create cURL connection
$curl_connection = 
  curl_init('http://xxxxxxxxxxxxxx.nu/login/index.php');

//set options
curl_setopt($curl_connection, CURLOPT_POST, 1); 
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl_connection, CURLOPT_USERAGENT, 
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);

//set data to be posted
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);

//perform our request
$result = curl_exec($curl_connection);

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . 
                curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
echo '<br>TTT'.$post_string;
?>