首页 > 编程技术 > php

file_get_contents实现数据Post数据方法

发布时间:2016-11-25 15:51

file_get_contents() 函数把整个文件读入一个字符串中。

和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。

file_get_contents() 函数是用于将文件的内容读入到一个字符串中的首选方法。如果操作系统支持,还会使用内存映射技术来增强性能。

语法
file_get_contents(path,include_path,context,start,max_length)参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context 可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。
 
start 可选。规定在文件中开始读取的位置。该参数是 php教程 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 php 5.1 新加的。


php代码

<?php 
function post($url, $post = null) 

    $context = array(); 
 
    if (is_array($post)) 
    { 
        ksort($post); 
 
        $context['http'] = array 
        ( 
            'method' => 'post', 
            'content' => http_build_query($post, '', '&'), 
        ); 
    } 
 
    return file_get_contents($url, false, stream_context_create($context)); 

 
$data = array 

    'name' => 'test', 
    'email' => 'test@gmail.com', 
    'submit' => 'submit', 
); 
 
echo post('http://localhost/5-5/request_post_result.php', $data); 
?>  
接收数据:

request_post_result.php  接收经过post的数据:
php代码
<?php 
echo $_post['name']; 
echo $_post['email']; 
echo $_post['submit']; 
echo "fdfd"; 
?>  


实例二

/**
* 其它版本
* 使用方法:
* $post_string = "app=request&version=beta";
* request_by_other('http://facebook.cn/restserver.php',$post_string);
*/
function request_by_other($remote_server,$post_string){
    $context = array(
        'http'=>array(
            'method'=>'post',
            'header'=>'content-type: application/x-www-form-urlencoded'."rn".
                      'user-agent : jimmy's post example beta'."rn".
                      'content-length: '.strlen($post_string)+8,
            'content'=>'mypost='.$post_string)
        );
    $stream_context = stream_context_create($context);
    $data = file_get_contents($remote_server,false,$stream_context);
    return $data;
}

由于checkbox特殊性所以我们要获取它的值,在很我语言中都不一样的,在php教程 以我们是以数组形式来处理,并且利用遍历他的值,下面有大量实例。

<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h1>checkbox demo</h1>

<h3>demonstrates checkboxes</h3>
<form action ="handleformcheckbox.php">

<ul>
  <li><input type ="checkbox" name ="chkfries" value ="11.00">fries</li>
  <li><input type ="checkbox" name ="chksoda"  value ="12.85">soda</li>
  <li><input type ="checkbox" name ="chkshake" value ="1.30">shake</li>
  <li><input type ="checkbox" name ="chkketchup" value =".05">ketchup</li>
</ul>
<input type ="submit">
</form>

</body>
</html>


<!-- handleformcheckbox.php
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h3>demonstrates reading checkboxes</h3>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>

here;

$total = 0;

if (!empty($chkfries)){
  print ("you chose fries <br>");
  $total = $total + $chkfries;
}

if (!empty($chksoda)){
  print ("you chose soda <br>");
  $total = $total + $chksoda;
}

if (!empty($chkshake)){
  print ("you chose shake <br>");
  $total = $total + $chkshake;
}

if (!empty($chkketchup)){
  print ("you chose ketchup <br>");
  $total = $total + $chkketchup;
}

print "the total cost is $$total";

?>
</body>
</html>


-->

