php发送get、post请求的6种方法代码示例

本文主要展示了php发送get、post请求的6种方法的代码示例,分别为使用file_get_contents 、fopen、fsockopen、curl来发送GET和POST请求,代码如下:

方法1: 用file_get_contents 以get方式获取内容:

1 <?php
2     $url=‘http://www.jb51.net/‘;
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
 5     $opts = array (
 6         ‘http‘ => array (
 7             ‘method‘ => ‘POST‘,
 8             ‘header‘=> “Content-type: application/x-www-form-urlencodedrn” . “Content-Length: ” . strlen($data) . “rn”,
10             ‘content‘ => $data
11         )
12     );
13
14     $context = stream_context_create($opts);
15     $html = file_get_contents(‘http://localhost/e/admin/test.html‘, false, $context);
16
17     echo $html;
18 ?>

方法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([email protected]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
35         return false;
36     }
37 ?>

方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

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

方法6:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

 1 <?php
 2     $ch = curl_init();
 3     $timeout = 5;
 4     curl_setopt ($ch, CURLOPT_URL, ‘http://www.jb51.net/‘);
 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
10     echo $file_contents;
11 ?>
时间: 2024-08-04 01:41:56

php发送get、post请求的6种方法代码示例的相关文章

axios发送两次请求原因及解决方法

axios发送两次请求原因及解决方法 最近Vue项目中使用axios组件,在页面交互中发现axios会发送两次请求,一种请求方式为OPTIONS,另外一种为自己设置的. 如图: 什么是CORS通信? CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而克服了AJAX只能同源使用的限制. CORS需要浏览器和服务器同时支持.目前,所有浏览器都支持该功能,IE浏

项目中经常遇到的跨域请求的几种方法

什么是跨域 JSONP proxy代理 cors xdr 关于跨域无非就是jsonp和iframe,随着跨域请求的应用越来越多,W3C提供了跨域请求的标准方案(Cross-Origin Resource Sharing).IE8.Firefox 3.5 及其以后的版本.Chrome浏览器.Safari 4 等已经实现了 Cross-Origin Resource Sharing 规范,实现了跨域请求.在服务器响应客户端的时候,带上Access-Control-Allow-Origin头信息. 如

ASP.NET MVC 实现AJAX跨域请求的两种方法

通常发送AJAX请求都是在本域内完成的,也就是向本域内的某个URL发送请求,完成部分页面的刷新.但有的时候需要向其它域发送AJAX请求,完成数据的加载,例如Google. 在ASP.NET MVC 框架里实现跨域的AJAX请求有几种方式可以实现,以下就介绍常用的两种方法. 1.     发送JSONP请求 客户端: JQuery对发送JSONP请求有很好的支持,客户端通过. ajax() 函数发送请求,其中需要制定 dataType 为“jsonp”  jsonpCallback 为指定的回调函

Godaddy空间,PHP,邮件发送网页表单的两种方法

这里采用的是Godaddy 的Linux主机,Windows主机应该也是类似,本人未尝试. 第一种方法,利用php自带的mail()函数发送邮件 文件组织,将123.html与send.php两个文件放在同一目录下 123.html <!DOCTYPE html> <html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&g

【转】Android下编译jni库的二种方法(含示例) -- 不错

原文网址:http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html 总结如下:两种方法是:1)使用Android源码中的Make系统2)使用NDK(从NDK r5开始) ---------------------------------源码要求: 如果Android工程的顶层目录为my_android_project_name:1) my_android_project_name/jni/Android.mk文件的示例内容如下(以生成libjni_

php发送http请求的几种方法

有很多时候,我们还是需要用php去发送http请求的,它可以模拟浏览器的行为,通常它的应用场景有:1.后端测试自己的接口.2.后端请求别人的数据. 后端测试自己的接口,比如我们写了一个返回json数据的接口,我们可以让前端去测,但是前端不一定有空啊,或者前端界面还没做出来,由于界面不一定是现成的,因此测试起来也会有点麻烦.当然我们可以用谷歌浏览器的postman或者火狐的poster,这些都可以发送post.delete请求等等,但是它们应用起来并没有那么爽.因此,我们可以实用php编程的方式来

php发送post请求的三种方法

引用:http://blog.sjzycxx.cn/post/435/ 1.使用 file_get_contents() /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data post键值对数据 * @return string */function send_post($url, $post_data) { $postdata = http_build_query($post_data); $options = a

php发送get、post请求的几种方法

转自:http://blog.csdn.net/haha00217/article/details/7969504 方法1: 用file_get_contents 以get方式获取内容 1 <?php 2 $url='http://www.domain.com/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?> 方法2: 用fopen打开url, 以get方式获取内容 1 <?php 2 $fp = fopen($url, '

(转)php发送get、post请求的几种方法

方法1: 用file_get_contents 以get方式获取内容 1 <?php 2 $url='http://www.domain.com/'; 3 $html = file_get_contents($url); 4 echo $html; 5 ?> 方法2: 用fopen打开url, 以get方式获取内容 1 <?php 2 $fp = fopen($url, 'r'); 3 //返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等) 4 stream_ge