看了PHP手册不怎么明白,有没有知道它倒是在哪方面应用最多,或者说解决什么问题最好?
解决方案 »
- php 超链接传值 报错404
- ruby到底是什么。。。
- 关于ajax+php多用户同时请求的问题,高手请进!
- 邀请大侠速成小网站之php+html,后台编程。
- 帮忙写一个正则,替换超连接。谢谢!
- Fatal error: Call to undefined function: mssql_connect() in D:\chen\htdocs\index.php on line 2
- 菜鸟数组问题!在线
- ADODB模拟mysql字段的autoincrement插入数据库
- PHP5中的 析构函数 是什么意思
- 怎么会这样的问题....帮忙啊!!!!!给分...在线等...
- php与html混编
- 包含文件出错,重建文件就正常了,why?
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>模拟登陆页面.取页面内容.对外做接口.
Description
resource fsockopen ( string $hostname [, int $port= -1 [, int &$errno [, string &$errstr [, float $timeout= ini_get("default_socket_timeout") ]]]] )Initiates a socket connection to the resource specified by hostname .PHP supports targets in the Internet and Unix domains as described in List of Supported Socket Transports. A list of supported transports can also be retrieved using stream_get_transports().The socket will by default be opened in blocking mode. You can switch it to non-blocking mode by using stream_set_blocking().
Parametershostname If you have compiled in OpenSSL support, you may prefix the hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.
port The port number.
errno If provided, holds the system level error number that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket.
errstr The error message as a string.
timeout The connection timeout, in seconds. Note: If you need to set a timeout for reading/writing data over the socket, use stream_set_timeout(), as the timeout parameter to fsockopen() only applies while connecting the socket.Return Valuesfsockopen() returns a file pointer which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()). If the call fails, it will return FALSE
Errors/ExceptionsThrows E_WARNING if hostname is not a valid domain.
ChangelogVersion Description
4.3.0 Added support for the timeout parameter on win32.
4.3.0 SSL and TLS over TCP/IP support was added.
4.0.0 UDP support was added.ExamplesExample #1 fsockopen() Example
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>Example #2 Using UDP connectionThe example below shows how to retrieve the day and time from the UDP service "daytime" (port 13) in your own machine.
<?php
$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!$fp) {
echo "ERROR: $errno - $errstr<br />\n";
} else {
fwrite($fp, "\n");
echo fread($fp, 26);
fclose($fp);
}
?>Notes Note: Depending on the environment, the Unix domain or the optional connect timeout may not be available.WarningUDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only become apparent when you read or write data to/from the socket. The reason for this is because UDP is a "connectionless" protocol, which means that the operating system does not try to establish a link for the socket until it actually needs to send or receive data. Note: When specifying a numerical IPv6 address (e.g. fe80::1), you must enclose the IP in square brackets—for example, tcp://[fe80::1]:80.
善用被你冷落的php.net
然而对于隐藏在后面的通讯原理,还是得有个大概了解。
为什么敲进url,浏览器就帮我获取到相关的网页呢?
其实浏览器只不过是帮我们封装好了socket请求。那我们在程序中就想访问对方站点怎么办?很简单,向站点发送socket请求,写入http协议。站点一监听到socket,就开始检查协议内容,正确的话就给我们返回内容。
怎么发送?利用fsockopen建立连接,再写入头信息.或者curl,file_get_contents,原理都一样。