实例

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server['php_self']);
if (!empty($food)) {
    echo "the foods selected are:<br />";
    foreach($food as $foodstuf)
    {
        echo "<strong>".htmlentities($foodstuf)."</strong><br />";
    }
}
else
{
    echo ("<form action="$self" ");
    echo ('method="get">
    <fieldset>
        <label>italian <input type="checkbox" name="food[]" value="italian" />
</label>
        <label>mexican <input type="checkbox" name="food[]" value="mexican" />
</label>
        <label>chinese <input type="checkbox" name="food[]" value="chinese"
        checked="checked" /></label>
    </fieldset>
    <input type="submit" value="go!" >');
}
?>
</body>
</html>


多选checkbox

<?php
$options = array('option 1', 'option 2', 'option 3');

$valid = true;
if (is_array($_get['input'])) {
    $valid = true;
    foreach($_get['input'] as $input) {
        if (!in_array($input, $options)) {
            $valid = false;
        }
    }
    if ($valid) {
        //process input
    }
}
?>

实例checkbox多值获取

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get["food"];//www.3ppt.com
if (!empty($food)){
    echo "the foods selected are: <strong>";
    foreach($food as $foodstuff){
        echo '<br />'.htmlentities($foodstuff);
    }
    echo "</strong>.";
}
else {
    echo ('
    <form action="'. htmlentities($_server["php_self"]).'" method="get">
        <fieldset>
            <label>
                italian
                <input type="checkbox" name="food[]" value="italian" />
            </label>
            <label>
                mexican
                <input type="checkbox" name="food[]" value="mexican" />
            </label>
            <label>
                chinese
                <input type="checkbox" name="food[]" value="chinese" checked="checked" />
            </label>
        </fieldset>
        <input type="submit" value="go!" />
    </form> ');
    }
?>
</body>
</html>

数组的排序
在php教程中,排序方式有三种,通过索引排序、通过值排序(不保留原索引)、通过值排序(保留原索引)。每种又分为升序、降序以及用户定义顺序三个函数。它们分别如下:
通过索引排序:①升序 ksort() ②降序 krsort() ③用户定义顺序 uksort()
不保留原索引值排序:①升序 sort() ②降序 rsort() ③用户定义顺序 usort()
保留原索引值排序:①升序 asort() ②降序 arsort() ③用户定义顺序 uasort()
在php中,也可以用array_multisort来一次排序多个数组,不过项目中可能用得比较少。
翻转数组,把数字索引翻转,索引重新从0开始:array_reverse()
把索引和值调换:array_flip()
随机顺序:shuffle()
迭代器遍历

 

$test01 = array('a', 'b', 'c');
// for
for ($i = 0; $i < count($test01); $i++) {
    echo $test01[$i];
}
// foreach value only
foreach ($test01 as $value) {
    echo $value;
}
// foreach key and value
$test01 = array('a' => 'aaaa', 'b' => 'bbbb', 'c' => 'cccc');
foreach ($test01 as $key => $value) {
    echo "$key => $value";
}


$test01 = array('a' => 'aaaa', 'b' => 'bbbb', 'c' => 'cccc');
while (list($key, $value) = each($test01)) {
    echo "$key => $value" . "<br/>";
}

php中,迭代遍历主要要用到以下函数。
current() 迭代的当前元素。
reset() 重新移动到第一个元素并返回它。
next() 移动到下一个元素并返回它。
prev() 移动到上一个元素并返回它。
end() 移动到最后一个元素并返回它。
each() 以数组的形式返回当前元素的索引和值,并移动到下一个迭代。
key() 返回当前的索引。
array_ walk() 为每一个元素调用函数。
array_ reduce() 为每一个元素依次计算。

变通遍历
 

array_walk($test01, walk_test);
function walk_test($key, $value) {
    echo "walk: $key => $value" . "<br/>";
}
 
$test02 = array(1, 2, 3, 4, 5);
echo array_reduce($test02, reduce_test);
function reduce_test($run_result, $current_value) {
    return $run_result + $current_value * $current_value;
}

 

正则表达式的匹配规则修改如下:
([.n]*),当然,如果是在java程序中直接写到话,需要改为([.n]*)
结果再次运行程序,发现什么内容也取不到了。我百思不得其解,又将其修改为如下规则:
([.|n]*) 以及 ([n.]*)
结果还是不行,什么内容都取不到。看来点符号和换行符卯上劲了~
然后上网一查,虽然没有查出上述规则到底是什么地方出问题了,但是查出了一个解决办法,经过一试,果然可以匹配包括换行符在内的任意字符,以下为正确的正则表达式匹配规则:
([ss]*)
同时,也可以用 “([dd]*)”、“([ww]*)” 来表示。


在文本文件里, 这个表达式可以匹配所有的英文
/[ -~]/
这个表达式可以匹配所有的非英文(比如中文)
/[^ -~]/
/是vi里用的. 你在editplus或程序里不需要/

 

<form action="pcre.php教程" name="form1" method="post" >
输入将要匹配的字符串:<input type="text" name="str"  id="str" /><br />
<input type="submit" name="submit1" id="submit1" value="提交" />
</form>
<?php
$pattern="/d/";
if(preg_match($pattern, $str,$output)){
echo "匹配字符成功".$output;
}
else echo "匹配字符失败";
?>
</body>

php教程 $ _post函数 与$_get函数详解

1、get是从服务器上获取数据,post则是向服务器传送数据;

    2、get将表单中数据的按照variable=value的 形式,添加到action所指向的url后面,并且两者使用“?”连接,而各个变量之间使用“&”连接。post是将表单中的数据放在form的 数据体中,按照变量和值相对应的方式,传递到action所指向url;fashion jewelry wholesale

    所以从上述也可以得到另外两个结论:

       1、get传送的数据量较小,不能大于2kb,这主要是因为受url长度限制。post传送的数据量较大,所以在上传文件只能使用post。一般被默认为不受限制。但理论上,iis4中最大量为80kb,iis5中为100kb;

       2、get安全性非常低(因为用户可以通过url看到),post安全性较高(其所有操作对用户来说都是不可见的)。但是执行效率却比post方法好;

    3、get限制form表单的数据集的值必须为ascii字符;而post支持整个iso10646字符集;cleaning cloth

    4、get是form的默认方法;

    5、get方式提交来的数据在服务器端用request.querystring()来获取,用post方式提交的数据用request.form()来获取(这一点不是很明确);

建议:

    1、如果是包含机密信息的话,建议用post数据提交方式;

    2、在做数据查询时,建议用get方式;而在做数据添加、修改或删除时,建议用post方式;


get实例

内置的$ _get函数是用来收集从表单发送一个method="get"的值。信息是一个用get方法的形式发送的.用户可见的(它会在浏览器的地址栏显示),并已对信息发送数量限制。
举个例子 :
<form action="test.php" method="get">
    name: <input type="text" name="username" />
    age: <input type="text" name="age" />
    <input type="submit" value="submit" />
</form>
当用户点击“提交”按钮,发送到服务器的url可能看起来像这样:
http: //localhost/demo/test.php?username=lily&age=28

在"test.php"文件,现在可以使用$ _get这个函数来收集表单数据(表单字段的名称将自动在$ _get数组的键):
welcome <?php echo $_get["username"]; ?><br />
you are <?php echo $_get["age"]; ?> years old!

那何时使用 method="get" 呢?
当使用 method="get" 在html表单,所有的变量名和值显示在url中。注意:这个方法不应该被使用在密码或其他敏感信息!但是,由于变量在显示网址,是有可能的书签页面。这可以在某些情况下有用。get方法是不适合非常大的变量值。它不应该使用超过2000个字符的值


$_post实例

内置在$ _post功能是用来收集在一个表单 method="post" 值。使用post方法的形式发送的信息用户是看不见的,并已对信息发送量没有限制。但是,对于post方法有一个8 mb max size,默认情况下(可通过设置在php.ini文件中的post_max_size更改)。
举个例子 :
<form action="welcome.php" method="post">
    name: <input type="text" name="username" />
    age: <input type="text" name="age" />
    <input type="submit" value="submit" />
</form>

当用户点击“提交”按钮,发送到服务器的url可能看起来像这样:
http: //localhost/demo/test.php

在"test.php"文件,现在可以使用了$ _post功能,收集表单数据(表单字段的名称将自动在$ _post数组的键):
welcome <?php echo $_post["username"]; ?>!<br />
you are <?php echo $_post["age"]; ?> years old.


那何时使用 method="post" 呢?
       使用post方法的形式发送的信息用户是看不见的,并已对信息发送量没有限制。但是,由于变量没有在url中显示,这是不可能的书签的页面。

php的$ _request的作用
        php的内置函数包含在$ _request中的有两种形式 $ _get,$ _post和$ _cookie 。$ _request函数,可用于收集get和post方法发送表单数据。
welcome <?php echo $_request["username"]; ?>!<br />
you are <?php echo $_request["age"]; ?> years old.

标签:[!--infotagslink--]

您可能感兴趣的文章: