您的位置 首页 php

php访问url的四种方式

10.jpg

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

推荐学习:PHP视频教程

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

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

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 时候将获取数据返回        $r = curl_exec($ch);      curl_close($ch);      return $r;  }

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

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);     if (!$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访问url的四种方式的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:php访问url的四种方式

文章地址:https://www.zhihuclub.com/3038.shtml

关于作者: 智云科技

热门文章

评论已关闭

8条评论

  1. I’m really enjoying the theme/design of your website.
    Do you ever run into any browser compatibility issues? A handful of my blog visitors have complained about my site not working correctly
    in Explorer but looks great in Chrome. Do you have any tips to help fix this issue?

  2. I like what you guys are usually up too. This type of clever work and coverage!
    Keep up the fantastic works guys I’ve incorporated
    you guys to my personal blogroll.

  3. Hi there! I know this is kinda off topic nevertheless I’d figured I’d ask.
    Would you be interested in trading links or maybe guest writing a blog post or vice-versa?
    My website covers a lot of the same topics as yours and
    I feel we could greatly benefit from each other. If you’re
    interested feel free to shoot me an e-mail.

    I look forward to hearing from you! Superb blog by the way!

  4. Greetings! Very helpful advice in this particular post!
    It’s the little changes which will make the largest changes.

    Thanks for sharing!

  5. At worst, they could end up with a product containing harmful ingredients, as has happened with many of the fake versions of Viagra that have been sold around the world

  6. Please let me know if you’re looking for a article author
    for your blog. You have some really good posts and I believe I would be a good
    asset. If you ever want to take some of the load off, I’d absolutely love
    to write some material for your blog in exchange for a link back to mine.
    Please blast me an email if interested. Thank you!

  7. I don’t know whether it’s just me or if perhaps everybody else experiencing problems with your site.
    It appears like some of the written text in your content are running off
    the screen. Can someone else please comment and let me know if this is happening to them too?
    This might be a issue with my browser because I’ve had this
    happen before. Kudos

网站地图