PHP进行POST(GET)并获取返回值

2018年4月8日 6401点热度 4人点赞 0条评论

其实这件事情用Python做挺好,但某些情况下用PHP更方便(而且我感觉用到数据库的时候两者速度相差很大),更直观。问:如果想用简单的web方式来实现,一个html from表单就解决问题吗?当然是“一个”表单解决不了啦(没有教大家做坏事的意思),不多说废话,直接上代码好了。

function send($url, $post_data) {
    $postdata = http_build_query($post_data);//格式化数据
    $options = array(
        'http' => array(
            'method' => 'POST',//发送方式,post或get
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 60 // 超时时间(单位:s)
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}

调用方式:

$post_data = array(
    'username' => 'admin',
    'password' => 'admin'
);
$res=send_post($posturl, $post_data);

 

jlqwer

这个人很懒,什么都没留下

文章评论