php实现post和get
2015-08-26 03:55
579 查看
file_get_contents版本:
使用如下:
实战经验:
当我利用上述代码给另一台服务器发送http请求时,发现,如果服务器处理请求时间过长,本地的PHP会中断请求,即所谓的超时中断,第一个怀疑的是PHP本身执行时间的超过限制,但想想也不应该,因为老早就按照这篇文章设置了“PHP执行时间限制”(【推荐】PHP上传文件大小限制大全 ),仔细琢磨,想想,应该是http请求本身的一个时间限制,于是乎,就想到了怎么给http请求时间限制搞大一点。。。。。。查看PHP手册,果真有个参数
“ timeout ”,默认不知道多大,当把它的值设大一点,问题得已解决,弱弱地做个笔记~~~
Socket版本:
Curl版本:
Curl版本(2)
调用 WCF接口 的一个例子:$json =restRequest($r_url,'POST',json_encode($data));
01 | <?php |
02 | /** |
03 | * 发送post请求 |
04 | * @param string $url 请求地址 |
05 | * @param array $post_data post键值对数据 |
06 | * @return string |
07 | */ |
08 | function send_post( $url , $post_data ) { |
09 |
10 | $postdata =http_build_query( $post_data ); |
11 | $options = array ( |
12 | 'http' => array ( |
13 | 'method' => 'POST' , |
14 | 'header' => 'Content-type:application/x-www-form-urlencoded' , |
15 | 'content' => $postdata , |
16 | 'timeout' =>15 * 60 // 超时时间(单位:s) |
17 | ) |
18 | ); |
19 | $context =stream_context_create( $options ); |
20 | $result = file_get_contents ( $url ,false, $context ); |
21 |
22 | return $result ; |
23 | } |
1 | $post_data
array ( |
2 | 'username' => 'stclair2201' , |
3 | 'password' => 'handan' |
4 | ); |
5 | send_post( 'http://blog.snsgou.com' , $post_data ); |
实战经验:
当我利用上述代码给另一台服务器发送http请求时,发现,如果服务器处理请求时间过长,本地的PHP会中断请求,即所谓的超时中断,第一个怀疑的是PHP本身执行时间的超过限制,但想想也不应该,因为老早就按照这篇文章设置了“PHP执行时间限制”(【推荐】PHP上传文件大小限制大全 ),仔细琢磨,想想,应该是http请求本身的一个时间限制,于是乎,就想到了怎么给http请求时间限制搞大一点。。。。。。查看PHP手册,果真有个参数
“ timeout ”,默认不知道多大,当把它的值设大一点,问题得已解决,弱弱地做个笔记~~~
Socket版本:
01 | /** |
02 | * Socket版本 |
03 | * 使用方法: |
04 | * $post_string ="app=socket&version=beta"; |
05 | * request_by_socket('blog.snsgou.com','/restServer.php',$post_string); |
06 | */ |
07 | function request_by_socket( $remote_server , $remote_path , $post_string , $port =80, $timeout
|
08 | $socket = fsockopen ( $remote_server , $port , $errno , $errstr , $timeout ); |
09 | if (! $socket ) die ( "$errstr($errno)" ); |
10 | fwrite( $socket , "POST $remote_path HTTP/1.0" ); |
11 | fwrite( $socket , "User-Agent:Socket Example" ); |
12 | fwrite( $socket , "HOST:$remote_server" ); |
13 | fwrite( $socket , "Content-type:application/x-www-form-urlencoded" ); |
14 | fwrite( $socket , "Content-length:" .( strlen ( $post_string ) + 8) . "" ); |
15 | fwrite( $socket , "Accept:*/*" ); |
16 | fwrite( $socket , "" ); |
17 | fwrite( $socket , "mypost=$post_string" ); |
18 | fwrite( $socket , "" ); |
19 | $header = "" ; |
20 | while ( $str
fgets ( $socket ,4096))) { |
21 | $header .= $str ; |
22 | } |
23 |
24 | $data = "" ; |
25 | while (! feof ( $socket )) { |
26 | $data .= fgets ( $socket ,4096); |
27 | } |
28 |
29 | return $data ; |
30 | } |
Curl版本:
01 | /** |
02 | * Curl版本 |
03 | * 使用方法: |
04 | * $post_string ="app=request&version=beta"; |
05 | * request_by_curl('http://blog.snsgou.com/restServer.php',$post_string); |
06 | */ |
07 | function request_by_curl( $remote_server , $post_string ) { |
08 | $ch =curl_init(); |
09 | curl_setopt( $ch ,CURLOPT_URL, $remote_server ); |
10 | curl_setopt( $ch ,CURLOPT_POSTFIELDS, 'mypost=' . $post_string ); |
11 | curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,true); |
12 | curl_setopt( $ch ,CURLOPT_USERAGENT, "snsgou.com's CURL Example beta" ); |
13 | $data =curl_exec( $ch ); |
14 | curl_close( $ch ); |
15 |
16 | return $data ; |
17 | } |
Curl版本(2)
01 | /** |
02 | * 发送HTTP请求 |
03 | * |
04 | * @param string $url 请求地址 |
05 | * @param string $method 请求方式 GET/POST |
06 | * @param string $refererUrl 请求来源地址 |
07 | * @param array $data 发送数据 |
08 | * @param string $contentType |
09 | * @param string $timeout |
10 | * @param string $proxy |
11 | * @return boolean |
12 | */ |
13 | function send_request( $url , $data , $refererUrl = '' , $method = 'GET' , $contentType = 'application/json' , $timeout =30, $proxy =false) { |
14 | $ch =null; |
15 | if ( 'POST' === strtoupper ( $method )) { |
16 | $ch =curl_init( $url ); |
17 | curl_setopt( $ch ,CURLOPT_POST,1); |
18 | curl_setopt( $ch ,CURLOPT_HEADER,0 ); |
19 | curl_setopt( $ch ,CURLOPT_FRESH_CONNECT,1); |
20 | curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,1); |
21 | curl_setopt( $ch ,CURLOPT_FORBID_REUSE,1); |
22 | curl_setopt( $ch ,CURLOPT_TIMEOUT, $timeout ); |
23 | if ( $refererUrl ) { |
24 | curl_setopt( $ch ,CURLOPT_REFERER, $refererUrl ); |
25 | } |
26 | if ( $contentType ) { |
27 | curl_setopt( $ch ,CURLOPT_HTTPHEADER, array ( 'Content-Type:' . $contentType )); |
28 | } |
29 | if ( is_string ( $data )){ |
30 | curl_setopt( $ch ,CURLOPT_POSTFIELDS, $data ); |
31 | } else { |
32 | curl_setopt( $ch ,CURLOPT_POSTFIELDS,http_build_query( $data )); |
33 | } |
34 | } else if ( 'GET' === strtoupper ( $method )) { |
35 | if ( is_string ( $data )) { |
36 | $real_url = $url .( strpos ( $url , '?' ) ===false ? '?' : '' ). $data ; |
37 | } else { |
38 | $real_url = $url .( strpos ( $url , '?' ) ===false ? '?' : '' ).http_build_query( $data ); |
39 | } |
40 |
41 | $ch =curl_init( $real_url ); |
42 | curl_setopt( $ch ,CURLOPT_HEADER,0); |
43 | curl_setopt( $ch ,CURLOPT_HTTPHEADER, array ( 'Content-Type:' . $contentType )); |
44 | curl_setopt( $ch ,CURLOPT_RETURNTRANSFER,1); |
45 | curl_setopt( $ch ,CURLOPT_TIMEOUT, $timeout ); |
46 | if ( $refererUrl ) { |
47 | curl_setopt( $ch ,CURLOPT_REFERER, $refererUrl ); |
48 | } |
49 | } else { |
50 | $args =func_get_args(); |
51 | return false; |
52 | } |
53 |
54 | if ( $proxy ) { |
55 | curl_setopt( $ch ,CURLOPT_PROXY, $proxy ); |
56 | } |
57 | $ret =curl_exec( $ch ); |
58 | $info =curl_getinfo( $ch ); |
59 | $contents = array ( |
60 | 'httpInfo' => array ( |
61 | 'send' => $data , |
62 | 'url' => $url , |
63 | 'ret' => $ret , |
64 | 'http' => $info , |
65 | ) |
66 | ); |
67 |
68 | curl_close( $ch ); |
69 | return $ret ; |
70 | } |
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- GET和post取值限制区别分析
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- 深入C#中get与set的详解
- ASP.NET、ASP、PHP、JSP之间有什么区别?