爱站屋博客 - 好东西不私藏,大家一起分享!关于博客 留言板

网站首页 技术教程 正文

php发送get、post请求的6种方法简明总结

星云 2018-12-05 技术教程 401 ℃
php发送get、post请[db:标签]的以下6种方法

方法1:

用file_get_contents 以get方式获取容:
  1. <?php
  2. $url='http://lanyou.vip/';
  3. $html = file_get_contents($url);
  4. echo $html;
  5. ?>

方法2:

用fopen打开url, 以get方式获取内容:
  1. <?php
  2. $fp = fopen($url, r');
  3. stream_get_meta_data($fp);
  4. while(!feof($fp)) {
  5. $result .= fgets($fp, 1024);
  6. }
  7. echo “url body: $result”;
  8. fclose($fp);
  9. ?>

方法3:

用file_get_contents函数,以post方式获取url
  1. <?php
  2. $data = array (‘foo' => ‘bar');
  3. $data = http_build_query($data);
  4. $opts = array (
  5. http' => array (
  6. ‘method' => POST',
  7. ‘header'=> Content-type: application/x-www-form-urlencodedrn .
  8. Content-Length: . strlen($data) . rn”,
  9. content' => $data
  10. )
  11. );
  12. $context = stream_context_create($opts);
  13. $html = file_get_contents(‘http://localhost/e/admin/test.html', false, $context);
  14. echo $html;
  15. ?>

方法4:

用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启
  1. <?php
  2. function get_url ($url,$cookie=false)
  3. {
  4. $url = parse_url($url);
  5. $query = $url[path].”?”.$url[query];
  6. echo Query:”.$query;
  7. $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
  8. if (!$fp) {
  9. return false;
  10. } else {
  11. $request = GET $query HTTP/1.1rn”;
  12. $request .= Host: $url[host]rn”;
  13. $request .= Connection: Closern”;
  14. if($cookie) $request.=”Cookie: $cookien”;
  15. $request.=”rn”;
  16. fwrite($fp,$request);
  17. while(!@feof($fp)) {
  18. $result .= @fgets($fp, 1024);
  19. }
  20. fclose($fp);
  21. return $result;
  22. }
  23. }
  24. //获取url的html部分,去掉header
  25. function GetUrlHTML($url,$cookie=false)
  26. {
  27. $rowdata = get_url($url,$cookie);
  28. if($rowdata)
  29. {
  30. $body= stristr($rowdata,”rnrn”);
  31. $body=substr($body,4,strlen($body));
  32. return $body;
  33. }
  34. return false;
  35. }
  36. ?>

方法5:

用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body
  1. <?php
  2. function HTTP_Post($URL,$data,$cookie, $referrer=”")
  3. {
  4. // parsing the given URL
  5. $URL_Info=parse_url($URL);
  6.  
  7. // Building referrer
  8. if($referrer==”") // if not given use this script as referrer
  9. $referrer=”111″;
  10. // making string from $data
  11. foreach($data as $key=>$value)
  12. $values[]=”$key=”.urlencode($value);
  13. $data_string=implode(“&”,$values);
  14. // Find out which port is needed – if not given use standard (=80)
  15. if(!isset($URL_Info["port"]))
  16. $URL_Info["port"]=80;
  17. // building POST-request:
  18. $request.=”POST “.$URL_Info["path"].” HTTP/1.1n”;
  19. $request.=”Host: “.$URL_Info["host"].”n”;
  20. $request.=”Referer: $referern”;
  21. $request.=”Content-type: application/x-www-form-urlencodedn”;
  22. $request.=”Content-length: “.strlen($data_string).”n”;
  23. $request.=”Connection: closen”;
  24. $request.=”Cookie: $cookien”;
  25. $request.=”n”;
  26. $request.=$data_string.”n”;
  27. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  28. fputs($fp, $request);
  29. while(!feof($fp)) {
  30. $result .= fgets($fp, 1024);
  31. }
  32. fclose($fp);
  33. return $result;
  34. }
  35. ?>

方法6:

使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
  1. <?php
  2. $ch = curl_init();
  3. $timeout = 5;
  4. curl_setopt ($ch, CURLOPT_URL, http://www.lanyou.vip/');
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  7. $file_contents = curl_exec($ch);
  8. curl_close($ch);
  9. echo $file_contents;
  10. ?>



猜你喜欢

本文暂时没有评论哦(●'◡'●)

欢迎 发表评论:

:如果评论未出现,请刷新网页,谢谢合作!

会员中心
搜索
ip签名图
    IP签名图
网站分类
标签列表
文章归档
站点信息
  • 文章总数:80720
  • 页面总数:3
  • 分类总数:12
  • 标签总数:629
  • 评论总数:1899
  • 浏览总数:968187
爱站云安全认证