友善交流技术...

PHP Socket (实现http get 请求)

上一篇 / 下一篇  2011-12-14 13:44:52 / 个人分类:开发代码

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_DONTROUTE

Bypass 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、具体的代码实现如下。

<?php
 /*
 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);
?>

 


TAG:

 

评分:0

我来说两句

Open Toolbar