PHP   发布时间:2019-11-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP使用socket发送HTTP请求的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了php使用socket发送http请求的方法。分享给大家供大家参,具体如下:

socket方式

20,"usec"=>0)); socket_connect($socket,'www.baidu.com',80); //里面的换行代表 \r\n 注意拷贝的代码后面可能有空格 $http = <<http/1.0 Accept: */* User-Agent: Lowell-Agent Host: www.baidu.com Connection: Close eof; socket_write($socket,$http,strlen($http)); while($str = socket_read($socket,1024)) { echo $str; } socket_close($socket);

fsockopen方式

php;"> $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方式

php;"> $fp = stream_socket_client("tcp://www.baidu.com:80",30); if (!$fp) { echo "$errstr ($errno)
\n"; } else { $http = <<http/1.0 Accept: */* User-Agent: Lowell-Agent Host: www.baidu.com Connection: Close eof; fwrite($fp,1024); } fclose($fp); }

stream 方式(get):

php;"> $http = <<array( 'header' => $http,'timeout'=>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',$context);

stream 方式 post

:

'save','id'=>387171)); $http = <<array( 'header' => $http,//超时 秒 'method' => 'POST','content' => $postdata,         'protocol_version' => '1.1',); //参数格式参 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',$context);

注意:http1.1 中必须包含 Host 头, 而 http1.0中则可以没有

更多关于php相关内容感兴趣的读者可查看本站专题:《》、《》、《》及《

希望本文所述对大家php程序设计有所帮助。

大佬总结

以上是大佬教程为你收集整理的PHP使用socket发送HTTP请求的方法全部内容,希望文章能够帮你解决PHP使用socket发送HTTP请求的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:socket