file_get_contents()实现get+post请求

先看file_get_contents()的定义:

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = 0 [, int $maxlen ]]]] )

其中$filename可以传文件的相对/绝对地址,也可以传url。

简单Get请求:

$ret = file_get_contents("http://www.baidu.com");

复杂Get+Post请求:

$url = '***';
$data = ['username' => '***','password' => '***'];
$data = json_encode($data);
$opts = [
                 'http' => [
                                    'method' => 'POST',
                                    'header'  => 'Content-type:application/json',
                                    'content' => $data,
                           ]
        ];
$context = stream_context_create($opts);
$ret = file_get_contents($url,false,$context);

 

posted @ 2017-03-18 15:02  purelightme  阅读(549)  评论(0编辑  收藏  举报