This example describes how PHP uses the socket to send HTTP requests. Share to everyone for your reference, specific as follows:
Socket Mode :
$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 ',);
It's a newline. Note that the copied code may be followed by a space
$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 Way :
$fp = Fsockopen ("www.baidu.com", $errno, $errstr,);
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);
Original Socket Method :
$fp = Stream_socket_client ("tcp://www.baidu.com:80", $errno, $errstr);
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 mode (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-uffoanfpvg92nmf4u3niteoxofh4yvbctnzamutotunhzmxrwthwn25iaujvqvfbqufbjcqaaaaaaaaaaaeaaadzo1gkc2lxaw5pyw8aaaaaaaaaaaaaaa Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoer-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,//Timeout seconds ' method ' =>
' Get ',//Default way ' protocol_version ' => ' 1.1 ',//default to 1.0); Argument latticeThe format of reference http://php.net/manual/zh/context.http.php//curl mode can be referenced; http://php.net/manual/zh/context.curl.php $context =
Stream_context_create ($hdrs);
echo file_get_contents (' http://www.baidu.com ', 0, $context);
stream Way 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-uffoanfpvg92nmf4u3niteoxofh4yvbctnzamutotunhzmxrwthwn25iaujvqvfbqufbjcqaaaaaaaaaaaeaaadzo1gkc2lxaw5pyw8aaaaaaaaaaaaaaa Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaoer-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,//Timeout seconds ' method ' => ' POST ', ' content ' => $postdat
A, ' protocol_version ' => ' 1.1 ',//default to 1.0); Parameter format reference http://php.net/manual/zh/context.http.php//curl format can be referenced; http://php.net/manual/zh/context.curl.php $
Context = Stream_context_create ($hdrs);
echo file_get_contents (' http://test.cm/song.php ', 0, $context);
Note: The http1.1 must contain the Host header, while the http1.0 can not
More about PHP Interested readers can view the site topics: "PHP Socket Usage Summary", "PHP basic Grammar Introductory Course", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"
I hope this article will help you with the PHP program design.