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);