当前位置:Gxlcms > PHP教程 > php中请求url有哪些方法

php中请求url有哪些方法

时间:2021-07-01 10:21:17 帮助过:1人阅读

PHP中打开URL地址的几种方法总结,这里的函数主要用于小偷采集等函数。

1: 用file_get_contents

以get方式获取内容

  1. <?php
  2. $url='www.baidu.com/';
  3. $html = file_get_contents($url);
  4. //print_r($http_response_header);
  5. ec($html);
  6. printhr();
  7. printarr($http_response_header);
  8. printhr();
  9. ?>

示例代码2: 用fopen打开url,

以get方式获取内容

  1. <?
  2. $fp = fopen($url, 'r');
  3. printarr(stream_get_meta_data($fp));
  4. printhr();
  5. while(!feof($fp)) {
  6. $result .= fgets($fp, 1024);
  7. }
  8. echo "url body: $result";
  9. printhr();
  10. fclose($fp);
  11. ?>

示例代码3:用file_get_contents函数,以post方式获取url

  1. <?php
  2. $data = array ('foo' =>
  3. 'bar');
  4. $data = http_build_query($data);
  5. $opts = array (
  6. 'http'
  7. => array (
  8. 'method' => 'POST',
  9. 'header'=> "Content-type:
  10. application/x-www-form-urlencoded" .
  11. "Content-Length: " . strlen($data) .
  12. "",
  13. 'content' => $data
  14. ),
  15. );
  16. $context =
  17. stream_context_create($opts);
  18. $html =
  19. file_get_contents('localhost/e/admin/test.html', false, $context);
  20. echo $html;
  21. ?>

示例代码4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body

  1. <?
  2. function get_url
  3. ($url,$cookie=false) {
  4. $url = parse_url($url);
  5. $query =
  6. $url[path]."?".$url[query];
  7. ec("Query:".$query);
  8. $fp = fsockopen(
  9. $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
  10. if (!$fp) {
  11. return false;
  12. } else {
  13. $request = "GET $query HTTP/1.1";
  14. $request .= "Host: $url[host]";
  15. $request .= "Connection: Close";
  16. if($cookie) $request.="Cookie: $cookie\n";
  17. $request.="";
  18. fwrite($fp,$request);
  19. while(!@feof($fp)) {
  20. $result .= @fgets($fp,
  21. 1024);
  22. }
  23. fclose($fp);
  24. return $result;
  25. }
  26. }
  27. //获取url的html部分,去掉header
  28. function GetUrlHTML($url,$cookie=false) {
  29. $rowdata = get_url($url,$cookie);
  30. if($rowdata)
  31. {
  32. $body=
  33. stristr($rowdata,"");
  34. $body=substr($body,4,strlen($body));
  35. return $body;
  36. }
  37. return false;
  38. }
  39. ?>

最近开发中遇到一个问题,程序第4行会请求一个url,通过查找相关的资料发现有多种方法,本文给大家介绍了关于php中请求url的五种方法,分别是用fopen()函数、file()函数、file_get_contents()函数、curl() 请求远程url数据和exec() 执行命令行命令

本文主要给大家介绍了关于php中请求url的五种方法,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍:

五种方法:

  • 前三种都是php基本的文件操作函数

  • curl()是php扩展需要开启,linux下需要安装

  • exec()执行的是linux命令行下的命令wget下载远程文件

其中wget命令在本地虚机测试请求www.baidu.com时,没有成功,在远程服务器上却可以,考虑时DNS解析的问题,于是直接请求IP成功下载了index.html的文件。

这里只提供了方法,其中的优缺点需要详细了解每一个方法的功能和缺陷。

一、fopen()函数

  1. $file = fopen("www.gxlcms.com", "r") or die("打开远程文件失败!");
  2. while (!feof($file)) {
  3. $line = fgets($file, 1024);
  4. //使用正则匹配标题标记
  5. if (preg_match("/<title>(.*)<\/title>/i", $line, $out)) {
  6. $title = $out[1]; //将标题标记中的标题字符取出
  7. break; //退出循环,结束远程文件读取
  8. }
  9. }
  10. fclose($file);

二、file()函数


  1. $lines = file("www.gxlcms.com/article/48866.htm");
  2. readfile(www.gxlcms.com/article/48866.htm);

三、file_get_contents()函数


  1. $content = file_get_contents(www.gxlcms.com/article/48866.htm);

四、curl() 请求远程url数据


  1. $url = "www.baidu.com";
  2. $ch = curl_init();
  3. $timeout = 5;
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  7. $contents = curl_exec($ch);
  8. curl_close($ch);

五、exec() 执行命令行命令

  1. //exec("wget 220.181.111.188");
  2. shell_exec("wget 220.181.111.188");

以上就是php中请求url有哪些方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行