- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章php 利用socket发送GET,POST请求的实例代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
作为php程序员一定会接触http协议,也只有深入了解http协议,编程水平才会更进一步。最近我一直在学习php的关于http的编程,许多东西恍然大悟,受益匪浅。希望分享给大家。本文需要有一定http基础的开发者阅读.
今天给大家带来的是如何利用socket发送GET,POST请求。我借用燕十八老师封装好的一个Http类给进行说明.
在日常编程中相信很多人和我一样大部分时间是利用浏览器向服务器提出GET,POST请求,那么可否利用其它方式提出GET,POST请求呢?答案必然是肯定的。了解过HTTP协议的人知道,浏览器提交请求的实质是向服务器发送一个请求信息,这个请求信息有请求行,请求头,请求体(非必须)构成。服务器根据请求信息返回一个响应信息。连接断开.
HTTP请求的格式如下所示:
1
2
3
4
|
<
request-line
>
<
headers
>
<
blank
line>
[<
request-body
>]
|
HTTP响应的格式与请求的格式十分相似:
1
2
3
4
5
|
<
status-line
>
<
headers
>
<
blank
line>
[<
response-body
>]
|
我们可以利用HTTP发送请求的原理,可以重新考虑利用socket发送HTTP请求.
Socket的英文原义是“孔”或“插座”。通常也称作“套接字”,用于描述IP地址和端口,是一个通信链的句柄,可以用来实现不同虚拟机或不同计算机之间的通信。在Internet上的主机一般运行了多个服务软件,同时提供几种服务。每种服务都打开一个Socket,并绑定到一个端口上,不同的端口对应于不同的服务。如此看来,其实利用socket操作远程文件和读写本地的文件一样容易,把本地文件看成通过硬件传输,远程文件通过网线传输就行了.
因而可以将发送请求的考虑成 建立连接->打开socket接口(fsockopen())->写入请求(fwrite())->读出响应(fread()->关闭文件(fclose())。话不多说,直接上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
interface
Proto {
// 连接url
function
conn(
$url
);
//发送get查询
function
get();
// 发送post查询
function
post();
// 关闭连接
function
close();
}
class
Http
implements
Proto {
const
CRLF =
"\r\n"
;
protected
$errno
= -1;
protected
$errstr
=
''
;
protected
$response
=
''
;
protected
$url
= null;
protected
$version
=
'HTTP/1.1'
;
protected
$fh
= null;
protected
$line
=
array
();
protected
$header
=
array
();
protected
$body
=
array
();
public
function
__construct(
$url
) {
$this
->conn(
$url
);
$this
->setHeader(
'Host: '
.
$this
->url[
'host'
]);
}
// 此方法负责写请求行
protected
function
setLine(
$method
) {
$this
->line[0] =
$method
.
' '
.
$this
->url[
'path'
] .
'?'
.
$this
->url[
'query'
] .
' '
.
$this
->version;
}
// 此方法负责写头信息
public
function
setHeader(
$headerline
) {
$this
->header[] =
$headerline
;
}
// 此方法负责写主体信息
protected
function
setBody(
$body
) {
$this
->body[] = http_build_query(
$body
);
}
// 连接url
public
function
conn(
$url
) {
$this
->url =
parse_url
(
$url
);
// 判断端口
if
(!isset(
$this
->url[
'port'
])) {
$this
->url[
'port'
] = 80;
}
// 判断query
if
(!isset(
$this
->url[
'query'
])) {
$this
->url[
'query'
] =
''
;
}
$this
->fh =
fsockopen
(
$this
->url[
'host'
],
$this
->url[
'port'
],
$this
->errno,
$this
->errstr,3);
}
//构造get请求的数据
public
function
get() {
$this
->setLine(
'GET'
);
$this
->request();
return
$this
->response;
}
// 构造post查询的数据
public
function
post(
$body
=
array
()) {
$this
->setLine(
'POST'
);
// 设计content-type
$this
->setHeader(
'Content-type: application/x-www-form-urlencoded'
);
// 设计主体信息,比GET不一样的地方
$this
->setBody(
$body
);
// 计算content-length
$this
->setHeader(
'Content-length: '
.
strlen
(
$this
->body[0]));
$this
->request();
return
$this
->response;
}
// 真正请求
public
function
request() {
// 把请求行,头信息,实体信息 放在一个数组里,便于拼接
$req
=
array_merge
(
$this
->line,
$this
->header,
array
(
''
),
$this
->body,
array
(
''
));
//print_r($req);
$req
= implode(self::CRLF,
$req
);
//echo $req; exit;
fwrite(
$this
->fh,
$req
);
while
(!
feof
(
$this
->fh)) {
$this
->response .=
fread
(
$this
->fh,1024);
}
$this
->close();
// 关闭连接
}
// 关闭连接
public
function
close() {
fclose(
$this
->fh);
}
}
|
利用此类发送一个简单的GET请求:
1
2
3
4
5
6
7
8
|
<?php
//记得引用Http类
$url
=
"http://home.cnblogs.com/u/DeanChopper/"
;
$http
=
new
Http(
$url
);
$response
=
$http
->get();
print_r(
$response
);
|
返回值为信息,可以对响应信息进行进一步处理,得到自己想得到的内容.
以上就是php 利用socket发送GET,POST请求的实例代码的详细内容,更多关于php 发送GET,POST请求的资料请关注我其它相关文章! 。
原文链接:https://www.cnblogs.com/DeanChopper/p/4681401.html 。
最后此篇关于php 利用socket发送GET,POST请求的实例代码的文章就讲到这里了,如果你想了解更多关于php 利用socket发送GET,POST请求的实例代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
基于 socket.io 的官方网站 http://socket.io/#how-to-use , 我找不到任何术语。socket.emit 、 socket.on 和 socket.send 之间有
我正在使用 lua-socket 3.0rc1.3(Ubuntu Trusty 附带)和 lua 5.1。我正在尝试监听 unix 域套接字,我能找到的唯一示例代码是 this -- send std
这两者有什么区别? 我注意到如果我在一个工作程序中从 socket.emit 更改为 socket.send ,服务器无法接收到消息,虽然我不明白为什么。 我还注意到,在我的程序中,如果我从 sock
使用套接字在两台服务器之间发送数据是个好主意,还是应该使用 MQ 之类的东西来移动数据。 我的问题:套接字是否可靠,如果我只需要一次/有保证的数据传输? 还有其他解决方案吗? 谢谢。 最佳答案 套接字
引自 this socket tutorial : Sockets come in two primary flavors. An active socket is connected to a
我已经安装了在端口81上运行的流服务器“Lighttpd”(light-tpd)。 我有一个C程序,它使用套接字api创建的服务器套接字在端口80上监听http请求。 我希望从客户端收到端口80上的请
这是我正在尝试做的事情: 当有新消息可用时,服务器会将消息发送给已连接的客户端。另一方面,客户端在连接时尝试使用send()向服务器发送消息,然后使用recv()接收消息,此后,客户端调用close(
如何将消息发送到动态 session 室,以及当服务器收到该消息时,如何将该消息发送到其他成员所在的同一个 session 室? table_id是房间,它将动态设置。 客户: var table_i
这是我尝试但不起作用的方法。我可以将传入的消息从WebSocket连接转发到NetSocket,但是只有NetSocket收到的第一个消息才到达WebSocket后面的客户端。 const WebSo
我正在实现使用boost将xml发送到客户端的服务器。我面临的问题是缓冲区不会立即发送并累积到一个点,然后发送整个内容。这在我的客户端造成了一个问题,当它解析xml时,它可能具有不完整的xml标记(不
尝试使用Nginx代理Gunicorn套接字。 /etc/systemd/system/gunicorn.service文件 [Unit] Description=gunicorn daemon Af
我正在使用Lua套接字和TCP制作像聊天客户端和服务器这样的IRC。我要弄清楚的主要事情是如何使客户端和服务器监听消息并同时发送它们。由于在服务器上执行socket:accept()时,它将暂停程序,
我看了一下ZMQ PUSH/PULL套接字,尽管我非常喜欢简单性(特别是与我现在正在通过UDP套接字在系统中实现的自定义碎片/ack相比),但我还是希望有自定义负载平衡功能,而不是幼稚的回合-robi
我正在编写一个应用程序,其中有多个 socket.io 自定义事件,并且所有工作正常,除了这个: socket.on("incomingImg", function(data) {
在我的应用程序中,我向服务器发送了两条小消息(类似 memcached 的服务)。在类似 Python 的伪代码中,这看起来像: sock.send("add some-key 0") ignored
很抱歉再次发布此问题,但大多数相关帖子都没有回答我的问题。我在使用 socket.io 的多个连接时遇到问题我没有使用“socket.socket.connect”方法,但我从第一次连接中得到了反馈。
我尝试使用 socket.io 客户端连接到非 socket.io websocket 服务器。但我做不到。我正在尝试像这样连接到套接字服务器: var socket = io.connect('ws
我遇到了一个奇怪的问题。在我非常基本的服务器中,我有: server.listen(8001); io.listen(server); var sockets = io.sockets; 不幸的是,套
我正在使用带套接字 io 的sailsjs。帆的版本是 0.10.5。我有以下套接字客户端进行测试: var socketIOClient = require('socket.io-client');
这个问题在这里已经有了答案: What is the fundamental difference between WebSockets and pure TCP? (4 个答案) 关闭 4 年前。
我是一名优秀的程序员,十分优秀!