php中fsockopen用法实例
这篇文章主要介绍了php中fsockopen用法,实例分析了fsockopen的创建、写入及关闭等具体流程,需要的朋友可以参考下
本文实例讲述了php中fsockopen用法。分享给大家供大家参考。
具体实现方法如下:
复制代码 代码如下:
$fp=fsockopen("127.0.0.1",80); //打开数据流
if(!$fp) //如果打开出错
{
echo "unable to openn"; //输出内容
}
else //如果成功打开
{
fwrite($fp,"get / http/1.0rnrn"); //向数据流写入内容
stream_set_timeout($fp,2); //进行超时设置
$res=fread($fp,2000); //读取内容
$info=stream_get_meta_data($fp); //获取数据流报头
fclose($fp); //关闭数据流
if($info['timed_out']) //如果超时
{
echo 'connection timed out!'; //输出内容
}
else
{
echo $res; //输出读取内容
}
}
//实例二
//创建服务端
$socket=stream_socket_server("tcp://0.0.0.0:8000",$errno,$errstr);
//如果创建失败
if(!$socket)
{
echo "$errstr ($errno)
n";
}
//如果创建成功
else
{
//接受连接
while($conn=stream_socket_accept($socket))
{
//写入数据
fwrite($conn,'the local time is '.date('n/j/y g:i a')."n");
//关闭连接
fclose($conn);
}
//关闭socket
fclose($socket);
}
//
$file="test.txt"; //定义文件
$fp=fopen($file,"w"); //打开数据流
if($fp) //如果成功打开
{
stream_set_write_buffer($fp,0); //设置缓冲区
fwrite($fp,$output); //写入内容
fclose($fp); //关闭数据流
}
希望本文所述对大家的php程序设计有所帮助。
,
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
