您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP如何访问URL?php访问URL的方法总结(代码)

2024/6/12 11:42:48发布41次查看
这篇文章给大家介绍的内容是关于php如何访问url?php访问url的方法总结(代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
1、fopen方式
//访问指定url函数function access_url($url) { if($url=='') return false; $fp = fopen($url, 'r') or exit('open url faild!'); if($fp){ while(!feof($fp)) { $file .= fgets($fp) . ""; } fclose($fp); } return $file; }
2、file_get_contents方式 (打开远程文件的时候会造成cpu飙升。file_get_contents其实也可以post)
//以post方式获取url$data = array ('foo' => 'bar');$data = http_build_query($data);$opts['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/test.html', false, $context);echo $html;
3、curl方式
function curl_file_get_contents($durl){ $ch = curl_init(); curl_setopt($ch, curlopt_url, $durl); curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回 curl_setopt($ch, curlopt_binarytransfer, true) ; // 在启用 curlopt_returntransfer 时候将获取数据返回 $data = curl_exec($ch); curl_close($ch); return $data; }
4、fsockopen方式(只能获取网站主页信息,其他页面不可以)
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); (!$fp) { echo "$errstr ($errno)<br />\n"; }else { $out="get / http/1.1\r\n"; $out.="host: www.example.com\r\n"; $out.="connection: close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); }
相关文章推荐:
php使用phpmailer如何发送邮件(附代码)
ubuntu系统下如何来隐藏tp的入口文件
以上就是php如何访问url?php访问url的方法总结(代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product