socket方式:
$socket = socket_create(af_inet, sock_stream, sol_tcp);//socket_set_option($socket, sol_socket, so_sndtimeo, array(sec=>20, usec=>0));socket_connect($socket, 'www.baidu.com', 80);//里面的换行代表 \r\n 注意拷贝的代码后面可能有空格$http = <<
$fp = fsockopen(www.baidu.com, 80, $errno, $errstr, 30);if (!$fp) { echo $errstr ($errno)
\n;} else { $out = get / http/1.1\r\n; $out .= host: www.baidu.com\r\n; $out .= connection: close\r\n\r\n; fwrite($fp, $http); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp);}
原始socket方式:
$fp = stream_socket_client(tcp://www.baidu.com:80, $errno, $errstr, 30);if (!$fp) { echo $errstr ($errno)
\n;} else { $http = <<
$http = 1, //超时 秒 'method' => 'get', //默认方式 'protocol_version' => '1.1', //默认为 1.0 ),);//参数格式参考 http://php.net/manual/zh/context.http.php//curl方式的格式可以参考; http://php.net/manual/zh/context.curl.php$context = stream_context_create($hdrs);echo file_get_contents('http://www.baidu.com', 0, $context);
stream 方式 post:
$postdata = http_build_query(array('act'=>'save', 'id'=>387171));$http = 1, //超时 秒 'method' => 'post', 'content' => $postdata, 'protocol_version' => '1.1', //默认为 1.0 ),);//参数格式参考 http://php.net/manual/zh/context.http.php//curl方式的格式可以参考; http://php.net/manual/zh/context.curl.php$context = stream_context_create($hdrs);echo file_get_contents('http://test.cm/song.php', 0, $context);
注意:http1.1 中必须包含 host 头, 而 http1.0中则可以没有
更多关于php相关内容感兴趣的读者可查看本站专题:《php socket用法总结》、《php基本语法入门教程》、《php错误与异常处理方法总结》及《php常用函数与技巧总结》
希望本文所述对大家php程序设计有所帮助。
您可能感兴趣的文章:利用php如何实现socket服务器php 利用socket发送http请求(get,post)php的socket网络编程入门指引详解php的socket通信php自定义类fsocket模拟post或get请求的方法php+html5基于websocket实现聊天室的方法php的socket通信之udp通信实例php使用socket post数据到其它web服务器的方法php socket编程详解php中关于socket的系列函数总结php socket通信(tcp/udp)实例分析
http://www.bkjia.com/phpjc/1099067.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1099067.htmltecharticlephp使用socket发送http请求的方法,phpsocket发送请求 本文实例讲述了php使用socket发送http请求的方法。分享给大家供大家参考,具体如下: so...