PHP使用file_get_contents 以get方式获取内容
<?php $url='//blog.csdn.net/qq_37428797'; $html = file_get_contents($url); echo $html; ?>
PHP使用fopen打开url, 以get方式获取内容
<?php $fp = fopen($url, ‘r'); stream_get_meta_data($fp); while(!feof($fp)) { $result .= fgets($fp, 1024); } echo “url body: $result”; fclose($fp); ?>
PHP使用file_get_contents函数,以post方式获取url
<?php $data = array (‘foo' => ‘bar'); $data = http_build_query($data); $opts = array ( ‘http' => array ( ‘method' => ‘POST', ‘header'=> “Content-type: application/x-www-form-urlencodedrn” . “Content-Length: ” . strlen($data) . “rn”, ‘content' => $data ) ); $context = stream_context_create($opts); $html = file_get_contents(‘http://localhost/e/admin/test.html', false, $context); echo $html; ?>
PHP使用curl库获取
<?php $ch = curl_init(); $timeout = 5; curl_setopt ($ch, CURLOPT_URL, ‘//blog.csdn.net/qq_37428797'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $file_contents = curl_exec($ch); curl_close($ch); echo $file_contents; ?>
PHP发送get、post请求的4种常用方法
最新推荐文章于 2024-02-19 16:56:34 发布