PHP请求http(file_get_content)
来源:互联网 发布:csgo数据互换器一次性 编辑:程序博客网 时间:2024/05/16 08:38
有时候,我们需要在服务器端模拟 POST/GET 等请求,也就是在 PHP 程序中去实现模拟,改怎么做到呢?或者说,在 PHP 程序里,给你一个数组,如何将这个数组 POST/GET 到另外一个地址呢?当然,使用 CURL 很容易办到,那么如果不使用 CURL 库,又该怎么办呢?其实,在 PHP 里已经有相关的函数实现了,这个函数就是接下来要讲的stream_context_create()。
直接 show you the code,这是最好的方法:
01
$data
=
array
(
02
'foo'
=>
'bar'
,
03
'baz'
=>
'boom'
,
04
'site'
=>
'www.nowamagic.net'
,
05
'name'
=>
'nowa magic'
);
06
07
$data
= http_build_query(
$data
);
08
09
//$postdata = http_build_query($data);
10
$options
=
array
(
11
'http'
=>
array
(
12
'method'
=>
'POST'
,
13
'header'
=>
'Content-type:application/x-www-form-urlencoded'
,
14
'content'
=>
$data
15
//'timeout' => 60 * 60 // 超时时间(单位:s)
16
)
17
);
18
19
$url
=
"http://www.nowamagic.net/test2.php"
;
20
$context
= stream_context_create(
$options
);
21
$result
=
file_get_contents
(
$url
, false,
$context
);
22
23
echo
$result
;
http://www.nowamagic.net/test2.php 的代码为:
1
$data
=
$_POST
;
2
3
echo
'<pre>'
;
4
print_r(
$data
);
5
echo
'</pre>'
;
运行结果为:
1
Array
2
(
3
[foo] => bar
4
[baz] => boom
5
[site] => www.nowamagic.net
6
[name] => nowa magic
7
)
一些要点讲解:
1. 以上程序用到了 http_build_query() 函数,如果需要了解,可以参看 PHP函数补完:http_build_query()构造URL字符串。
2. stream_context_create() 是用来创建打开文件的上下文件选项的,比如用POST访问,使用代理,发送header等。就是创建一个流,再举一个例子吧:
01
$context
= stream_context_create(
array
(
02
'http'
=>
array
(
03
'method'
=>
'POST'
,
04
'header'
=> sprintf(
"Authorization: Basic %s\r\n"
,
base64_encode
(
$username
.
':'
.
$password
)).
05
"Content-type: application/x-www-form-urlencoded\r\n"
,
06
'content'
=> http_build_query(
array
(
'status'
=>
$message
)),
07
'timeout'
=> 5,
08
),
09
));
10
$ret
=
file_get_contents
(
'http://twitter.com/statuses/update.xml'
, false,
$context
);
3. stream_context_create创建的上下文选项即可用于流(stream),也可用于文件系统(file system)。对于像 file_get_contents、file_put_contents、readfile直接使用文件名操作而没有文件句柄的函数来说更有用。stream_context_create增加header头只是一部份功能,还可以定义代理、超时等。这使得访问web的功能不弱于curl。
4. stream_context_create() 作用:创建并返回一个文本数据流并应用各种选项,可用于fopen(),file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。
5. stream_context_create 还能通过增加 timeout 选项解决file_get_contents超时处理:
01
$opts
=
array
(
02
'http'
=>
array
(
03
'method'
=>
"GET"
,
04
'timeout'
=>60,
05
)
06
);
07
//创建数据流上下文
08
$context
= stream_context_create(
$opts
);
09
10
$html
=
file_get_contents
(
'http://www.nowamagic.net'
, false,
$context
);
11
12
//fopen输出文件指针处的所有剩余数据:
13
//fpassthru($fp); //fclose()前使用
- PHP请求http(file_get_content)
- PHP http(file_get_content) GET与POST请求方式
- PHP http(file_get_content) GET与POST请求方式
- [PHP]使用file_get_content
- php http post 方式有curl 、 file_get_content 、 fsocket ,来post提交数据
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- PHP发送POST请求的三种方式 分别使用curl file_get_content fsocket 来实现post提交数据
- php中Http请求!
- php请求http
- PHP模拟http请求
- PHP 发送HTTP请求
- php 发送 http 请求
- php异步http请求
- php模拟http请求
- php http请求
- PHP模拟HTTP请求
- PHP之Http请求
- PHP模拟http请求
- crash统计跟踪工具
- Python:使用ctypes库调用外部DLL(转)
- MVC缓存,使用数据层缓存,添加或修改时让缓存失效
- spring容器加载完毕干一件事情(利用ContextRefreshedEvent事件)
- java定时每天执行任务
- PHP请求http(file_get_content)
- C/C++中volatile关键字
- label里默认的字怎么设
- Java 实现单链表反序
- hdu3572 任务分配/最大流判断满流
- 对话框打不开
- openapi 简介
- P值之死
- POJ2262