zoukankan      html  css  js  c++  java
  • 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");  
    2. //或者是:
      $str = file("http://bbs.lampbrother.net");
      //或者是:
      readfile("http://bbs.lampbrother.net");

    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 /> ";     
    4. else {     
    5.     $out="GET / HTTP/1.1 ";     
    6.     $out.="Host: www.example.com ";     
    7.     $out.="Connection: Close ";     
    8.     fwrite($fp, $out);     
    9.     while (!feof($fp)) {     
    10.         echo fgets($fp, 128);     
    11.     }  
    12.     fclose($fp);     
    13. }   
     
     
  • 相关阅读:
    线性DP
    2020年第十一届蓝桥杯第二场C/C++ B组省赛题解
    筛质数 + 质因子分解
    快速幂
    DP 背包问题
    CF510B Fox And Two Dots
    怎样看人生的价值和意义!--找回迷失的自己
    Ionic+AngularJS 开发的页面在微信公众号下显示不出来原因查究
    AngularJS directive 指令相关记录
    AngularJS的一点学习笔记
  • 原文地址:https://www.cnblogs.com/hfdp/p/5673332.html
Copyright © 2011-2022 走看看