fsockopen(地址,端口,错误码,错误信息,超时) 返回资源类型
$fh = fsockopen ('192.168.1.100' , 80 ,$errno , $errstr , 5);//打开socket连接
$data = array('title'=>'abcde', 'content'=>'abcde'); //准备请求的数据
$data = http_build_query($data); //将请求的数据转成url编码(encode)
$req = array( //准备报文
'POST /test/content.php HTTP/1.1',
'Host: localhost',
'Content-type: application/x-www-form-urlencoded',
'Content-length: 24',
'',
"$data",
);
//拆分请求
$req = implode("\n", $req); //将报文转成字符串
//写入资源
fwrite($fh, $req); //将报文字符串写入资源
//循环读取返回值
while($row = fread($fh,32)) { //循环读取资源,显示到浏览器。
echo $row;
}
fclose($fh); //关闭资源
-------------------------------------------------------------------------------------------
$data = array('title'=>'灌水', 'content'=>'灌水机器人'); //准备请求数据
$data = http_build_query($data); //将请求的数据转成url编码(encode)
$opts = array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type:application/x-www-form-urlencoded',
'content'=>$data
)
);
//创建并返回一个资源流上下文
$context = stream_context_create($opts);
//将整个文件读入一个字符串,第三个参数是一个有效的上下文资源
echo file_get_contents('http://192.168.1.102/test/content.php', null, $context);
--------------------------------------------------------------------------------------------
$ch = curl_init(); //初始化
curl_setopt($ch, CURLOPT_URL, '192.168.1.100/test/content.php'); //创建连接$ch
//curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // PHP 5.6.0 后必须开启
curl_setopt($ch, CURLOPT_POSTFIELDS, ['title'=>'灌水', 'content'=>'灌水机器人']); //携带POST数据请求连接$ch
curl_exec(); //执行,并将内容输出到浏览器
curl_close($ch); //关闭资源
$fh = fsockopen ('192.168.1.100' , 80 ,$errno , $errstr , 5);//打开socket连接
$data = array('title'=>'abcde', 'content'=>'abcde'); //准备请求的数据
$data = http_build_query($data); //将请求的数据转成url编码(encode)
$req = array( //准备报文
'POST /test/content.php HTTP/1.1',
'Host: localhost',
'Content-type: application/x-www-form-urlencoded',
'Content-length: 24',
'',
"$data",
);
//拆分请求
$req = implode("\n", $req); //将报文转成字符串
//写入资源
fwrite($fh, $req); //将报文字符串写入资源
//循环读取返回值
while($row = fread($fh,32)) { //循环读取资源,显示到浏览器。
echo $row;
}
fclose($fh); //关闭资源
-------------------------------------------------------------------------------------------
$data = array('title'=>'灌水', 'content'=>'灌水机器人'); //准备请求数据
$data = http_build_query($data); //将请求的数据转成url编码(encode)
$opts = array(
'http'=>array(
'method'=>'POST',
'header'=>'Content-type:application/x-www-form-urlencoded',
'content'=>$data
)
);
//创建并返回一个资源流上下文
$context = stream_context_create($opts);
//将整个文件读入一个字符串,第三个参数是一个有效的上下文资源
echo file_get_contents('http://192.168.1.102/test/content.php', null, $context);
--------------------------------------------------------------------------------------------
$ch = curl_init(); //初始化
curl_setopt($ch, CURLOPT_URL, '192.168.1.100/test/content.php'); //创建连接$ch
//curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false); // PHP 5.6.0 后必须开启
curl_setopt($ch, CURLOPT_POSTFIELDS, ['title'=>'灌水', 'content'=>'灌水机器人']); //携带POST数据请求连接$ch
curl_exec(); //执行,并将内容输出到浏览器
curl_close($ch); //关闭资源