PHP逐行读取文件然后POST到其他页面

大胆的番茄 2014-06-12 11:56:22
要求类似标题
文件格式如下:

guuk 武装部
guwv 一部分
guww 残疾人
guxa 开门红
guyk 下意识
guyw 下半夜
gvav 一刀切

要求a.php实现读取和post到result.php页面,result.php有运行时间,最好能有提示已经运行完成a.php再读取第二行内容
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
傲雪星枫 2014-06-13
  • 打赏
  • 举报
回复
运行时间: Array ( [0] => Array ( [row] => guuk 武装部 [posttime] => 2014-06-12 17:27:08 ) [1] => Array ( [row] => guwv 一部分 [posttime] => 2014-06-12 17:27:08 ) [2] => Array ( [row] => guww 残疾人 [posttime] => 2014-06-12 17:27:08 ) [3] => Array ( [row] => guxa 开门红 [posttime] => 2014-06-12 17:27:08 ) [4] => Array ( [row] => guyk 下意识 [posttime] => 2014-06-12 17:27:08 ) [5] => Array ( [row] => guyw 下半夜 [posttime] => 2014-06-12 17:27:08 ) [6] => Array ( [row] => gvav 一刀切 [posttime] => 2014-06-12 17:27:08 ) )
傲雪星枫 2014-06-13
  • 打赏
  • 举报
回复
file.txt

guuk 武装部
guwv 一部分
guww 残疾人
guxa 开门红
guyk 下意识
guyw 下半夜
gvav 一刀切
a.php

$file = file_get_contents('file.txt');
$content = explode("\n", $file);
$url = 'http://localhost/result.php'; // 这里需用http

$result = array();

echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">';

foreach($content as $row){
    $t = postdata($url, $row);
    $result[] = array('row'=>$row, 'posttime'=>$t);
    // 提示
    if($t){
        echo 'post data success<br>';
    }else{
        echo 'post data error';
    }
}

print_r($result);

function postdata($url, $data){

    $pdata = array(
        'text' => $data        
    );

    $opts = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'content-type:application/x-www-form-urlencoded',
            'content' => http_build_query($pdata)
        )    
    );

    $context = stream_context_create($opts);

    $result = file_get_contents($url, false, $context);

    return $result;
}
result.php

$row = isset($_POST['text'])? $_POST['text'] : '';

if($row){
    echo date('Y-m-d H:i:s');
}else{
    echo 'error';
}

21,890

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