欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP使用socket发送HTTP请求的方法

程序员文章站 2024-04-01 17:50:10
本文实例讲述了php使用socket发送http请求的方法。分享给大家供大家参考,具体如下: socket方式: $socket = socket_create...

本文实例讲述了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);
}

原始socket方式

$fp = stream_socket_client("tcp://www.baidu.com:80", $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno)<br />\n";
} else {
  $http = <<<eof
get / http/1.0
accept: */*
user-agent: lowell-agent
host: www.baidu.com
connection: close
eof;
  fwrite($fp, $http);
  while (!feof($fp)) {
    echo fgets($fp, 1024);
  }
  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(
    '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', 0, $context);

stream  方式 post:

$postdata = http_build_query(array('act'=>'save', 'id'=>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(
    'http' =>array(
        'header' => $http,
        'timeout'=>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程序设计有所帮助。