php访问url的四种方式

php访问url的四种方式

1.fopen方式
//访问指定URL函数

  1. function access_url($url) {    
  2.     if ($url=='') return false;    
  3.     $fp = fopen($url, 'r') or exit('Open url faild!');    
  4.     if($fp){  
  5.     while(!feof($fp)) {    
  6.         $file.=fgets($fp)."";  
  7.     }  
  8.     fclose($fp);    
  9.     }  
  10.     return $file;  
  11. }  


2.file_get_contents方式(打开远程文件的时候会造成CPU飙升。file_get_contents其实也可以post)

  1. $content = file_get_contents("http://www.google.com");  


3.curl方式

  1. function curl_file_get_contents($durl){  
  2.     $ch = curl_init();  
  3.     curl_setopt($ch, CURLOPT_URL, $durl);  
  4.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回    
  5.     curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回    
  6.     $r = curl_exec($ch);  
  7.     curl_close($ch);  
  8.     return $r;  
  9. }  


4.fsockopen方式(只能获取网站主页信息,其他页面不可以)

    1. $fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);     
    2. if (!$fp) {     
    3.     echo "$errstr ($errno)<br />\n";     
    4. else {     
    5.     $out="GET / HTTP/1.1\r\n";     
    6.     $out.="Host: www.example.com\r\n";     
    7.     $out.="Connection: Close\r\n\r\n";     
    8.     fwrite($fp, $out);     
    9.     while (!feof($fp)) {     
    10.         echo fgets($fp, 128);     
    11.     }  
    12.     fclose($fp);     
    13. }  

转载于:https://www.cnblogs.com/shsgl/p/5494675.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值