php socket提供http,PHP Socket (实现http get 请求)

1、通过PHP socket实现 http get请求

目的:学习PHP - socket 通信过程

2、 开发socket 注意的问题

1)socket_send  and socket_recv

第四个参数一般都写0就可以的,如果按PHP的帮助文档中写如下的参数如:

MSG_OOBSend OOB (out-of-band) data.

MSG_EORIndicate a record mark. The sent data completes the record.

MSG_EOFClose the sender side of the socket and include an appropriate notification of this at the end of the sent data. The sent data completes the transaction.

MSG_DONTROUTEBypass routing, use direct interface.

都会出现下面的错误

Warning: socket_recv() [function.socket-recv]: unable to read from socket [0]: 操作成功完成。 inD:\phpwork\fsoap\socket\aa.phpon line30

buf1 out put

2)php_sockets.dll 无法加载问题

可以直接用命令 dl(); 方式加载

3、具体的代码实现如下。

/*

DATE: 2011-12-14

AUTHOR: I see

Function: 通过Socket 实现 http get 请求。

*/

dl('php_sockets.dll');

set_time_limit(0);

error_reporting(E_ALL);

$service_port=9080;

$address="192.168.37.22";

//创建socket

if (false == ($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {

echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";

}

//创建链接

$result=socket_connect($socket,$address,$service_port);

$InfoStr = "GET/umsrest/rs/users/getUserByName?userName=test_fang@163.comHTTP/1.1\r\n";

$InfoStr .= "Host: 192.168.37.22:9080 \r\n";

$InfoStr .= "Connection: Close\r\n\r\n";

//发送数据

socket_send($socket,$InfoStr,strlen($InfoStr),0);

//接受数据

socket_recv($socket,$buf1,9000,0);

//打印数据

echo 'buf1 out put '.$buf1 ;

//关闭链接

socket_close($socket);

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值