已掉线,重新登录

首页 > 绿虎论坛 > 历史版块 > 编程 > PHP > 教程

标题: file_get_contents实现数据Post发送

作者: @Ta

时间: 2011-09-22

点击: 2691

方法一:
<?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));
 }
?>
方法二:
<?php
$post_string = "app=request&version=beta";
function post($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;
}
?>

[隐藏样式|查看源码]


『回复列表(3|隐藏机器人聊天)』

1. 用得着这么麻烦?html直接发送就行!
(/@Ta/2011-09-22 21:03//)

2. 无语地说:你不懂
(/@Ta/2011-09-23 09:00//)

3. 超时$comtext[http][timeout]=30
(/@Ta/2011-10-24 13:57//)

回复需要登录

9月13日 12:50 星期六

本站由hu60wap6驱动

备案号: 京ICP备18041936号-1