duanji2002 2015-01-12 06:32
浏览 132
已采纳

使用PHP Curl提交表单并获得结果[重复]

This question already has an answer here:

The form on the website is...

<form action="" method="POST">
<input style="width:30%;background-color:#e2e2e2;border:#000;color:#000;" type="text" name="userName" placeholder="Enter a username" required="">
<br>
<input type="submit" name="userBtn" value="get Username">
</form>

Once you fill out a value in name="userName" and click name="userBtn" the page refreshes and changes the value of name="userName" to the information that I want.

How would I go about submitting a form and then retrieving the data that it writes?

</div>
  • 写回答

1条回答 默认 最新

  • dsdukbc60905239 2015-01-12 06:51
    关注

    See this detailed example, Begin by creating a new connection.

    $curl_connection = 
      curl_init('http://www.domainname.com/target_url.php');
    

    A new connection is created using curl_init() function, which takes the target URL as parameter (The URL where we want to post our data). The target URL is same as the “action” parameters of a normal form, which would look like this:

    <form method="post" action="http://www.domainname.com/target_url.php">
    

    Now let’s set some options for our connection. We can do this using the curl_setopt() function. Go to curl_setopt() reference page for more information on curl_setopt() and a complete list of options.

    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);
    

    What options do we set here?

    First, we set the connection timeout to 30 seconds, so we don’t have our script waiting indefinitely if the remote server fails to respond.

    Then we set how cURL will identify itself to the remote server. Some servers will return different content for different browsers (or agents, such as spiders of the search engines), so we want our request to look like it is coming from a popular browser.

    CURLOPT_RETURNTRANSFER set to true forces cURL not to display the output of the request, but return it as a string.

    Then we set CURLOPT_SSL_VERIFYPEER option to false, so the request will not trigger an error in case of an invalid, expired or not signed SSL certificate.

    Finally, we set CURLOPT_FOLLOWLOCATION to 1 to instruct cURL to follow “Location: ” redirects found in the headers sent by the remote site.

    Now we must prepare the data that we want to post. We can first store this in an array, with the key of an element being the same as the input name of a regular form, and the value being the value that we want to post for that field.

    For example,if in a regular form we would have:

    <input type="text" name="firstName" value="Name">
    <input type="hidden" name="action" value="Register">
    

    we add this to our array like this:

    $post_data['firstName'] = 'Name';
    $post_data['action'] = 'Register'
    

    Do the same for every form field.

    Data will be posted in the following format: key1=value1&key2=value2

    In order to format the data like this, we are going to create strings for each key-value pair (for example key1=value1), put them in another array ($post_items) then combine them in one string using PHP function implode() .

      foreach ( $post_data as $key => $value) 
        {
            $post_items[] = $key . '=' . $value;
        }
    
    $post_string = implode ('&', $post_items);
    

    Next, we need to tell cURL which string we want to post. For this, we use the CURLOPT_POSTFIELDS option.

    curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); Finally, we execute the post, then close the connection.

    $result = curl_exec($curl_connection);
    curl_close($curl_connection);
    

    By now, the data should have been posted to the remote URL. Go check this, and if it did not work properly, use curl_getinfo() function to see any errors that might have occurred.

    print_r(curl_getinfo($curl_connection));
    

    This line displays an array of information regarding the transfer. This must be used before closing the connection with curl_close();

    You can also see number and description of the error by outputting curl_errno($curl_connection) and curl_error($curl_connection).

    So let’s put everything together. Here is our code:

    <?php
    
    //create array of data to be posted
    $post_data['firstName'] = 'Name';
    $post_data['action'] = 'Register';
    
    //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://www.domainname.com/target_url.php');
    
    //set options
    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);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?