首页 > 后端开发 > php教程 > PHP中socket如何发送HTTP请求

PHP中socket如何发送HTTP请求

墨辰丷
发布: 2023-03-29 18:46:01
原创
1770 人浏览过

本篇文章主要介绍PHP中socket如何发送HTTP请求,感兴趣的朋友参考下,希望对大家有所帮助。

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 = <<<eof
GET / 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方式

$fp = fsockopen("www.baidu.com", 80, $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)<br />\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);
}
登录后复制

stream 方式(get):

$http = <<<eof
Host: www.baidu.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cookie: BAIDUID=79D98B1AD8436C57B967E111E484F1CD:FG=1; BDUSS=lF-UFFOanFPVG92NmF4U3NiTEoxOFh4YVBCTnZaMUtoTUNhZmxrWThwN25IaUJVQVFBQUFBJCQAAAAAAAAAAAEAAADzo1gKc2lxaW5pYW8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOeR-FPnkfhTU; BAIDUPSID=79D98B1AD8436C57B967E111E484F1CD; BD_UPN=13314352; BD_HOME=1; H_PS_PSSID=10047_1435_10874_10212_10501_10496_10753_10796_10219_10355_10666_10597_10095_10658_10442_10700_10460_10360_10618; sug=3; sugstore=0; ORIGIN=2; bdime=0
Connection: keep-alive
Cache-Control: max-age=0
eof;
$hdrs = array(
    &#39;http&#39; =>array(
        &#39;header&#39; => $http,
        &#39;timeout&#39;=>1, //超时 秒
        &#39;method&#39; => &#39;GET&#39;, //默认方式
         &#39;protocol_version&#39; => &#39;1.1&#39;, //默认为 1.0
    ),
);
$context = stream_context_create($hdrs);
echo file_get_contents(&#39;http://www.baidu.com&#39;, 0, $context);
登录后复制

stream 方式 post:

$postdata = http_build_query(array(&#39;act&#39;=>&#39;save&#39;, &#39;id&#39;=>387171));
$http = <<<eof
Host: www.baidu.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Content-Type: application/x-www-form-urlencoded; charset=UTF-8 
Cookie: BAIDUID=79D98B1AD8436C57B967E111E484F1CD:FG=1; BDUSS=lF-UFFOanFPVG92NmF4U3NiTEoxOFh4YVBCTnZaMUtoTUNhZmxrWThwN25IaUJVQVFBQUFBJCQAAAAAAAAAAAEAAADzo1gKc2lxaW5pYW8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOeR-FPnkfhTU; BAIDUPSID=79D98B1AD8436C57B967E111E484F1CD; BD_UPN=13314352; BD_HOME=1; H_PS_PSSID=10047_1435_10874_10212_10501_10496_10753_10796_10219_10355_10666_10597_10095_10658_10442_10700_10460_10360_10618; sug=3; sugstore=0; ORIGIN=2; bdime=0
Connection: keep-alive
Cache-Control: max-age=0
eof;
#注意post方式需要增加Content-Type
$hdrs = array(
    &#39;http&#39; =>array(
        &#39;header&#39; => $http,
        &#39;timeout&#39;=>1, //超时 秒
        &#39;method&#39; => &#39;POST&#39;,
        &#39;content&#39; => $postdata,
         &#39;protocol_version&#39; => &#39;1.1&#39;, //默认为 1.0
    ),
);
$context = stream_context_create($hdrs);
echo file_get_contents(&#39;http://test.cm/song.php&#39;, 0, $context);
登录后复制

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

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php基于CodeIgniter实现图片上传、剪切功能的方法

php metaphone()函数和php localeconv() 函数实例详解

php图片上传类及调用方法

以上是PHP中socket如何发送HTTP请求的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板