PHP中使用socket方式GET、POST数据实例
程序员文章站
2023-09-28 16:14:42
1. 使用 php 获得网页内容 get方式
复制代码 代码如下:
<?php
function socketget($url, &$ret)
{
$urlarr = parse_url($url);
$host = $urlarr['host'];
$port = isset($urlarr['port'])?$urlarr['port']:80;
$path = isset($urlarr['path'])?$urlarr['path']:"/";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
return false;
}
else
{
$out = "get $path http/1.1\r\n";
$out .= "host: $host\r\n";
$out .= "connection: close\r\n\r\n";
$ret = "";
fwrite($fp, $out);
while (!feof($fp))
{
$ret .= fgets($fp, 128);
}
fclose($fp);
}
return true;
}
?>
<?php
function socketpost($url, $data, &$ret)
{
$urlarr = parse_url($url);
$host = $urlarr['host'];
$port = isset($urlarr['port'])?$urlarr['port']:80;
$path = isset($urlarr['path'])?$urlarr['path']:"/";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
return false;
}
else
{
$out = "post $path http/1.1\r\n";
$out .= "host: $host\r\n";
$out .= "content-type: application/x-www-form-urlencoded\r\n";
$out .= "content-length: ".strlen($data)."\r\n";
$out .= "connection: keep-alive\r\n\r\n";
$out .= $data;
$ret = "";
fwrite($fp, $out);
while (!feof($fp))
{
$ret .= fgets($fp, 128);
}
fclose($fp);
}
return true;
}
?>
如果post报错,把$out .= "connection: keep-alive\r\n\r\n";中的keep-alive改成close
1. 使用 php 获得网页内容 get方式
复制代码 代码如下:
<?php
function socketget($url, &$ret)
{
$urlarr = parse_url($url);
$host = $urlarr['host'];
$port = isset($urlarr['port'])?$urlarr['port']:80;
$path = isset($urlarr['path'])?$urlarr['path']:"/";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
return false;
}
else
{
$out = "get $path http/1.1\r\n";
$out .= "host: $host\r\n";
$out .= "connection: close\r\n\r\n";
$ret = "";
fwrite($fp, $out);
while (!feof($fp))
{
$ret .= fgets($fp, 128);
}
fclose($fp);
}
return true;
}
?>
2. 使用 php 向页面 post 数据
复制代码 代码如下:
<?php
function socketpost($url, $data, &$ret)
{
$urlarr = parse_url($url);
$host = $urlarr['host'];
$port = isset($urlarr['port'])?$urlarr['port']:80;
$path = isset($urlarr['path'])?$urlarr['path']:"/";
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
return false;
}
else
{
$out = "post $path http/1.1\r\n";
$out .= "host: $host\r\n";
$out .= "content-type: application/x-www-form-urlencoded\r\n";
$out .= "content-length: ".strlen($data)."\r\n";
$out .= "connection: keep-alive\r\n\r\n";
$out .= $data;
$ret = "";
fwrite($fp, $out);
while (!feof($fp))
{
$ret .= fgets($fp, 128);
}
fclose($fp);
}
return true;
}
?>
如果post报错,把$out .= "connection: keep-alive\r\n\r\n";中的keep-alive改成close
上一篇: 高数计算,我Python替你承包了
下一篇: Yii实现自动加载类地图的方法
推荐阅读
-
Python使用post及get方式提交数据的实例
-
使用PHP Socket 编程模拟Http post和get请求
-
php使用socket post数据到其它web服务器的方法
-
PHP中使用socket方式GET、POST数据实例
-
微信小程序授权 获取用户的openid和session_key【后端使用java语言编写】,我写的是get方式,目的是测试能否获取到微信服务器中的数据,后期我会写上post请求方式。
-
PHP下使用CURL方式POST数据至API接口的代码
-
php使用file_get_contents(‘php://input‘)和$_POST的区别实例对比
-
php+mysqli使用面向对象方式更新数据库实例
-
php+mysqli使用面向对象方式查询数据库实例
-
php中使用Curl、socket、file_get_contents三种方法POST提交数据